国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

剛學編程,寫了個判斷獨立點與多邊形位置關系的算法(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);
}
}
}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 海兴县| 南漳县| 香格里拉县| 通城县| 吴川市| 武威市| 舞阳县| 台中市| 侯马市| 封丘县| 基隆市| 广河县| 广西| 牙克石市| 沈阳市| 三原县| 衡山县| 颍上县| 珲春市| 庆云县| 甘泉县| 长海县| 资兴市| 华阴市| 柳河县| 南澳县| 凤山县| 宜川县| 昆明市| 吴旗县| 荆州市| 屏山县| 金川县| 荣成市| 顺昌县| 阳泉市| 虞城县| 盐源县| 炎陵县| 泽普县| 渑池县|