如何创建随机数量的 D2D 形状(矩形和椭圆形)并在绘图时将它们作为数组引用?
让我详细说明一下。我像这样定义一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过以下两种方法之一来实现此目的:
选项 1 - 创建 2 个分别包含矩形和椭圆形的数组。然后我们想要选择一个随机形状来绘制,首先选择随机数组(选择是否绘制椭圆或矩形),然后从该数组中选择一个特定的矩形/椭圆。
选项 2 - 使用 OO 创建多态 Draw 函数。
然后,您可以创建一个
DrawableShape[]
数组,您可以从中随机选择。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.
You can then create an array of
DrawableShape[]
from which you can choose randomly from.