如何使用.Net绘制多边形并计算其面积 [英] how to draw polygon and calculate its area using .Net
问题描述
如何使用鼠标绘制多边形然后使用asp.net计算其面积。
好的,这实际上并不困难,但你需要想一想,做几件事。
如果我假设你在面板上画画,那么你需要设置一个Point对象列表:
private 列表< Point> polygon = null ;
这将在你创建它时保存多边形顶点。
然后你需要处理Panel.MouseClick事件:
< pre lang =c#> private void myPolygonPanel_MouseClick( object sender,MouseEventArgs e)
{
if (polygon == null )
{
polygon = new List< Point>();
}
polygon.Add(e.Location);
myPolygonPanel.Invalidate();
}
以及Paint事件:
private void myPolygonPanel_Paint( object sender,PaintEventArgs e)
{
if (polygon!= null && polygon.Count > ; 1 )
{
for ( int i = 0 ; i < polygon.Count - 1 ; i ++)
{
e.Graphics.DrawLine(Pens.Red,polygon [i],polygon [i + 1 跨度>]);
}
}
}
计算面积是另一回事:这是一个简单的Brute-Force-Ignorance方法,由C ++转换而来/>
double GetArea(List< Point> polygon)
{
double area = 0 。 0 ;
for ( int i = 0 ; i < polygon.Count; i ++)
{
int j =(i + 1 )%polygon.Count;
area + = polygon [i] .X * polygon [j] .Y;
area - = polygon [i] .Y * polygon [j] .X;
}
area / = 2 ;
if (area < 0 )
{
area = -area;
}
返回区域;
}
你可能想要更聪明一点! Wiki可能对您有所帮助。
How to draw a polygon using mouse then after calculate its area using asp.net.
Ok, this isn''t actually difficult, but you need to think about it, and do a few things.
If I assume you draw on a panel, then you need to set up a list of Point objects:
private List<Point> polygon = null;This will hold the polygon vertices as you create it.
You then need to handle the Panel.MouseClick event:
private void myPolygonPanel_MouseClick(object sender, MouseEventArgs e) { if (polygon == null) { polygon = new List<Point>(); } polygon.Add(e.Location); myPolygonPanel.Invalidate(); }And the Paint event:
private void myPolygonPanel_Paint(object sender, PaintEventArgs e) { if (polygon != null && polygon.Count > 1) { for (int i = 0; i < polygon.Count - 1; i++) { e.Graphics.DrawLine(Pens.Red, polygon[i], polygon[i + 1]); } } }Calculating the area is another matter: this is a simple Brute-Force-and-Ignorance method converted from C++
double GetArea(List<Point> polygon) { double area = 0.0; for (int i = 0; i < polygon.Count; i++) { int j = (i + 1) % polygon.Count; area += polygon[i].X * polygon[j].Y; area -= polygon[i].Y * polygon[j].X; } area /= 2; if (area < 0) { area = -area; } return area; }You may want to be a little more intelligent! Wiki can probably help you.
这篇关于如何使用.Net绘制多边形并计算其面积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!