问题
Basically i have a menu that user choose the shape wanted to be drawn, then the user click on two points, between those two points the chosen shape will be drawn.
I did the Square which is calculated this way
// calculate ranges and mid points
xDiff = oppPt.X - keyPt.X;
yDiff = oppPt.Y - keyPt.Y;
xMid = (oppPt.X + keyPt.X) / 2;
yMid = (oppPt.Y + keyPt.Y) / 2;
// draw square
g.DrawLine(blackPen, (int)keyPt.X, (int)keyPt.Y,
(int)(xMid + yDiff / 2), (int)(yMid - xDiff / 2));
g.DrawLine(blackPen, (int)(xMid + yDiff / 2), (int)(yMid - xDiff / 2),
(int)oppPt.X, (int)oppPt.Y);
g.DrawLine(blackPen, (int)oppPt.X, (int)oppPt.Y,
(int)(xMid - yDiff / 2), (int)(yMid + xDiff / 2));
g.DrawLine(blackPen, (int)(xMid - yDiff / 2),
(int)(yMid + xDiff / 2), (int)keyPt.X, (int)keyPt.Y);
but i can't figure out how to draw the circle and the triangle the same way
Please advise, thanks
回答1:
On Same Way.
int left = 20, top = 20
int right = 100, bot = 100;
// triangle
g.DrawLine(Pens.Red, left, bot, right, bot);
g.DrawLine(Pens.Red, right, bot, left + (right-left) / 2 /* was miss calc */, top);
g.DrawLine(Pens.Red, left + (right - left) / 2, top, left, bot); // better looks
//g.DrawLine(Pens.Red, left, bot, left + (right-left) / 2 /* was miss calc */, top);
// circle
int len = (right - left) / 2;
int centerX = left + (right - left) / 2, centerY = top + (bot - top) / 2;
for (int i = 0; i <= 360; i++)
{
int degrees = i;
double radians = degrees * (Math.PI / 180);
int x = centerX + (int)(len * Math.Cos(radians));
int y = centerY + (int)(len * Math.Sin(radians));
e.Graphics.FillRectangle(Brushes.Red, x, y, 1, 1); // single pixel drawing
}
But Ellipse is more difficult
http://www.petercollingridge.co.uk/tutorials/computational-geometry/finding-angle-around-ellipse/
Upper link is help for Ellipse
回答2:
To draw a circle, use g.DrawEllipse
, here is a nice extension method for it:
public static void DrawCircle(Graphics g, Pen pen, float centerX, float centerY, float radius)
{
g.DrawEllipse(pen, centerX - radius, centerY - radius, radius + radius, radius + radius);
}
To draw filled, simply change DrawEllipse
to FillElipse
and Pen
to Brush
:
public static void DrawCircleFilled(Graphics g, Brush brush, float centerX, float centerY, float radius)
{
g.FillEllipse(brush, centerX - radius, centerY - radius, radius + radius, radius + radius);
}
If you insist on drawing shapes only with lines, then fear not. A trinagle is simply drawn with three lines, that's fairly easy so I won't write that here. As for a circle:
Circle is made out of a several segments, the more segments, the better the quality of a circle. You can start with a simple triangle, then add one more line and you have a square, then pentagon, hexagon and so one. With each line the shapes resembles more of a circle optically.
As you can see, each line covers (360 / N) of the total angle. So you can use a for cycle to draw all segments (lines).
回答3:
If you specify with two points((x1,y1),(x2,y2)) the diameter of the circle,
then use DrawEllipse
with x,y,w,h
x = cx-r
y = cy-r
d = w = h = sqrt((x2-x1)^2+(y2-y1)^2)
where
cx = |x2-x1|/2
cy = |y2-y1|/2
r = d/2
For triangle you really need three points though. You could restrict it to two points as in specifying the hypotenuse of a right triangle. That still isn't enough information though because there are two right triangles that could have that hypotenuse, the upper and lower side. What kind of triangle do you want? right, iso, acute, equilateral, obtuse?
来源:https://stackoverflow.com/questions/54652668/c-sharp-drawing-library-calculate-ranges-and-midpoints-to-draw-and-circle