如何创建随机数量的 D2D 形状(矩形和椭圆形)并在绘图时将它们作为数组引用?

发布于 2024-10-08 08:17:15 字数 574 浏览 4 评论 0原文

让我详细说明一下。我像这样定义一个 D2D 矩形:

D2D1_RECT_F rect1 = D2D1::RectF(5, 0, 150, 150);

和一个椭圆:

D2D1_ELLIPSE ellipse1 = D2D1::Ellipse(D2D1::Point2F(75.f, 75.f), 75.f, 75.f);

要绘制这些形状,我首先对它们进行变换并将它们传递给渲染目标:

m_pRenderTarget->SetTransform(D2D1::Matrix3x2F::Translation(D2D1::SizeF(200, 50)));
m_pRenderTarget->FillRectangle(&rect1, m_pLinearGradientBrush);

我想要一种方法来创建随机数量的矩形和椭圆,并将它们存储在数组,然后也能够绘制它们。我有一个返回从零到五的随机数的函数。我希望能够使用该数字创建一个指向这些形状的数组,并迭代它们以将它们绘制到屏幕上。关于如何解决这个问题有什么想法吗?

Let me elaborate. I define a D2D Rectangle like so:

D2D1_RECT_F rect1 = D2D1::RectF(5, 0, 150, 150);

and an ellipse as:

D2D1_ELLIPSE ellipse1 = D2D1::Ellipse(D2D1::Point2F(75.f, 75.f), 75.f, 75.f);

To draw these shapes, I first transform them and pass them to the rendertarget:

m_pRenderTarget->SetTransform(D2D1::Matrix3x2F::Translation(D2D1::SizeF(200, 50)));
m_pRenderTarget->FillRectangle(&rect1, m_pLinearGradientBrush);

I'd like a way to create a random number of rectangles and ellipses, and store them in an array, and then be able to draw them as well. I have a function that returns a random number from zero to five. I want to be able to use that number to create an array that points to these shapes, and iterates through them to draw them to the screen. Any ideas on how I can approach this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光沙漏 2024-10-15 08:17:16

您可以通过以下两种方法之一来实现此目的:

选项 1 - 创建 2 个分别包含矩形和椭圆形的数组。然后我们想要选择一个随机形状来绘制,首先选择随机数组(选择是否绘制椭圆或矩形),然后从该数组中选择一个特定的矩形/椭圆。

选项 2 - 使用 OO 创建多态 Draw 函数。

// Define new base class for your shapes
class DrawableShape
{
    HRESULT DrawMe(ID2D1RenderTarget* pUseThisRT);
};

// Create a MyD2DEllipse class implementing DrawableShape
class MyD2DEllipse : public D2D1_RECT_F, public DrawableShape
{
    HRESULT DrawMe(... pUseThisRT)
    {
        pUseThisRT->FillEllipse(this, ...);
    }
};

// Similarly create MyD2DRectangle
class MyD2DRectangle : ..
{
    ...
};

然后,您可以创建一个 DrawableShape[] 数组,您可以从中随机选择。

void DrawRandomShape(DrawableShape* shapes[])
{
   shapes[rand()]->DrawMe(pUseThisRT);
}

You could achieve this is one of 2 ways:

Option 1 - Create 2 arrays containing Rectangles and Ellipses respectively. Then we you want to choose a random shape to draw, you first pick the random array (choosing whether to draw an ellipse or rect), and then choose a particular rect/ellipse from that array.

Option 2 - Use OO to create a polymorphic Draw functions.

// Define new base class for your shapes
class DrawableShape
{
    HRESULT DrawMe(ID2D1RenderTarget* pUseThisRT);
};

// Create a MyD2DEllipse class implementing DrawableShape
class MyD2DEllipse : public D2D1_RECT_F, public DrawableShape
{
    HRESULT DrawMe(... pUseThisRT)
    {
        pUseThisRT->FillEllipse(this, ...);
    }
};

// Similarly create MyD2DRectangle
class MyD2DRectangle : ..
{
    ...
};

You can then create an array of DrawableShape[] from which you can choose randomly from.

void DrawRandomShape(DrawableShape* shapes[])
{
   shapes[rand()]->DrawMe(pUseThisRT);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文