Author Topic: Point is Inside polyline (Now with Bulges)  (Read 22658 times)

0 Members and 1 Guest are viewing this topic.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Point is Inside polyline
« Reply #15 on: April 16, 2015, 09:58:22 AM »
Thank you ymg for that interesting link: http://geomalgorithms.com/a03-_inclusion.html
Here is my 'port' of the Crossing Number (ray) algorithm:
Code - Auto/Visual Lisp: [Select]
  1. ; Based on C++ code by Dan Sunday.
  2. ; http://geomalgorithms.com/a03-_inclusion.html
  3. (defun PointInsidePointList_P (pt ptLst)
  4.   (=
  5.     1
  6.     (logand
  7.       (apply
  8.         '+
  9.         (mapcar
  10.           '(lambda (ptA ptB)
  11.             (if
  12.               (and
  13.                 (or
  14.                   (and (<= (cadr ptA) (cadr pt)) (> (cadr ptB) (cadr pt))) ; Upward crossing.
  15.                   (and (> (cadr ptA) (cadr pt)) (<= (cadr ptB) (cadr pt))) ; Downward crossing.
  16.                 )
  17.                 (<
  18.                   (car pt)
  19.                   (+
  20.                     (car ptA)
  21.                     (*
  22.                       (- (cadr pt) (cadr ptA))
  23.                       (/ (- (car ptB) (car ptA)) (- (cadr ptB) (cadr ptA))) ; DeltaX/DeltaY edge.
  24.                     )
  25.                   )
  26.                 )
  27.               )
  28.               1
  29.               0
  30.             )
  31.           )
  32.           ptLst
  33.           (append (cdr ptLst) (list (car ptLst)))
  34.         )
  35.       )
  36.       1
  37.     )
  38.   )
  39. )
« Last Edit: April 16, 2015, 10:01:28 AM by roy_043 »

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Point is Inside polyline
« Reply #16 on: April 16, 2015, 11:27:23 AM »
FWIW .. Here are some benchmarks (Using MP's benchmark code) for YMG, ROY & Vovka's code. I modified YMG's code to accept a point list like the other two.

Quote
3668 Points
Benchmarking .........Elapsed milliseconds / relative speed for 64 iteration(s):


    (PTINPOLY_P P L).................1422 / 1.60 <fastest>
    (POINTINSIDEPOINTLIST_P P L).....2031 / 1.12
    (VK_ISPOINTINSIDE P L)...........2282 / 1.00 <slowest>



36639 Points
Benchmarking ........Elapsed milliseconds / relative speed for 32 iteration(s):


    (PTINPOLY_P P L).................1500 / 1.65 <fastest>
    (POINTINSIDEPOINTLIST_P P L).....2469 / 1.00 <slowest>
    (vk_ispointinside p l) ...Hard error occurred *** internal stack limit reached (simulated)




« Last Edit: April 16, 2015, 11:33:03 AM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Andrea

  • Water Moccasin
  • Posts: 2372
Re: Point is Inside polyline
« Reply #17 on: April 16, 2015, 11:50:04 AM »
my contribution...
not the Quicker way or smallest  code...
but it work when the polygon is far away from 0,0.    :)

