[英]Draw Shape in Windows Form Application
I am trying to draw a shape with 4 corners. 我正在尝试绘制一个有4个角的形状。 The corner details are given in X and Y co-ordinate (as shown in the Picture below).
角部细节以X和Y坐标给出(如下图所示)。 I tried a method as given by this link: Drawing Colors in a picturebox?
我尝试了此链接提供的方法: 在图片框中绘制颜色? .
。 But the issue is it only for rectangles.
但是问题是它仅适用于矩形。
Could any one propose something. 任何人都可以提出一些建议。 I need it basically to generate a swept path (area taken by a car while driving) of the car.
我基本上需要它来生成汽车的后掠路径(汽车行驶时的区域)。 So, I get center of the Car in X and Y and the Orientation in degrees.
因此,我在X和Y方向上获得了Car的中心,在角度上获得了Orientation。 From that I determine the Corner Points of the car in X and Y space.
由此确定在X和Y空间中汽车的拐角点。 Now I need to show it Visualize it.
现在,我需要对其进行可视化显示。 Please help.
请帮忙。
You can use the Graphics.DrawPolygon
(or Graphics.FillPolygon
) method in the OnDraw
method of your Form/Control, as follows: 您可以在Form / Control的
OnDraw
方法中使用Graphics.DrawPolygon
(或Graphics.FillPolygon
)方法,如下所示:
protected override void OnPaint(PaintEventArgs e)
{
// If there is an image and it has a location,
// paint it when the Form is repainted.
base.OnPaint(e);
PointF[] rotatedVertices = // Your rotated rectangle vertices
e.Graphics.DrawPolygon(yourPen, rotatedVertices);
// OR
e.Graphics.FillPolygon(new SolidBrush(Color.Red), rotatedVertices);
}
Since you know the rotation degree, you can use Graphics.RotateTransform
for that. 既然知道旋转度,就可以使用
Graphics.RotateTransform
。 This way you don't need to calculate the corners yourself (guess this implementation is faster). 这样,您无需自己计算转角(猜测此实现会更快)。
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.RotateTransform(45 /* your degrees here */);
e.Graphics.FillRectangle(Brushes.Red, 10, 10, 200, 100);
}
Note that it rotates around (0;0)
, so you may need to translate it (using Graphics.TranslateTransform
), too. 请注意,它绕
(0;0)
旋转,因此您可能也需要对其进行翻译(使用Graphics.TranslateTransform
)。
You can use the Rectangle
class along with the Matrix
class to create a rectangle and then rotate it by your orientation like so: 您可以将
Rectangle
类和Matrix
类一起使用来创建一个矩形,然后按照您的方向旋转它,如下所示:
Graphics g = new Graphics()
Rectangle car = new Rectangle(200, 200, 100, 50)
Matrix m = new Matrix()
m.RotateAt(orientation, new PointF(car.Left + (car.Width / 2), car.Top + (car.Height / 2)));
g.Transform = m
g.FillRectangle(Pens.Red, car)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.