Code Red > AutoLISP (Vanilla / Visual)

Point within a polyline... need some input

(1/1)

hendie:
I have to check whether or not a point is within a closed polyline. The polyline can be an irregular shaped polygon.
Anyone else done anything similar ? I could use a few pointers or suggestions as to how to go about it.

SomeCallMeDave:
There has been a ton of discussion on this topic (maybe here but I know on the old VBDesign)

For simple cases where the polyline doesn't intersect itself and everything thing is in a plane with no fancy USC,  one can count the number of times ray intersects the pline.  An odd number of intersections means the point is inside, even number means the point is outside.  The point falling on the polyline can impact the results too.

Here is some code that handles the simplest of the simple cases (assumes only 1 or 2 intersections possible)


--- Code: ---(defun css_isPtInside(pPl pPt / oPl oPt1 oPt2 r1 saInters retVal )
;;returns T if pt is inside or nil if it isn't
   (if (not (equal (type pPl) 'VLA-OBJECT))
       (setq oPl (vlax-ename->vla-object pPl))
       (setq oPl pPl)
   )
  (setq oPt1 (vlax-3d-point pPt)
        oPt2 (vlax-3d-point (polar pPt 1.5 1.5))
        r1   (vla-addray (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object))) opt1 opt2)
        saInters (vlax-variant-value (vla-intersectwith oPl r1 0))
   )

   (if (equal (vlax-safearray-get-u-bound saInters 1) 2)
      (setq retVal t)
      (setq retVal nil)
   )           
  (vla-delete r1)
  (vlax-release-object r1)
   retval
);defun css_isPtInside


--- End code ---

CAB:
There have been a few. 8-)

http://tinyurl.com/2ymr3k

hendie:
thanks guys, I actually carried out a search yesterday before posting but didn't come up with anything substantial... must have been using the wrong search terms
I'm sure I'll have more questions but this'll do to be going on with.
Thanks a bunch

efernal:

--- Code: ---;; this code is very usefull...

(DEFUN is_or_not_inside? (point_list point      /          i_counter
                          j_counter  return     quantity   it
                          jt         x_coord    y_coord
                         )
  (SETQ i_counter 0
        j_counter 0
        return    nil
        quantity  (LENGTH point_list)
        x_coord   (CAR point)
        y_coord   (CADR point)
  )
  (REPEAT quantity
    (SETQ j_counter (IF (= (1+ j_counter) quantity)
                      0
                      (1+ j_counter)
                    )
          it        (CADR (NTH i_counter point_list))
          jt        (CADR (NTH j_counter point_list))
    )
    (IF (OR (AND (< it y_coord) (>= jt y_coord))
            (AND (< jt y_coord) (>= it y_coord))
        )
      (IF (< (+ (CAR (NTH i_counter point_list))
                (* (/ (- y_coord it) (- jt it))
                   (- (CAR (NTH j_counter point_list))
                      (CAR (NTH i_counter point_list))
                   )
                )
             )
             x_coord
          )
        (SETQ return (NOT return))
      )
    )
    (SETQ i_counter (1+ i_counter))
  )
  return
  ;; T if point is inside polygon, nil if not...
)

;|
;; USAGE

Command:

(SETQ lista nil)

(WHILE (SETQ pt (GETPOINT "\n-> Polygon point : "))
  (SETQ lista (APPEND lista (LIST pt)))
)


((186.913 236.085 0.0) (262.733 218.305 0.0) (283.171 223.573 0.0) (202.077
140.598 0.0) (299.654 114.256 0.0) (229.108 61.5732 0.0) (404.483 41.1585 0.0)
(355.035 126.768 0.0) (390.638 110.963 0.0) (449.316 149.159 0.0) (366.244
198.549 0.0) (480.303 232.134 0.0) (368.881 242.012 0.0) (305.588 264.402 0.0)
(231.746 248.598 0.0))

Command: (SETQ PT (GETPOINT))
(325.367 174.841 0.0)

Command: (IS_OR_NOT_INSIDE? LISTA PT) -> T

|;


--- End code ---

Navigation

[0] Message Index

Go to full version