<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Testing to see if a Point is within a Polygon</title>
	<atom:link href="http://www.developingfor.net/c-20/testing-to-see-if-a-point-is-within-a-polygon.html/feed" rel="self" type="application/rss+xml" />
	<link>http://www.developingfor.net/c-20/testing-to-see-if-a-point-is-within-a-polygon.html</link>
	<description>Real World .NET Methods, Tricks, and Examples</description>
	<lastBuildDate>Thu, 11 Mar 2010 18:24:27 -0500</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Antonio</title>
		<link>http://www.developingfor.net/c-20/testing-to-see-if-a-point-is-within-a-polygon.html/comment-page-1#comment-5856</link>
		<dc:creator>Antonio</dc:creator>
		<pubDate>Wed, 15 Apr 2009 16:36:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.developingfor.net/net-20/testing-to-see-if-a-point-is-within-a-polygon.html#comment-5856</guid>
		<description>Thank u very much!</description>
		<content:encoded><![CDATA[<p>Thank u very much!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: phil</title>
		<link>http://www.developingfor.net/c-20/testing-to-see-if-a-point-is-within-a-polygon.html/comment-page-1#comment-2632</link>
		<dc:creator>phil</dc:creator>
		<pubDate>Fri, 12 Dec 2008 01:21:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.developingfor.net/net-20/testing-to-see-if-a-point-is-within-a-polygon.html#comment-2632</guid>
		<description>Thanks!</description>
		<content:encoded><![CDATA[<p>Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: batterfly</title>
		<link>http://www.developingfor.net/c-20/testing-to-see-if-a-point-is-within-a-polygon.html/comment-page-1#comment-137</link>
		<dc:creator>batterfly</dc:creator>
		<pubDate>Mon, 30 Jun 2008 03:13:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.developingfor.net/net-20/testing-to-see-if-a-point-is-within-a-polygon.html#comment-137</guid>
		<description>this is my code ,can you help me to correct it ,or add your methods to it let it gain right result ?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Drawing.Drawing2D;
using System.Collections.Generic;


namespace LineAndPolygon
{
    public partial class Form1 : Form
    {
        private Point lastPoint = new Point(-1, -1);      
        private List alPoints; //save all vertex of polygon 
        private IEnumerator enumerator;
        private Region areaRegion = null;
        private GraphicsPath areaPath = null;
        //test if pp is in the polygon
        Point[] pp = { new Point(81, 24), new Point(200, 158) };
        //m_Bitmap1 store the opened image info
        Bitmap m_Bitmap1 = new Bitmap(2, 2);
        
        public Form1()
        {
            InitializeComponent();
            alPoints = new  List();
             
        }

         protected  GraphicsPath AreaPath
         {
             get
             {
                 return areaPath;
             }
             set 
             {
                 areaPath = value;
             }

         }

        protected Region AreaRegion
        {
            get
            {
                return areaRegion;
            }
            set
            {
                areaRegion = value;
            }
        }
/// 
        /// 判断点是否在多边形内
        /// 
        /// 
        /// 
        private bool PointInObject(Point point)
        {
            return AreaRegion.IsVisible(point); 
        }
 private void CreateObject()
        {
            if (AreaPath != null)
                return;
            AreaPath = new GraphicsPath();
            int x1 = 0, y1 = 0;
            int x2, y2;
            enumerator = alPoints.GetEnumerator();
            if (enumerator.MoveNext()) //point to the first value
            {
                x1 = ((Point)enumerator.Current).X;
                y1 = ((Point)enumerator.Current).Y;
            }
            while(enumerator.MoveNext())
            {
                x2 = ((Point)enumerator.Current).X;
                y2 = ((Point)enumerator.Current).Y;
                AreaPath.AddLine(x1, y1, x2, y2);
                x1 = x2;
                y1 = y2;
            }
            AreaPath.CloseFigure();
            AreaRegion = new Region(AreaPath);
        }
        
        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
           
           Graphics g = panel1.CreateGraphics();  
            //Random rand = new Random();
            //int rc = rand.Next(255);
            //int gc = rand.Next(255);
            //int bc = rand.Next(255);

            if (e.Button == MouseButtons.Left &amp;&amp;　this.radioButton1.Checked)
            {
                g.FillRectangle(new SolidBrush(Color.Black), e.X, e.Y, 5, 5);

                if (lastPoint.X != -1 &amp;&amp; lastPoint.Y != -1)
                {
                    g.DrawLine(new Pen(new SolidBrush(Color.Red)),
                        lastPoint, new Point(e.X, e.Y));
                }

                lastPoint.X = e.X;
                lastPoint.Y = e.Y;
                alPoints.Add(lastPoint);
            }
            else if (e.Button == MouseButtons.Right &amp;&amp; this.radioButton1.Checked)
            {
               
                Point[] points = alPoints.ToArray();
                //if (rand.Next(1) == 0)
                //{
                    g.DrawPolygon(new Pen(new SolidBrush(
                        Color.FromArgb(0, 255, 0))), points);
                //}
                //else
                //{
                //    g.FillPolygon(new SolidBrush(Color.FromArgb(rc, gc, bc)),points);
                   
                //}
               // alPoints.Clear();
                lastPoint.X = -1;
                lastPoint.Y = -1;
               CreateObject();
                testPoint(pp);
                alPoints.Clear();
            }
         //   g.Dispose();
        }
public void testPoint(Point[] pp)
        {
            ////i :图像的行，j： 图像列，则有pp.y=i,pp.x=j; pp.length=i*img.width+j;
            
            for (int k = 0; k &lt; pp.Length; k++)
            {
                if (PointInObject(pp[k]))
                {
                   
                    MessageBox.Show(&quot; pp[&quot;+k.ToString()+&quot;] is in the  polygon&quot;);
                    label1.Text = Convert.ToString(k);

                }
               else
                {
                    MessageBox.Show(&quot; pp[&quot; + k.ToString() + &quot;] is not in the polygon&quot;);
                    label1.Text = &quot;none point in&quot;;
                }

            }
            label1.Text = &quot;&quot;;
        }
private void button1_Click(object sender, EventArgs e)
        {
            
            openFileDialog1.Filter = &quot;Bitmap文件(*.bmp)&#124;*.bmp&#124; Jpeg文件(*.jpg)&#124;*.jpg&#124; 所有合适文件(*.bmp/*.jpg)&#124;*.bmp/*.jpg&quot;; 
             if(DialogResult.OK == openFileDialog1.ShowDialog())
             {
                 m_Bitmap1 = (Bitmap)Bitmap.FromFile(openFileDialog1.FileName, false);
             }

            ////获得图像个分量的像素值
             for (int k = 0; k &lt; m_Bitmap1.Width * m_Bitmap1.Height; k++)
             {
                // for (int i = 0; i &lt; m_Bitmap1.Height; i++)
                // { 
                //   for (int j = 0; j &lt; m_Bitmap1.Width; j++)
                //   {
                //       positonPoint[k] = new Point(i, j);
                //       pixelValue = m_Bitmap1.GetPixel(i, j);
                //       rValue = pixelValue.R;
                //       gValue = pixelValue.G;
                //       bValue = pixelValue.B;
                //   }

                // }
            }
           
             
              //panel1.Invalidate();
             //panel1.Refresh();
            Graphics g = panel1.CreateGraphics();
            g.DrawImage(m_Bitmap1, new Point(0, 0));
            //g.Dispose();
        }</description>
		<content:encoded><![CDATA[<p>this is my code ,can you help me to correct it ,or add your methods to it let it gain right result ?<br />
using System;<br />
using System.Collections.Generic;<br />
using System.ComponentModel;<br />
using System.Data;<br />
using System.Drawing;<br />
using System.Text;<br />
using System.Windows.Forms;<br />
using System.Collections;<br />
using System.Drawing.Drawing2D;<br />
using System.Collections.Generic;</p>
<p>namespace LineAndPolygon<br />
{<br />
    public partial class Form1 : Form<br />
    {<br />
        private Point lastPoint = new Point(-1, -1);<br />
        private List alPoints; //save all vertex of polygon<br />
        private IEnumerator enumerator;<br />
        private Region areaRegion = null;<br />
        private GraphicsPath areaPath = null;<br />
        //test if pp is in the polygon<br />
        Point[] pp = { new Point(81, 24), new Point(200, 158) };<br />
        //m_Bitmap1 store the opened image info<br />
        Bitmap m_Bitmap1 = new Bitmap(2, 2);</p>
<p>        public Form1()<br />
        {<br />
            InitializeComponent();<br />
            alPoints = new  List();</p>
<p>        }</p>
<p>         protected  GraphicsPath AreaPath<br />
         {<br />
             get<br />
             {<br />
                 return areaPath;<br />
             }<br />
             set<br />
             {<br />
                 areaPath = value;<br />
             }</p>
<p>         }</p>
<p>        protected Region AreaRegion<br />
        {<br />
            get<br />
            {<br />
                return areaRegion;<br />
            }<br />
            set<br />
            {<br />
                areaRegion = value;<br />
            }<br />
        }<br />
///<br />
        /// 判断点是否在多边形内<br />
        ///<br />
        ///<br />
        ///<br />
        private bool PointInObject(Point point)<br />
        {<br />
            return AreaRegion.IsVisible(point);<br />
        }<br />
 private void CreateObject()<br />
        {<br />
            if (AreaPath != null)<br />
                return;<br />
            AreaPath = new GraphicsPath();<br />
            int x1 = 0, y1 = 0;<br />
            int x2, y2;<br />
            enumerator = alPoints.GetEnumerator();<br />
            if (enumerator.MoveNext()) //point to the first value<br />
            {<br />
                x1 = ((Point)enumerator.Current).X;<br />
                y1 = ((Point)enumerator.Current).Y;<br />
            }<br />
            while(enumerator.MoveNext())<br />
            {<br />
                x2 = ((Point)enumerator.Current).X;<br />
                y2 = ((Point)enumerator.Current).Y;<br />
                AreaPath.AddLine(x1, y1, x2, y2);<br />
                x1 = x2;<br />
                y1 = y2;<br />
            }<br />
            AreaPath.CloseFigure();<br />
            AreaRegion = new Region(AreaPath);<br />
        }</p>
<p>        private void panel1_MouseDown(object sender, MouseEventArgs e)<br />
        {</p>
<p>           Graphics g = panel1.CreateGraphics();<br />
            //Random rand = new Random();<br />
            //int rc = rand.Next(255);<br />
            //int gc = rand.Next(255);<br />
            //int bc = rand.Next(255);</p>
<p>            if (e.Button == MouseButtons.Left &amp;&amp;　this.radioButton1.Checked)<br />
            {<br />
                g.FillRectangle(new SolidBrush(Color.Black), e.X, e.Y, 5, 5);</p>
<p>                if (lastPoint.X != -1 &amp;&amp; lastPoint.Y != -1)<br />
                {<br />
                    g.DrawLine(new Pen(new SolidBrush(Color.Red)),<br />
                        lastPoint, new Point(e.X, e.Y));<br />
                }</p>
<p>                lastPoint.X = e.X;<br />
                lastPoint.Y = e.Y;<br />
                alPoints.Add(lastPoint);<br />
            }<br />
            else if (e.Button == MouseButtons.Right &amp;&amp; this.radioButton1.Checked)<br />
            {</p>
<p>                Point[] points = alPoints.ToArray();<br />
                //if (rand.Next(1) == 0)<br />
                //{<br />
                    g.DrawPolygon(new Pen(new SolidBrush(<br />
                        Color.FromArgb(0, 255, 0))), points);<br />
                //}<br />
                //else<br />
                //{<br />
                //    g.FillPolygon(new SolidBrush(Color.FromArgb(rc, gc, bc)),points);</p>
<p>                //}<br />
               // alPoints.Clear();<br />
                lastPoint.X = -1;<br />
                lastPoint.Y = -1;<br />
               CreateObject();<br />
                testPoint(pp);<br />
                alPoints.Clear();<br />
            }<br />
         //   g.Dispose();<br />
        }<br />
public void testPoint(Point[] pp)<br />
        {<br />
            ////i :图像的行，j： 图像列，则有pp.y=i,pp.x=j; pp.length=i*img.width+j;</p>
<p>            for (int k = 0; k &lt; pp.Length; k++)<br />
            {<br />
                if (PointInObject(pp[k]))<br />
                {</p>
<p>                    MessageBox.Show(&#8221; pp["+k.ToString()+"] is in the  polygon&#8221;);<br />
                    label1.Text = Convert.ToString(k);</p>
<p>                }<br />
               else<br />
                {<br />
                    MessageBox.Show(&#8221; pp[" + k.ToString() + "] is not in the polygon&#8221;);<br />
                    label1.Text = &#8220;none point in&#8221;;<br />
                }</p>
<p>            }<br />
            label1.Text = &#8220;&#8221;;<br />
        }<br />
private void button1_Click(object sender, EventArgs e)<br />
        {</p>
<p>            openFileDialog1.Filter = &#8220;Bitmap文件(*.bmp)|*.bmp| Jpeg文件(*.jpg)|*.jpg| 所有合适文件(*.bmp/*.jpg)|*.bmp/*.jpg&#8221;;<br />
             if(DialogResult.OK == openFileDialog1.ShowDialog())<br />
             {<br />
                 m_Bitmap1 = (Bitmap)Bitmap.FromFile(openFileDialog1.FileName, false);<br />
             }</p>
<p>            ////获得图像个分量的像素值<br />
             for (int k = 0; k &lt; m_Bitmap1.Width * m_Bitmap1.Height; k++)<br />
             {<br />
                // for (int i = 0; i &lt; m_Bitmap1.Height; i++)<br />
                // {<br />
                //   for (int j = 0; j &lt; m_Bitmap1.Width; j++)<br />
                //   {<br />
                //       positonPoint[k] = new Point(i, j);<br />
                //       pixelValue = m_Bitmap1.GetPixel(i, j);<br />
                //       rValue = pixelValue.R;<br />
                //       gValue = pixelValue.G;<br />
                //       bValue = pixelValue.B;<br />
                //   }</p>
<p>                // }<br />
            }</p>
<p>              //panel1.Invalidate();<br />
             //panel1.Refresh();<br />
            Graphics g = panel1.CreateGraphics();<br />
            g.DrawImage(m_Bitmap1, new Point(0, 0));<br />
            //g.Dispose();<br />
        }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joel</title>
		<link>http://www.developingfor.net/c-20/testing-to-see-if-a-point-is-within-a-polygon.html/comment-page-1#comment-125</link>
		<dc:creator>Joel</dc:creator>
		<pubDate>Fri, 27 Jun 2008 15:29:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.developingfor.net/net-20/testing-to-see-if-a-point-is-within-a-polygon.html#comment-125</guid>
		<description>I can&#039;t share the complete code as this is in live proprietary software, but I will say that using the actual PolygonF code is very simple:

PolygonF poly = new PolygonF(POINTF_ARRAY);
if (poly.Contains(SOME_POINTF))
{
...
}

And it works in our production code every time, millions of calls in a single thread execution.

This is a very complex application, and there is a lot of code prior to this that sets up the POINTF_ARRAY, taking into consideration adjustments for margins, padding, available size, centering, etc.  In the end, there are a few things that I think are critical.  First, all the points are in positive space.  Mathematically speaking, this probably shouldn&#039;t matter, but I know in positive space it always works.  Second, the points are added to the array in the correct order, ensuring that the proper shape is represented.

This is a great time to try some Unit Testing! I added a Test project to my Solution, used TestInitialize to create a PolygonF object, and then wrote a test method to test various points. Here is a simple example:

[TestInitialize()]
public void MyTestInitialize() 
{
    List&lt;PointF&gt; pointList = new List&lt;PointF&gt;()
    {
        new PointF(10, 10),
        new PointF(10, 20),
        new PointF(20, 20),
        new PointF(20, 10)
    };

    PolyF = new PolygonF(pointList.ToArray());
}

[TestMethod]
public void PolygonContainsPoint()
{
    Assert.IsTrue(PolyF.Contains(new PointF(15,15)));
}

Simply change this last point around to test within/without responses.  While this example is a simple square, this code is currently funcitoning with some fairly complex non-symmetrical polygons, up to 20 sides (a limit of our application, not this class).</description>
		<content:encoded><![CDATA[<p>I can&#8217;t share the complete code as this is in live proprietary software, but I will say that using the actual PolygonF code is very simple:</p>
<p>PolygonF poly = new PolygonF(POINTF_ARRAY);<br />
if (poly.Contains(SOME_POINTF))<br />
{<br />
&#8230;<br />
}</p>
<p>And it works in our production code every time, millions of calls in a single thread execution.</p>
<p>This is a very complex application, and there is a lot of code prior to this that sets up the POINTF_ARRAY, taking into consideration adjustments for margins, padding, available size, centering, etc.  In the end, there are a few things that I think are critical.  First, all the points are in positive space.  Mathematically speaking, this probably shouldn&#8217;t matter, but I know in positive space it always works.  Second, the points are added to the array in the correct order, ensuring that the proper shape is represented.</p>
<p>This is a great time to try some Unit Testing! I added a Test project to my Solution, used TestInitialize to create a PolygonF object, and then wrote a test method to test various points. Here is a simple example:</p>
<p>[TestInitialize()]<br />
public void MyTestInitialize()<br />
{<br />
    List
<pointf> pointList = new List</pointf>
<pointf>()<br />
    {<br />
        new PointF(10, 10),<br />
        new PointF(10, 20),<br />
        new PointF(20, 20),<br />
        new PointF(20, 10)<br />
    };</p>
<p>    PolyF = new PolygonF(pointList.ToArray());<br />
}</p>
<p>[TestMethod]<br />
public void PolygonContainsPoint()<br />
{<br />
    Assert.IsTrue(PolyF.Contains(new PointF(15,15)));<br />
}</p>
<p>Simply change this last point around to test within/without responses.  While this example is a simple square, this code is currently funcitoning with some fairly complex non-symmetrical polygons, up to 20 sides (a limit of our application, not this class).</pointf>
]]></content:encoded>
	</item>
	<item>
		<title>By: batterfly</title>
		<link>http://www.developingfor.net/c-20/testing-to-see-if-a-point-is-within-a-polygon.html/comment-page-1#comment-119</link>
		<dc:creator>batterfly</dc:creator>
		<pubDate>Fri, 27 Jun 2008 04:08:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.developingfor.net/net-20/testing-to-see-if-a-point-is-within-a-polygon.html#comment-119</guid>
		<description>hi ,
i wrote a code about drawing polygon ,and judge some points if there are in or not in the polygon using c# ,but the result is very different , some times the judge is right ,some times it is wrong to the same situation ,i don&#039;t kown why . and i need your help,ple leave a email ,so that i can send it to you.
thank you</description>
		<content:encoded><![CDATA[<p>hi ,<br />
i wrote a code about drawing polygon ,and judge some points if there are in or not in the polygon using c# ,but the result is very different , some times the judge is right ,some times it is wrong to the same situation ,i don&#8217;t kown why . and i need your help,ple leave a email ,so that i can send it to you.<br />
thank you</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: batterfly</title>
		<link>http://www.developingfor.net/c-20/testing-to-see-if-a-point-is-within-a-polygon.html/comment-page-1#comment-118</link>
		<dc:creator>batterfly</dc:creator>
		<pubDate>Fri, 27 Jun 2008 01:48:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.developingfor.net/net-20/testing-to-see-if-a-point-is-within-a-polygon.html#comment-118</guid>
		<description>thank you very much , but i have a requestion about if i can have your whole demo about this project?</description>
		<content:encoded><![CDATA[<p>thank you very much , but i have a requestion about if i can have your whole demo about this project?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joel</title>
		<link>http://www.developingfor.net/c-20/testing-to-see-if-a-point-is-within-a-polygon.html/comment-page-1#comment-68</link>
		<dc:creator>Joel</dc:creator>
		<pubDate>Mon, 09 Jun 2008 14:12:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.developingfor.net/net-20/testing-to-see-if-a-point-is-within-a-polygon.html#comment-68</guid>
		<description>I&#039;m glad you found it useful, and on the one year anniversary of that post too!</description>
		<content:encoded><![CDATA[<p>I&#8217;m glad you found it useful, and on the one year anniversary of that post too!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: gru</title>
		<link>http://www.developingfor.net/c-20/testing-to-see-if-a-point-is-within-a-polygon.html/comment-page-1#comment-67</link>
		<dc:creator>gru</dc:creator>
		<pubDate>Sat, 07 Jun 2008 13:51:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.developingfor.net/net-20/testing-to-see-if-a-point-is-within-a-polygon.html#comment-67</guid>
		<description>thank You very much, that&#039;s what i needed :)</description>
		<content:encoded><![CDATA[<p>thank You very much, that&#8217;s what i needed <img src='http://www.developingfor.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
</channel>
</rss>
