Author Topic: How to find if a line is behind a polyline segement  (Read 3344 times)

0 Members and 1 Guest are viewing this topic.

Coder

  • Swamp Rat
  • Posts: 827
How to find if a line is behind a polyline segement
« on: October 25, 2015, 06:49:50 AM »
Hello guys .

I would like to know how to find if a line is completely laying behind a Polyline segment ! any idea ?

Thank you.

ymg

  • Guest
Re: How to find if a line is behind a polyline segement
« Reply #1 on: October 25, 2015, 07:28:23 AM »
Use a cross product to test if each point of the line
are on the segment.

You could use the following test twice, however it means
that you need to do it  for every segment of your poly.

Smarter way would be to locate the segment on which the point
lies and test only that segment with the second point of the line.

Code: [Select]
;;                                                                            ;
;; online_p      by ymg                                                        ;
;;                                                                            ;
;; Returns t if point is strictly on line v1-> v2                          ;
;;                                                                            ;
;; Arguments:  p, Point on line to be tested                                                      ;
;;            v1, First point of Polyline Segment                             ;
;;            v2, Second point of Polyline Segment v1->v2                               ;
;;                                                                            ;


(defun online_p (p v1 v2 / xp yp)
   (setq  xp (car  p) yp (cadr  p))

   (zerop
     (- (* (- (cadr v1) yp) (- (car v2) xp)) (* (- (car v1) xp) (- (cadr v2) yp)))
   )
)
« Last Edit: October 25, 2015, 07:36:53 AM by ymg »

Coder

  • Swamp Rat
  • Posts: 827
Re: How to find if a line is behind a polyline segement
« Reply #2 on: October 25, 2015, 07:55:58 AM »
Thank you for your reply .

I am sorry I don't know how to test that function , can you give an example please ?

Lee Mac

  • Seagull
  • Posts: 12924
  • London, England
Re: How to find if a line is behind a polyline segement
« Reply #3 on: October 25, 2015, 08:17:58 AM »
To test whether a line lies beneath a polyline segment (and entirely within the bounds of the segment), I would use the following method:
Code - Auto/Visual Lisp: [Select]
  1. (defun line-on-line ( p1 p2 l1 l2 / d1 )
  2.     (setq d1 (distance l1 l2))
  3.     (and (equal (+ (distance l1 p1) (distance l2 p1)) d1 1e-8)
  4.          (equal (+ (distance l1 p2) (distance l2 p2)) d1 1e-8)
  5.     )
  6. )

Example program:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:test ( / a b e p )
  2.     (if
  3.         (and
  4.             (setq a (getpoint   "\nPick 1st line endpoint: "))
  5.             (setq b (getpoint a "\nPick 2nd line endpoint: "))
  6.             (setq p (entsel    "\nSelect polyline segment: "))
  7.             (setq e (car p)
  8.                   p (vlax-curve-getparamatpoint e (vlax-curve-getclosestpointto e (trans (cadr p) 1 0)))
  9.             )
  10.         )
  11.         (line-on-line
  12.             (trans a 1 0)
  13.             (trans b 1 0)
  14.             (vlax-curve-getpointatparam e (fix p))
  15.             (vlax-curve-getpointatparam e (1+ (fix p)))
  16.         )
  17.     )
  18. )


Coder

  • Swamp Rat
  • Posts: 827
Re: How to find if a line is behind a polyline segement
« Reply #4 on: October 25, 2015, 08:32:02 AM »
Thank you Lee for your reply and example.

You are testing the two lines by distance that is logically should be the same if they are beneath each other , correct ?

David Bethel

  • Swamp Rat
  • Posts: 656
Re: How to find if a line is behind a polyline segement
« Reply #5 on: October 25, 2015, 08:38:53 AM »
Similarly :

Code - Auto/Visual Lisp: [Select]
  1. ;;;ARG -> TestPt LinePt1 LinePt2 Fuzz
  2. ;;;RET T nil
  3. (defun is_pt_online (pt l1 l2 fz)
  4.   (and (numberp fz)
  5.        (equal (distance l1 l2)
  6.               (+ (distance l1 pt)
  7.                  (distance l2 pt)) fz)))
  8.  
  9. ;;;ARG -> LinePt1 LinePt2 SegPt1 SegPt2 Fuzz
  10. ;;;RET T nil
  11. (defun is_line_on_seg (l1 l2 s1 s2 fz)
  12.    (and (is_pt_online l1 s1 s2 fz)
  13.         (is_pt_online l2 s1 s2 fz)))
  14.  
  15.  
R12 Dos - A2K

Lee Mac

  • Seagull
  • Posts: 12924
  • London, England
Re: How to find if a line is behind a polyline segement
« Reply #6 on: October 25, 2015, 08:49:09 AM »
You are testing the two lines by distance that is logically should be the same if they are beneath each other , correct ?

Yes, the function is essentially using the Triangle Inequality with each line endpoint tested against the two polyline segment endpoints.

Coder

  • Swamp Rat
  • Posts: 827
Re: How to find if a line is behind a polyline segement
« Reply #7 on: October 25, 2015, 09:08:13 AM »
Similarly :

