【算法】點在多邊形內外判斷

射線發c#

 /// 射線發判斷點是否在多邊形內部
        /// p 待判斷的點。格式{x:X座標,y:Y座標}
        /// {Array} poly 多邊形定點,數組成員格式相同
        private string rayCasting(Point p, List<Point> poly)
        {
            double px = p.X;
            double py = p.Y;
            Boolean flag = false;
            for (int i = 0, l = poly.Count, j = l - 1; i < l; j = i, i++)
            {
                double sx = poly[i].X;
                double sy = poly[i].Y;
                double tx = poly[j].X;
                double ty = poly[j].Y;

                if((sx==px&&sy==py)||(tx==px&&ty==py)){
                    return "on";
                }

                //*****判斷線段兩短點是否在射線兩側
                if((sy<py&&ty>=py)||(sy>=py&&ty<py)){

                    double x = sx + (py - sy) * (tx - sx) / (ty - sy);
                    if (x == px)
                    {
                        return "on";
                    }

                    if (x > px)
                    {
                        flag = !flag;
                    }


                }
            }
            return flag ? "in" : "out";

        }

實際應用例子,GDI+ c#  數組

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics; //建立畫板,這裏的畫板是由Form提供的.
            Pen p = new Pen(Color.Blue, 2);//定義了一個藍色,寬度爲的畫筆
            g.DrawLine(p, 10, 10, 100, 10);//在畫板上畫直線,起始座標爲(10,10),終點座標爲(100,100)
            g.DrawLine(p, 100, 10, 100, 100);//在畫板上畫直線,起始座標爲(10,10),終點座標爲(100,100)
            g.DrawLine(p, 100, 100, 10, 100);//在畫板上畫直線,起始座標爲(10,10),終點座標爲(100,100)
            g.DrawLine(p, 10, 100, 50, 50);//在畫板上畫直線,起始座標爲(10,10),終點座標爲(100,100)
            g.DrawLine(p, 50, 50, 10, 10);//在畫板上畫直線,起始座標爲(10,10),終點座標爲(100,100)
           // g.DrawRectangle(p, 10, 10, 100, 100);//在畫板上畫矩形,起始座標爲(10,10),寬爲,高爲
          //  g.DrawEllipse(p, 10, 10, 100, 100);//在畫板上畫橢圓,起始座標爲(10,10),外接矩形的寬爲,高爲
            

            Point P = new Point();
            P.X = 10;
            P.Y = 50;
            g.DrawEllipse(p, 10, 50, 5, 5);//在畫板上畫橢圓,起始座標爲(10,10),外接矩形的寬爲,高爲
            
            List<Point> lst = new List<Point>();
            Point P1 = new Point();
            P1.X = 10;
            P1.Y = 10;
            Point P2 = new Point();
            P2.X = 100;
            P2.Y = 10;
            Point P3 = new Point();
            P3.X = 100;
            P3.Y = 100;
            Point P4 = new Point();
            P4.X = 10;
            P4.Y = 100;
            Point P5 = new Point();
            P5.X = 50;
            P5.Y = 50;
            lst.Add(P1); lst.Add(P2); lst.Add(P3); lst.Add(P4); lst.Add(P5);
            string w=rayCasting(P,lst);
            MessageBox.Show(w);
        }

        /// 射線發判斷點是否在多邊形內部
        /// p 待判斷的點。格式{x:X座標,y:Y座標}
        /// {Array} poly 多邊形定點,數組成員格式相同
        private string rayCasting(Point p, List<Point> poly)
        {
            double px = p.X;
            double py = p.Y;
            Boolean flag = false;
            for (int i = 0, l = poly.Count, j = l - 1; i < l; j = i, i++)
            {
                double sx = poly[i].X;
                double sy = poly[i].Y;
                double tx = poly[j].X;
                double ty = poly[j].Y;

                if((sx==px&&sy==py)||(tx==px&&ty==py)){
                    return "on";
                }

                //*****判斷線段兩短點是否在射線兩側
                if((sy<py&&ty>=py)||(sy>=py&&ty<py)){

                    double x = sx + (py - sy) * (tx - sx) / (ty - sy);
                    if (x == px)
                    {
                        return "on";
                    }

                    if (x > px)
                    {
                        flag = !flag;
                    }


                }
            }
            return flag ? "in" : "out";

        }
    }
    class Point
    {
        private double x;
        private double y;
        public double X
        {
            get
            {
                return x;

            }
            set
            {
                x = value;
            }
        }
        public double Y
        {
            get
            {
                return y;

            }
            set
            {
                y = value;
            }

        }

    }
}
相關文章
相關標籤/搜索