Code: [Select]
(defun Isinside (pt ent / pt1 pt2 $_object_1 $_object_2 $_object_3 #gr #pti Inresult vlazone)
  (if (>
(distance '(0 0 0) (cdr (assoc 10 (entget ent))))
9000000.00
      )
    (setq ofstval 0.8)
    (setq ofstval 0.001)
  )

  (if (and pt ent)
    (progn
      (setq vlazone (vlax-ename->vla-object ent))
      (setq $_object_1 (vlax-ename->vla-object ent))
      (setq $_object_2 (car (vlax-invoke $_object_1 'Offset ofstval))
    $_object_3 (car (vlax-invoke $_object_1 'Offset (- ofstval)))
      )
      (if (> (vla-get-area $_object_2) (vla-get-area $_object_3))
(progn
  (set '#gr $_object_2)
  (set '#pti $_object_3)
)
(progn
  (set '#gr $_object_3)
  (set '#pti $_object_2)
)
      )
      (setq pt1 (vlax-curve-getClosestPointTo #gr pt)
    pt2 (vlax-curve-getClosestPointTo #pti pt)
      )

      (if (> (distance pt pt1) (distance pt pt2))
(setq Inresult T)
(setq Inresult nil)
      )
      (mapcar (function (lambda (x)
  (progn
    (vla-delete x)
    (vlax-release-object x)
  )
)
      )
      (list $_object_2 $_object_3)
      )
      (if (not (vlax-object-released-p $_object_1))
(vlax-release-object $_object_1)
      )
    )
  )
  Inresult
)

;;test
;;(Isinside (getpoint) (car (entsel)))
Keep smile...

ymg

  • Guest
Re: Point is Inside polyline
« Reply #18 on: April 16, 2015, 05:16:17 PM »
Andrea,

The "isleft" test normally should not be too sensitive to
how far from zero you are.

Although it is a cross-product, you are multiplying only
the difference between coordinates.

Rojomp,

It could be a little faster if we would modify the isleft
to get 6 parameters (xp yp x1 y1 x2 y2) since
we already do some of the car and cadr in the calling
routine.

Beware! I did not test any of the code below,
just did the change on the fly!

Code - Auto/Visual Lisp: [Select]
  1. (defun isleft (xp yp x1 y1 x2 y2)
  2.    (minusp (- (* (- y1 yp) (- x2 xp))
  3.               (* (- x1 xp) (- y2 yp))
  4.            )
  5.    )
  6. )
  7.  

Code - Auto/Visual Lisp: [Select]
  1. ;;============================================================================;
  2. ;;                                                                            ;
  3. ;;      PtInPoly_p(): Winding number test for a point in a polygon            ;
  4. ;;                                                                            ;
  5. ;;      Input:    p = a point                                                 ;
  6. ;;                l = List of points forming the polyline                     ;
  7. ;;                                                                            ;
  8. ;;      Return:  t, if point is in polyline. (wn is not 0)                    ;
  9. ;;                                                                            ;
  10. ;; Original code in C++ by Dan Sunday                                         ;
  11. ;; See: http://geomalgorithms.com/a03-_inclusion.html                         ;
  12. ;;============================================================================;
  13.  
  14. (defun PtInPoly_p (p l / xp yp x1 y1 x2 y2 wn)
  15.  
  16.    ;; Returns t if point p is strictly to the left of v1->v2   by ymg         ;
  17.    (defun isleft (xp yp x1 y1 x2 y2)
  18.       (minusp (- (* (- y1 yp) (- x2 xp))
  19.                  (* (- x1 xp) (- y2 yp))
  20.               )
  21.       )
  22.    )
  23.  
  24.    (setq x1 (caar  l) y1 (cadar l)
  25.          xp (car p)   yp (cadr p)
  26.          wn 0                              
  27.    )     
  28.  
  29.    ;; Loop through all edges of polygon.                                      ;
  30.    (foreach p2 (cdr l)
  31.       (setq x2 (car p2) y2 (cadr p2))
  32.       (if (<= y1 yp)
  33.          (if (> y2 yp)                          
  34.             (if (isleft xp yp x1 y1 x2 y2) (setq wn (1+ wn)))
  35.          )  
  36.          (if (<= y2 yp)
  37.             (if (isleft xp yp x2 y2 x1 y1) (setq wn (1- wn)))
  38.          )
  39.       )
  40.       (setq x1 x2   y1 y2)
  41.    )
  42.    (not (zerop wn))
  43. )
  44.  

ymg
« Last Edit: April 16, 2015, 06:28:36 PM by ymg »

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Point is Inside polyline
« Reply #19 on: April 17, 2015, 08:49:00 AM »
Here's a quick test:

Quote
14739  Points


_$
Benchmarking ..........Elapsed milliseconds / relative speed for 128 iteration(s):


    (PTINPOLY_P P L)......1954 / 1.11 <fastest>
    (PTINPOLY_P2 P L).....2172 / 1.00 <slowest>
Benchmarking ..........Elapsed milliseconds / relative speed for 128 iteration(s):


    (PTINPOLY_P P L)......1969 / 1.12 <fastest>
    (PTINPOLY_P2 P L).....2203 / 1.00 <slowest>
Benchmarking ..........Elapsed milliseconds / relative speed for 128 iteration(s):


    (PTINPOLY_P P L)......1968 / 1.12 <fastest>
    (PTINPOLY_P2 P L).....2203 / 1.00 <slowest>
« Last Edit: April 17, 2015, 08:53:50 AM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ymg

  • Guest
Re: Point is Inside polyline
« Reply #20 on: April 17, 2015, 12:18:18 PM »
ronjomp,

Thanks! for the test.

There is a 15% difference,
but which one is the fastest
(6 arguments) or (3 arguments)?

ymg

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Point is Inside polyline
« Reply #21 on: April 17, 2015, 12:25:41 PM »
ronjomp,

Thanks! for the test.

There is a 15% difference,
but which one is the fastest
(6 arguments) or (3 arguments)?

ymg
Your code in the first post is faster.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ymg

  • Guest
Re: Point is Inside polyline
« Reply #22 on: April 17, 2015, 01:33:25 PM »
ronjomp,

Looks to me as if passing an argument takes about the same time
as a setq.

So maybe we could keep the test inline with the code
like so:

Code - Auto/Visual Lisp: [Select]
  1. ;;============================================================================;
  2. ;;                                                                            ;
  3. ;;      PtInPoly_p(): Winding number test for a point in a polygon            ;
  4. ;;                                                                            ;
  5. ;;      Input:    p = a point                                                 ;
  6. ;;                l = List of points forming the polyline                     ;
  7. ;;                                                                            ;
  8. ;;      Return:  t, if point is in polyline. (wn is not 0)                    ;
  9. ;;                                                                            ;
  10. ;; Original code in C++ by Dan Sunday                                         ;
  11. ;; See: http://geomalgorithms.com/a03-_inclusion.html                         ;
  12. ;;============================================================================;
  13.  
  14. (defun PtInPoly_p3 (p l / xp yp x1 y1 x2 y2 wn)
  15.  
  16.    (setq xp (car  p)  yp (cadr  p)
  17.          x1 (caar l)  y1 (cadar l)
  18.          wn 0                              
  19.    )     
  20.  
  21.    ;; Loop through all edges of polygon.                                      ;
  22.    (foreach p2 (cdr l)
  23.       (setq x2 (car p2) y2 (cadr p2))
  24.       (if (<= y1 yp)
  25.          (if (> y2 yp)                          
  26.             (if (minusp (- (* (- y1 yp) (- x2 xp))
  27.                            (* (- x1 xp) (- y2 yp))
  28.                         )
  29.                 )
  30.               (setq wn (1+ wn))
  31.             )
  32.          )  
  33.          (if (<= y2 yp)
  34.             (if (minusp (- (* (- y2 yp) (- x1 xp))
  35.                            (* (- x2 xp) (- y1 yp))
  36.                         )
  37.                 ) (setq wn (1- wn))
  38.             )
  39.          )
  40.       )
  41.       (setq x1 x2   y1 y2)
  42.    )
  43.    (not (zerop wn))
  44. )
  45.  


Result of a test on a small poly only 7 sides:

Quote
Benchmarking ..................Elapsed milliseconds / relative speed for 32768 iteration(s):

    (PTINPOLY_P3 P L)..........1232 / 1.09 <fastest>
    (VK_ISPOINTINSIDE P L).....1295 / 1.04
    (PTINPOLY_P2 P L)..........1310 / 1.02
    (PTINPOLY_P1 P L)..........1342 / 1.00 <slowest>
; 11 forms loaded from #<editor "C:/Lisp/ptinpoly_p test.LSP">
_$
Benchmarking ..................Elapsed milliseconds / relative speed for 32768 iteration(s):

    (PTINPOLY_P3 P L)..........1232 / 1.08 <fastest>
    (PTINPOLY_P2 P L)..........1295 / 1.02
    (VK_ISPOINTINSIDE P L).....1295 / 1.02
    (PTINPOLY_P1 P L)..........1326 / 1.00 <slowest>
; 11 forms loaded from #<editor "C:/Lisp/ptinpoly_p test.LSP">
_$
Benchmarking ..................Elapsed milliseconds / relative speed for 32768 iteration(s):

    (PTINPOLY_P3 P L)..........1248 / 1.08 <fastest>
    (PTINPOLY_P2 P L)..........1311 / 1.02
    (VK_ISPOINTINSIDE P L).....1326 / 1.01
    (PTINPOLY_P1 P L)..........1342 / 1.00 <slowest>
; 12 forms loaded from #<editor "C:/Lisp/ptinpoly_p test.LSP">
_$

Also notes that ptinpoly_p returns true if the points is on the poly.

Vovka's return false.

ymg
« Last Edit: April 17, 2015, 01:55:40 PM by ymg »

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Point is Inside polyline
« Reply #23 on: April 22, 2015, 03:48:26 AM »
Thank you ronjonp for your benchmark tests.

I am a little surprised that my code proves to be clearly slower than ymg's code. To find the cause I have created two initial variations (see *_01 and *_02 below), both of which avoid the addition of what can be a long list of integers. I have compared my original code and the two variations against ymg's latest function: PtInPoly_p3.

On BricsCAD V14 the benchmark results are markedly different from ronjonp's test. In all tests my original code is the fastest. Even if the point list contains 1000+ random points. Which, for my application, is not a very realistic scenario.

This makes me wonder what is causing the bottle neck on AutoCAD. Is it the use of mapcar and is foreach perhaps much faster on AutoCAD?

Benchmark:
Code: [Select]
(setq lstC ((629.78 294.034 0.0) (1964.78 1162.32 0.0) (2507.46 907.264 0.0) (2936.18 147.51 0.0) (2219.84 -623.098 0.0) (1036.79 -557.976 0.0) (249.903 -699.074 0.0) (249.903 -281.209 0.0) (1015.08 -281.209 0.0) (917.402 -37.002 0.0) (92.5253 28.1198 0.0) (92.5253 266.9 0.0)))

(setq pt (570.085 -112.977 0.0))

(KGX_BenchMark '((GEO_Geom_PointInsidePointList_P pt lstC) (GEO_Geom_PointInsidePointList_P_01 pt lstC) (GEO_Geom_PointInsidePointList_P_02 pt lstC) (PtInPoly_p3 pt lstC)) 100000)

Benchmarking .......... elapsed milliseconds / relative timing <100000 iterations>

  (GEO_GEOM_POINTINSIDEPOINTLIST_P PT LSTC) ........ 1593 / 1.27 <fastest>
  (GEO_GEOM_POINTINSIDEPOINTLIST_P_01 PT LSTC) ..... 1703 / 1.18
  (GEO_GEOM_POINTINSIDEPOINTLIST_P_02 PT LSTC) ..... 1813 / 1.11
  (PTINPOLY_P3 PT LSTC) ............................ 2016 / 1.00 <slowest>

Code:
Code - Auto/Visual Lisp: [Select]
  1. ; Based on C++ code by Dan Sunday.
  2. ; http://geomalgorithms.com/a03-_inclusion.html
  3. (defun GEO_Geom_PointInsidePointList_P (pt ptLst)
  4.   (=
  5.     1
  6.     (logand
  7.       (apply
  8.         '+
  9.         (mapcar
  10.           '(lambda (ptA ptB)
  11.             (if
  12.               (and
  13.                 (or
  14.                   (and (<= (cadr ptA) (cadr pt)) (> (cadr ptB) (cadr pt))) ; Upward crossing.
  15.                   (and (> (cadr ptA) (cadr pt)) (<= (cadr ptB) (cadr pt))) ; Downward crossing.
  16.                 )
  17.                 (<
  18.                   (car pt)
  19.                   (+
  20.                     (car ptA)
  21.                     (*
  22.                       (- (cadr pt) (cadr ptA))
  23.                       (/ (- (car ptB) (car ptA)) (- (cadr ptB) (cadr ptA))) ; DeltaX/DeltaY edge.
  24.                     )
  25.                   )
  26.                 )
  27.               )
  28.               1
  29.               0
  30.             )
  31.           )
  32.           ptLst
  33.           (append (cdr ptLst) (list (car ptLst)))
  34.         )
  35.       )
  36.       1
  37.     )
  38.   )
  39. )
  40.  
  41. (defun GEO_Geom_PointInsidePointList_P_01 (pt ptLst / ret)
  42.   (setq cnt 0)
  43.   (mapcar
  44.     '(lambda (ptA ptB)
  45.       (if
  46.         (and
  47.           (or
  48.             (and (<= (cadr ptA) (cadr pt)) (> (cadr ptB) (cadr pt))) ; Upward crossing.
  49.             (and (> (cadr ptA) (cadr pt)) (<= (cadr ptB) (cadr pt))) ; Downward crossing.
  50.           )
  51.           (<
  52.             (car pt)
  53.             (+
  54.               (car ptA)
  55.               (*
  56.                 (- (cadr pt) (cadr ptA))
  57.                 (/ (- (car ptB) (car ptA)) (- (cadr ptB) (cadr ptA))) ; DeltaX/DeltaY edge.
  58.               )
  59.             )
  60.           )
  61.         )
  62.         (setq cnt (1+ cnt))
  63.       )
  64.     )
  65.     ptLst
  66.     (append (cdr ptLst) (list (car ptLst)))
  67.   )
  68.   (= 1 (logand cnt 1))
  69. )
  70.  
  71. (defun GEO_Geom_PointInsidePointList_P_02 (pt ptLst / ret)
  72.   (setq cnt 0)
  73.   (setq xPt (car pt))
  74.   (setq yPt (cadr pt))
  75.   (mapcar
  76.     '(lambda (ptA ptB)
  77.       (if
  78.         (and
  79.           (or
  80.             (and (<= (cadr ptA) yPt) (> (cadr ptB) yPt)) ; Upward crossing.
  81.             (and (> (cadr ptA) yPt) (<= (cadr ptB) yPt)) ; Downward crossing.
  82.           )
  83.           (<
  84.             xPt
  85.             (+
  86.               (car ptA)
  87.               (*
  88.                 (- yPt (cadr ptA))
  89.                 (/ (- (car ptB) (car ptA)) (- (cadr ptB) (cadr ptA))) ; DeltaX/DeltaY edge.
  90.               )
  91.             )
  92.           )
  93.         )
  94.         (setq cnt (1+ cnt))
  95.       )
  96.     )
  97.     ptLst
  98.     (append (cdr ptLst) (list (car ptLst)))
  99.   )
  100.   (= 1 (logand cnt 1))
  101. )



ronjonp

  • Needs a day job
  • Posts: 7527
Re: Point is Inside polyline
« Reply #24 on: April 22, 2015, 09:17:47 AM »
This is what I get for a 940 point list:
Code: [Select]
Benchmarking ............Elapsed milliseconds / relative speed for 512 iteration(s):
    (PTINPOLY_P1 P L)............................2875 / 1.85 <fastest>
    (PTINPOLY_P2 P L)............................3859 / 1.38
    (PTINPOLY_P3 P L)............................4125 / 1.29
    (GEO_GEOM_POINTINSIDEPOINTLIST_P_02 ...).....4219 / 1.26
    (GEO_GEOM_POINTINSIDEPOINTLIST_P_01 ...).....5281 / 1.01
    (GEO_GEOM_POINTINSIDEPOINTLIST_P P L)........5328 / 1.00 <slowest>
And using your 'lstc' and 'pt':
Code: [Select]
Elapsed milliseconds / relative speed for 32768 iteration(s):
    (PTINPOLY_P3 P L)............................1125 / 1.51 <fastest>
    (PTINPOLY_P2 P L)............................1188 / 1.43
    (PTINPOLY_P1 P L)............................1297 / 1.31
    (GEO_GEOM_POINTINSIDEPOINTLIST_P_02 ...).....1468 / 1.16
    (GEO_GEOM_POINTINSIDEPOINTLIST_P_01 ...).....1656 / 1.03
    (GEO_GEOM_POINTINSIDEPOINTLIST_P P L)........1703 / 1.00 <slowest>
And here's compiled with the 940 point list and replacing your '(lambda with (function (lambda.
Code: [Select]
Elapsed milliseconds / relative speed for 2048 iteration(s):
    (PTINPOLY_P1 P L)............................1125 / 2.75 <fastest>
    (PTINPOLY_P2 P L)............................1187 / 2.61
    (PTINPOLY_P3 P L)............................1187 / 2.61
    (GEO_GEOM_POINTINSIDEPOINTLIST_P_02 ...).....2375 / 1.30
    (GEO_GEOM_POINTINSIDEPOINTLIST_P_01 ...).....3078 / 1.01
    (GEO_GEOM_POINTINSIDEPOINTLIST_P P L)........3094 / 1.00 <slowest>
« Last Edit: April 22, 2015, 09:45:33 AM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ymg

  • Guest
Re: Point is Inside polyline
« Reply #25 on: April 24, 2015, 06:39:49 AM »
Roy,

I am like you not understanding why ptinpoly_p1 would be faster.

There is obviously one "if"  too many in there and yet it does run faster
when I test it with Ronjomp poly.

ptinpoly_p1 was a litteral translation of Dan Sunday's code

ymg

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Point is Inside polyline
« Reply #26 on: April 25, 2015, 05:47:57 AM »
Benchmarking all the functions in this topic on BricsCAD V14.2.17 and on a sloooow computer...

Conclusion:
Ymg's function PtInPoly_p1 is the fastest in most situations.

Short point list:
Code: [Select]
(setq lstC '((629.78 294.034 0.0) (1964.78 1162.32 0.0) (2507.46 907.264 0.0) (2936.18 147.51 0.0) (2219.84 -623.098 0.0) (1036.79 -557.976 0.0) (249.903 -699.074 0.0) (249.903 -281.209 0.0) (1015.08 -281.209 0.0) (917.402 -37.002 0.0) (92.5253 28.1198 0.0) (92.5253 266.9 0.0)))
(setq pt '(570.085 -112.977 0.0))

Benchmarking .......... elapsed milliseconds / relative timing <32768 iterations>

  (GEO_GEOM_POINTINSIDEPOINTLIST_P_02 PT LSTC) ..... 500 / 1.56 <fastest>
  (GEO_GEOM_POINTINSIDEPOINTLIST_P PT LSTC) ........ 516 / 1.51
  (PTINPOLY_P1 PT LSTC) ............................ 531 / 1.47
  (GEO_GEOM_POINTINSIDEPOINTLIST_P_01 PT LSTC) ..... 531 / 1.47
  (PTINPOLY_P3 PT LSTC) ............................ 672 / 1.16
  (PTINPOLY_P2 PT LSTC) ............................ 735 / 1.06
  (VK_ISPOINTINSIDE PT LSTC) ....................... 781 / 1.00 <slowest>

Long point list (using P en L from ronjonp's pts.txt).
Code: [Select]
Benchmarking .......... elapsed milliseconds / relative timing <2048 iterations>

  (PTINPOLY_P1 P L) ............................. 1469 / 39.81 <fastest>
  (GEO_GEOM_POINTINSIDEPOINTLIST_P_02 P L) ...... 2125 / 27.52
  (GEO_GEOM_POINTINSIDEPOINTLIST_P P L) ......... 2375 / 24.63
  (GEO_GEOM_POINTINSIDEPOINTLIST_P_01 P L) ...... 2422 / 24.15
  (PTINPOLY_P3 P L) ............................. 2750 / 21.27
  (PTINPOLY_P2 P L) ............................. 2890 / 20.24
  (VK_ISPOINTINSIDE P L) ....................... 58485 / 1.00 <slowest>

Repeat of the last test leaving out VovKa's code:
Code: [Select]
Benchmarking .......... elapsed milliseconds / relative timing <2048 iterations>

  (PTINPOLY_P1 P L) ............................ 1297 / 1.93 <fastest>
  (GEO_GEOM_POINTINSIDEPOINTLIST_P P L) ........ 1953 / 1.28
  (GEO_GEOM_POINTINSIDEPOINTLIST_P_01 P L) ..... 2078 / 1.20
  (GEO_GEOM_POINTINSIDEPOINTLIST_P_02 P L) ..... 2188 / 1.14
  (PTINPOLY_P3 P L) ............................ 2453 / 1.02
  (PTINPOLY_P2 P L) ............................ 2500 / 1.00 <slowest>

GP

  • Newt
  • Posts: 83
  • Vercelli, Italy
Re: Point is Inside polyline
« Reply #27 on: April 25, 2015, 07:43:56 AM »
My $0.02   :-)

Code: [Select]
(defun inside_p (:p :Lst / remote_p cross)
    (setq remote_p (mapcar '+ '(1.0 1.0 0) (getvar "extmax")))
    (setq cross 0)
    (mapcar
        '(lambda (a b)
             (if (inters :p remote_p a b) (setq cross (1+ cross)))
         )
        :Lst (cdr :Lst)
    )
    (= 1 (rem cross 2))
)

ymg

  • Guest
Re: Point is Inside polyline
« Reply #28 on: April 25, 2015, 08:21:11 AM »
Gian Paolo,

I don't think this will work with self intersecting polylines.

Also what happen If point P is directly on a vertex of the polyline?

This is a well known method of counting the crossings.

ymg

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Point is Inside polyline
« Reply #29 on: April 25, 2015, 10:53:59 AM »
What about polylines with arc segments?

This thread may be of interest: http://www.theswamp.org/index.php?topic=47969.0
« Last Edit: April 25, 2015, 10:57:06 AM by Lee Mac »