.NET

Reply
Distinguished Contributor
VB_Autocad_guy
Posts: 136
Registered: ‎07-24-2009
Message 1 of 10 (1,279 Views)

Is Point inside Polygon or Block's Area?

1279 Views, 9 Replies
09-20-2011 10:43 AM

Anyone come across a good method or the algebraic formula for a funtion to tell me if a point is inside a polygon or block's area? 

 

Or if a two blocks or polygons are overlapping area? 

 

 

*Expert Elite*
Alfred.NESWADBA
Posts: 8,880
Registered: ‎06-29-2007
Message 2 of 10 (1,272 Views)

Betreff: Is Point inside Polygon or Block's Area?

09-20-2011 11:06 AM in reply to: VB_Autocad_guy

Hi,

 

if you can convert Polylines to MPOLYGON-objects you do have the calculation of isPointInside as a function of the object itself.

 

Public Overridable Function IsPointInsideMPolygon(worldPoint As Autodesk.AutoCAD.Geometry.Point3d, tolerance As Double) As Autodesk.AutoCAD.Geometry.IntegerCollection
     Member von Autodesk.AutoCAD.DatabaseServices.MPolygon

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Active Contributor
HomeBoyLV
Posts: 47
Registered: ‎06-04-2004
Message 3 of 10 (1,248 Views)

Re: Is Point inside Polygon or Block's Area?

09-20-2011 03:43 PM in reply to: VB_Autocad_guy
Member
l.skoropinski
Posts: 5
Registered: ‎09-09-2011
Message 4 of 10 (1,228 Views)

Re: Is Point inside Polygon or Block's Area?

09-21-2011 01:34 AM in reply to: VB_Autocad_guy

Found a formula, link listed below and produced this, appears to work, but you may want to test to ensure it suits your needs.

 

//Point inside a polyline came from the Solution 2 (2d) section of this website http://paulbourke.net/geometry/insidepoly/

        /// <summary>
        /// Check if the point is within the polyline
        /// </summary>
        /// <param name="polygon"></param>
        /// <param name="pt"></param>
        /// <returns></returns>
        public static bool InsidePolygon(Polyline polygon, Point3d pt)
        {
            int n = polygon.NumberOfVertices;
           double angle=0;
           Point pt1 , pt2 ;

           for (int i = 0; i < n; i++)
           {
              pt1.X = polygon.GetPoint2dAt(i).X - pt.X;
              pt1.Y = polygon.GetPoint2dAt(i).Y - pt.Y;
              pt2.X = polygon.GetPoint2dAt((i+1)%n).X - pt.X;
              pt2.Y = polygon.GetPoint2dAt((i+1)%n).Y - pt.Y;
              angle += Angle2D(pt1.X,pt1.Y,pt2.X,pt2.Y);
           }

           if (Math.Abs(angle) < Math.PI)
              return false;
           else
              return true;
        }
        
        /// <summary>
        /// Point structure to add InsidePolygon function
        /// </summary>
        public struct Point
        {
            public double X, Y;
        };

        /*
           
        */
        /// <summary>
        /// Return the angle between two vectors on a plane
        /// The angle is from vector 1 to vector 2, positive anticlockwise
        /// The result is between -pi -> pi
        /// </summary>
        /// <param name="x1"></param>
        /// <param name="y1"></param>
        /// <param name="x2"></param>
        /// <param name="y2"></param>
        /// <returns></returns>
        public static double Angle2D(double x1, double y1, double x2, double y2)
        {
           double dtheta,theta1,theta2;

           theta1 = Math.Atan2(y1,x1);
           theta2 = Math.Atan2(y2, x2);
           dtheta = theta2 - theta1;
           while (dtheta > Math.PI)
               dtheta -= (Math.PI * 2);
           while (dtheta < -Math.PI)
               dtheta += (Math.PI * 2);
               return(dtheta);
        }

 

 

Valued Contributor
Juergen.Becker
Posts: 53
Registered: ‎10-25-2007
Message 5 of 10 (1,221 Views)

Re: Is Point inside Polygon or Block's Area?

09-21-2011 02:34 AM in reply to: VB_Autocad_guy

Hi,