Code - Auto/Visual Lisp: [Select]
  1. ;;;ARG -> TestPt LinePt1 LinePt2 Fuzz
  2. ;;;RET T nil
  3. (defun is_pt_online (pt l1 l2 fz)
  4.   (and (numberp fz)
  5.        (equal (distance l1 l2)
  6.               (+ (distance l1 pt)
  7.                  (distance l2 pt)) fz)))
  8.  
  9. ;;;ARG -> LinePt1 LinePt2 SegPt1 SegPt2 Fuzz
  10. ;;;RET T nil
  11. (defun is_line_on_seg (l1 l2 s1 s2 fz)
  12.    (and (is_pt_online l1 s1 s2 fz)
  13.         (is_pt_online l2 s1 s2 fz)))
  14.  
  15.  
Thank you David , that works perfectly.  :-)

You are testing the two lines by distance that is logically should be the same if they are beneath each other , correct ?

Yes, the function is essentially using the Triangle Inequality with each line endpoint tested against the two polyline segment endpoints.

Thank you for the codes and for replying to my question. :-)

Many thanks guys.



irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: How to find if a line is behind a polyline segement
« Reply #8 on: October 26, 2015, 02:38:08 AM »
I was just wondering ... since it's just a line and a straight segment, and you're looking to see if the line is "completely" underneath the PL ... all you really need to do is test if the line's endpoints lie on the polyline.
Code - Auto/Visual Lisp: [Select]
  1. (defun c:LinePolyTest  (/ line poly lStart lEnd pStart pEnd dist)
  2.   (setq poly   (entsel "\nPick polyline: ")
  3.         line   (entsel "\nPick line: ")
  4.         dist   (getdist "\nEnter maximum error distance: ")
  5.         lStart (vlax-curve-getStartPoint (car line))
  6.         lEnd   (vlax-curve-getEndPoint (car line))
  7.         pStart (vlax-curve-getClosestPointTo (car poly) lStart)
  8.         pEnd   (vlax-curve-getClosestPointTo (car poly) lEnd))
  9.   (princ
  10.     (strcat
  11.       "The line is"
  12.       (if (and (equal lStart pStart dist)
  13.                (equal lEnd pEnd dist))
  14.         " "
  15.         " not")
  16.       " underneath the polyline."))
  17.   (princ))
I'm using a user specified distance as a fuzz factor, since the chance that floating point numbers in points are exactly the same is close to nil even if they're as close as damit. Change to whatever other fuzz you'd prefer.

Of course, this won't work if the line extends beyond the polyline - in that case the others' codes should suffice.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Lee Mac

  • Seagull
  • Posts: 12924
  • London, England
Re: How to find if a line is behind a polyline segement
« Reply #9 on: October 26, 2015, 04:54:20 AM »
I was just wondering ... since it's just a line and a straight segment, and you're looking to see if the line is "completely" underneath the PL ... all you really need to do is test if the line's endpoints lie on the polyline.

But what if the line endpoints lie on different polyline segments?


irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: How to find if a line is behind a polyline segement
« Reply #10 on: October 26, 2015, 05:26:27 AM »
But what if the line endpoints lie on different polyline segments?
OK, yes, I can then agree on that. Something like a triangle with 2 sides being the polyline and another the line - that would break my code and give a false positive. So I'd thus need to check if both of the line's endpoints fall within the "same" segment - i.e. the parameter needs to be in the same integer range.

But then also in the same breath it's possible to have two segments be co-linear and the line spanning through them. I think this may be a bit more complicated than it seemed. Will have to think about it more.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

David Bethel

  • Swamp Rat
  • Posts: 656
Re: How to find if a line is behind a polyline segement
« Reply #11 on: October 26, 2015, 05:40:15 AM »
And we assumed the OP had all points / lines on the same Z axis
R12 Dos - A2K

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: How to find if a line is behind a polyline segement
« Reply #12 on: October 26, 2015, 05:42:23 AM »
And we assumed the OP had all points / lines on the same Z axis


And that the polyline has no bulges.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Lee Mac

  • Seagull
  • Posts: 12924
  • London, England
Re: How to find if a line is behind a polyline segement
« Reply #13 on: October 26, 2015, 10:55:47 AM »
And we assumed the OP had all points / lines on the same Z axis

No, the functions which test the straight-line distance between a given line endpoint and the two polyline segment endpoints will also perform correctly with 3D points (providing all three points are expressed relative to the same coordinate system, which they are in my example).

David Bethel

  • Swamp Rat
  • Posts: 656
Re: How to find if a line is behind a polyline segement
« Reply #14 on: October 26, 2015, 02:39:49 PM »
No, the functions which test the straight-line distance between a given line endpoint and the two polyline segment endpoints will also perform correctly with 3D points (providing all three points are expressed relative to the same coordinate system, which they are in my example).

Yes, that is true.  My thought was more of , if in a plan view, were the line were directly under the segment, but not on the same z axis.  A 2D only kind of test.

-David
R12 Dos - A2K

Lee Mac

  • Seagull
  • Posts: 12924
  • London, England
Re: How to find if a line is behind a polyline segement
« Reply #15 on: October 26, 2015, 05:41:34 PM »
No, the functions which test the straight-line distance between a given line endpoint and the two polyline segment endpoints will also perform correctly with 3D points (providing all three points are expressed relative to the same coordinate system, which they are in my example).

Yes, that is true.  My thought was more of , if in a plan view, were the line were directly under the segment, but not on the same z axis.  A 2D only kind of test.

-David

Oh I see - a sort of 'apparent collinearity', view-dependent.