首页 > 开发 > 综合 > 正文

刚学编程,写了个判断独立点与多边形位置关系的算法(C#)

2024-07-21 02:18:24
字体:
来源:转载
供稿:网友

商业源码热门下载www.html.org.cn

#region using directives

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.windows.forms;

#endregion

namespace p_polygon
{
partial class form1 : form
{
public form1()
{
initializecomponent();
}

public int count = 0;//顶点计数

public bool reset=false;//设复位功能标志

private struct point//顶点结构
{
public float x, y;
}

point[] pt = new point[100];

private float max(float x,float y)
{
return(x>y?x:y);
}

private float min(float x, float y)
{
return (x < y ? x : y);
}

private void judge(point[] pt)//判断函数
{
float px = float.parse(textbox1.text);
float py = float.parse(textbox2.text);
float[] line = new float[99];
float linelast;
float flag = 1;//判断在直线左右的标志数(大于0在右,小于0在左)
bool online = false;
for (int i = 1; i < count; i++)
{
line[i - 1] = (pt[i].y - pt[i - 1].y) * px + (pt[i - 1].x - pt[i].x) * py +
(pt[i].x - pt[i - 1].x) * pt[i - 1].y - (pt[i].y - pt[i - 1].y) * pt[i - 1].x;
if (line[i - 1] == 0 && px <= max(pt[i].x, pt[i - 1].x) && px >= min(pt[i].x, pt[i - 1].x))
online = true;
flag *= line[i - 1];
if (i == count - 1)
{
linelast = (pt[0].y - pt[i].y) * px + (pt[i].x - pt[0].x) * py +
(pt[0].x - pt[i].x) * pt[i].y - (pt[0].y - pt[i].y) * pt[i].x;
flag *= linelast;
if (flag == 0)
{
if (px <= max(pt[i].x, pt[0].x) && py >= min(pt[i].x, pt[0].x))
online = true;
else
online = false;
}
}

if ((flag < 0 && online == false) || (flag == 0 && online == false))
{
messagebox.show("the isolated point is outside the polygon");
break;
}
else if (flag == 0 && i == count - 1 && online == true)
{
messagebox.show("the isolated point is on the border of the polygon");
break;
}
else if (flag > 0 && i == count - 1)
{
messagebox.show("the isolated point is inside the polygon");
break;
}

}


}

//复位函数
private void resetall()
{
textbox1.text = "";
textbox2.text = "";
textbox3.text = "";
textbox4.text = "";
count = 0;
label1.text = "0";
reset = true;
}


private void button1_click(object sender, eventargs e)
{
if (textbox3.text != "" && textbox4.text != "")
{

if (count < 100)
{
pt[count].x = float.parse(textbox3.text);
pt[count].y = float.parse(textbox4.text);
count++;
label1.text = count.tostring();
reset = false;
}
else
messagebox.show("only 100 vertexes support!");
}
else
messagebox.show("please input necessary data!");
}

private void button2_click(object sender, eventargs e)
{
if (reset == false)
{
if (textbox1.text != "" && textbox2.text != "" && textbox3.text != "" && textbox4.text != "")
judge(pt);
else
messagebox.show("please input necessary data!");
}
else
messagebox.show("all data cleared! input again.");

}

private void button3_click(object sender, eventargs e)
{
resetall();
}

private void button4_click(object sender, eventargs e)
{
messagebox.show("when you input the coordinates of vertexes,/nyou must ensure that the vertexes are ordinal/nand the polygon is at the right side of the line/nfrom previous vertex to current vertex./nor else, there must be error!/n/nplease remember! good luck!/n/n/npoint & polygon v1.0 <<iceboy 2005.1.2>>",
"attention!",messageboxbuttons.ok,messageboxicon.information);
}
}
}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表