Jump to content
Costinbos77

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

Recommended Posts

Costinbos77

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

Share this post


Link to post
Share on other sites
cwake

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.

Share this post


Link to post
Share on other sites
Lee Mac

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

Share this post


Link to post
Share on other sites
Costinbos77

Thanks guys, it seems to be a common problem.

Share this post


Link to post
Share on other sites
GP_

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.

Share this post


Link to post
Share on other sites
Costinbos77

Thanks GP_ look good.

Share this post


Link to post
Share on other sites
juanragal

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... 

Share this post


Link to post
Share on other sites
Costinbos77
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)

Share this post


Link to post
Share on other sites
juanragal

You are right...

 

Thank you Costinbos77

 

 

Share this post


Link to post
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
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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...