【算法】点在多边形内外判断

射线发

 /// 射线发判断点是否在多边形内部
        /// 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;
            }

        }

    }
}

 

posted on 2016-07-12 17:12  markygis  阅读(545)  评论(0编辑  收藏  举报