c#绘图库计算绘制和圆形的范围和中点 [英] c# drawing library calculate ranges and midpoints to draw and circle
本文介绍了c#绘图库计算绘制和圆形的范围和中点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
基本上我有一个菜单,用户选择要绘制的形状,然后用户单击两点,在这两点之间将绘制所选形状.
我做了这样计算的Square
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
请指教,谢谢
推荐答案
On Same Way.
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
}
但椭圆更难
http://www.petercollingridge.co.uk/tutorials/computational-geometry/finding-angle-around-ellipse/
上链接是 Ellipse 的帮助
Upper link is help for Ellipse
这篇关于c#绘图库计算绘制和圆形的范围和中点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文