Just learned to program, wrote an algorithm to determine the relationship between independent point and polygon position (C #)

Source: Internet
Author: User
Tags bool float max min reset
Programming | algorithm #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;//Vertex count

public bool reset=false;//set reset function mark

Private struct point//vertex structure
{
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)//Judgment function
{
float Px = float. Parse (TextBox1.Text);
float Py = float. Parse (TextBox2.Text);
Float[] line = new float[99];
float Linelast;
Float flag = 1;//To determine the number of flags in line or so (greater than 0 on the right, less than 0 in the left)
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
}

}


}

Reset function
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 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 this vertexes are ordinal\nand the Polyg ' On ' right side of the ' Line\nfrom PREVIOUS vertex to current vertex.\nor else, there must as Error!\n\nplease mber! Good Luck!\n\n\npoint & Polygon v1.0 <<iceboy 2005.1.2>> ",
"attention!", messageboxbuttons.ok,messageboxicon.information);
}
}
}




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.