what about this:

            public bool IsPointInPolyLine(Polyline Polyline, Point3d Point)

            {

                bool hIsInPLine = false;

                Point3d hPoint1 = Point;

                Point3d hPoint2 = PolarPoints(hPoint1, 0, 1);

 

                Vector3d hVector = hPoint2 - hPoint1;

                Ray hRay = new Ray();

           

                hRay.BasePoint = new Point3d(Point.X, Point.Y, 0);

                hRay.UnitDir = hVector;

 

                Point3dCollection hIntersectionPoints =

                        new Point3dCollection();

                Autodesk.AutoCAD.DatabaseServices.PlatformCompatibilityExtensionMethods.IntersectWith

                    (hRay, Polyline, Intersect.OnBothOperands, hIntersectionPoints);

                //hRay.IntersectWith(

                //  Polyline,

                //  Intersect.OnBothOperands,

                //  hIntersectionPoints, 0, 0);

 

                int hMod = hIntersectionPoints.Count % 2;

                if (hMod == 0)

                {

                    hIsInPLine = false;

                }

                else

                {

                    hIsInPLine = true;

                }

                return hIsInPLine;

 

            }

 

This method use a Ray to get the intersection with a polyline.

You get a error-message when yo use a 64 bit Windows but there is a workaround for that problem.

 

Regards Jürgen

*Expert Elite*
Alfred.NESWADBA
Posts: 8,880
Registered: ‎06-29-2007
Message 6 of 10 (1,213 Views)

Re: Is Point inside Polygon or Block's Area?

09-21-2011 03:45 AM in reply to: Juergen.Becker

Hi,

 

Attention! Both versions do have problems you should be aware of:

 

a) the version that summarizes the angles does only work on polylines with no arcs in it, that are not curved and not splined.

 

b) the version using ray and count the intersections will have two critical situations

  • it the ray crosses the poly very near to a vertex, it sometimes returns 2 intersectionpoints instead of 1, because AutoCAD does extend a poly-segment a little bit (even not told to extend any object for intersection-calulation) look at this screenshot:

        

  • Second situation is a (primitive/bad) version of creating islands with POLYLINES. The linework goes through the outline, than from one vertex to the inner border (island), around the island and back to the outer border. In this case you see a ray crossing two segments, one from outline to island and one from island to outline. So trying to get the intersectionpoints between the POLYLINE and the RAY should get 2 points ... but AutoCAD returns a collection (Point3DCollection) and within a collection double points are not allowed ... so you get at this point only one intersection back ==> and the result is wrong.

          

 

Important: I don't want to critism the above suggestions, I just want to inform about situations you should be prepared before start development.

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Member
l.skoropinski
Posts: 5
Registered: ‎09-09-2011
Message 7 of 10 (1,199 Views)

Re: Is Point inside Polygon or Block's Area?

09-21-2011 05:23 AM in reply to: Alfred.NESWADBA

Good point, my implementation is timber components which never have curves, so works well for me.

Distinguished Contributor
VB_Autocad_guy
Posts: 136
Registered: ‎07-24-2009
Message 8 of 10 (1,172 Views)

Re: Is Point inside Polygon or Block's Area?

09-21-2011 11:21 AM in reply to: VB_Autocad_guy

Thank you everyone for the help! Thanks again. 

 

Cheers!

*Expert Elite*
chiefbraincloud
Posts: 752
Registered: ‎02-13-2008
Message 9 of 10 (1,169 Views)

Re: Is Point inside Polygon or Block's Area?

09-21-2011 11:43 AM in reply to: Alfred.NESWADBA

Hi Alfred,  FYI Point3dCollections do not require the contents to be distinct.  There are several functions in my code where I am taking the points of a polyline that is required to be a closed loop, in order to standardize the way the polylines were drawn, I test distance from the last point to the first, and if it is not 0 then I add the first point in the collection onto the end of the collection so that the first and last points in the collection are equal.

 

If your intersection example with the bad return is something you have experienced, then there is another reason for it which I am not sure of.  

Dave O.                                                                  Sig-Logos32.png
*Expert Elite*
Alfred.NESWADBA
Posts: 8,880
Registered: ‎06-29-2007
Message 10 of 10 (1,165 Views)

Re: Is Point inside Polygon or Block's Area?

09-21-2011 11:52 AM in reply to: chiefbraincloud

Hi chiefbraincloud,

 

>> Point3dCollections do not require the contents to be distinct

Maybe, I have not tested it creating it by myself (as I'm using my own type of PointLists if I need it).

However, AutoCAD does not return 2 Intersections in the above sample (where the ray crosses two segments on the same point). And if I remember right, there are more Curve-functions also avoiding double/same points (neighboring) in the return collection.

For details I would have to search now, sorry to have no time at the moment, next time I get an example I hope to remember to this thread!

 

- alfred -

 

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community