Jump to content

Check if a point is inside an area bounded by a polyline


Costinbos77

Recommended Posts

Hello!

 

Is there a mathematical method that can check if a point is inside an area bounded by a polyline?

 

Point inside.jpg

 

Graphic method by reporting point P (x, y, z) and then use ssget function to see if it is inside, is slower.

Edited by Costinbos77
Link to comment
Share on other sites

I wanted to research this one a while ago, and there is quite a lot of information out there on this question.

 

Try google searches. I quickly found a thread at the swamp which may help. http://www.theswamp.org/index.php?topic=1890.0 But I think you'd even find this on Wikipedia. :lol:

 

The most popular method seems to be send out a few rays at random angles and count the number of intersections. If none of the rays report an even number of intersections with the polygon then it has to lie inside. If I find any more links I'll post them.

Link to comment
Share on other sites

Here is a simple function implementing a ray-casting algorithm.

 

However, note that this function does not test whether the ray 'grazes' a vertex - for example, in the following image the function will incorrectly determine the point as being outside the polyline since the ray intersects the boundary an even number of times.

 

raycast.png

  • Like 1
Link to comment
Share on other sites

My offer... :)

 

 

(defun c:test ( / poly pt Lv n)
   (if (and
           (setq poly (car (entsel "\nSelect a Polygon")))
           (setq pt (getpoint "\nSelect a point"))
       )
       (progn
           (repeat (setq n (1+ (fix (vlax-curve-getEndParam poly))))
               (setq Lv (cons (vlax-curve-getPointAtParam poly (setq n (1- n))) Lv))
           )
           (alert (inside_p Lv pt))
       )
   )
)

(defun inside_p (list_vert  / out_p cross on)
   (setq out_p (list (car (getvar "extmax")) (* 1.1 (cadr (getvar "extmax")))))
   (setq cross 0)
   (mapcar
       '(lambda (a b)
            (if (or
                    (equal (angle a ) (angle  b) 1e-
                    (equal a  1e-
                )
                (setq on t)
            )
            (if (setq : (inters  out_p a b)) (setq cross (1+ cross)))
        )
       list_vert
       (cdr list_vert)
   )
   (cond
       ( on "ON" )
       (  (> (rem cross 2) 0) "INSIDE" )
       ( t "OUTSIDE" )
   )
)

 

 

472.gif

Edited by GP_
Updated code.
Link to comment
Share on other sites

  • 6 years later...

Hi everyone.

 

I does find GP's routine named Inside_p on this post, but I think it's not complet. I searched for more info on net and find complet code on other post. 

(defun inside_p (list_vert :p / out_p cross on)
        (setq out_p (list (car (getvar "extmax")) (* 1.1 (cadr (getvar "extmax")))))
        (setq cross 0)
        (mapcar
            '(lambda (a b)
                 (if (or
                         (equal (angle a :p) (angle :p b) 1e-8)
                         (equal a :p 1e-8)
                     )
                     (setq on t)
                 )
                 (if (setq :p: (inters :p out_p a b))
                     (setq cross (1+ cross))
                 )
             )
            list_vert
            (cdr list_vert)
        )
        (cond
            (on "ON")
            ((> (rem cross 2) 0) "INSIDE")
            (t "OUTSIDE")
        )
    )

It's great! but I find a error on use and I don't understand the causes... When I use your code on a USC and north orientation run correctly, but, when i change the plant view, with a Universal system coordinates too, do not work correctly and every time give me Outside... Maybe the extmax variable?

 

Apologies for my english... 

Link to comment
Share on other sites

list_vert = Global WCS ,
 and  
(getvar "extmax") = Local UCS

should be in the same Coordinates System. 

 

you need to convert them with (trans p 0 1)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...