Author Topic: Help with intersection  (Read 5835 times)

0 Members and 1 Guest are viewing this topic.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Help with intersection
« Reply #15 on: July 18, 2015, 09:20:24 AM »
@ mianbaoche:
There is no need to use (Max-distance). The (inters) function has an optional 5th argument:
http://docs.autodesk.com/ACD/2011/ENU/filesALR/WS1a9193826455f5ff1a32d8d10ebc6b7ccc-69d4.htm

Please use code tags: http://www.theswamp.org/index.php?topic=48309.0.

BTW: Welcome to The Swamp.
« Last Edit: July 18, 2015, 09:25:39 AM by roy_043 »

velasquez

  • Newt
  • Posts: 195
Re: Help with intersection
« Reply #16 on: July 21, 2015, 07:56:18 PM »
You can step through the vertices of the polyline, and use the built in Lisp function 'inters' with the four points ( two from the polyline, per segment, and the two you show as yellow in your image ) you have.

1+

Here's an example:
http://www.theswamp.org/index.php?topic=43630.msg489026#msg489026

Hello Lee,
I worked with her suggestion and functions to create the JoyFindInters.lsp code.
The code needs to be improved, but he finds the intersection of two points and a polyline that is not straight.

Code: [Select]
;; Based on Lee Mac codes
;; Args -> <LWPolyline-entity> <Entity name: 7ffffc6cc40> <point1>
;; (setq JoyPT (JoyFindInters en ptbase)) ==> (792.205 374.229)
;;
  (defun JoyFindInters (en JoyPtBase / LM:ProjectPointToLine GetPointsInBoundingBox  _polyinters JoyPointList JoyPtAux)
    ;; Lee Mac
    ;; Projects pt onto the line defined by p1,p2
    (defun LM:ProjectPointToLine (pt p1 p2 / nm)
      (setq nm (mapcar '- p2 p1)
            p1 (trans p1 0 nm)
            pt (trans pt 0 nm)
      ) ;_ fim de setq
      (trans (list (car p1) (cadr p1) (caddr pt)) nm 0)
    ) ;_ fim de defun
;;;
    ;; Bounding Box  -  Lee Mac
    ;; List with top left point top right point
    (defun GetPointsInBoundingBox (/ a b lst)
      (vla-getboundingbox
        (vlax-ename->vla-object en)
        'a
        'b
      ) ;_ fim de vla-getboundingbox
      (setq lst (mapcar 'vlax-safearray->list (list a b)))
      (mapcar '(lambda (a) (mapcar '(lambda (b) ((eval b) lst)) a))
              '((caar cadadr) (caadr cadadr))
      ) ;_ fim de mapcar
    ) ;_ fim de defun
;;;
;;;Lee Mac's suggestion
    (defun _polyinters (/ vl)
      (setq en (entget en)
            vl (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= 10 (car x))) en))
      ) ;_ fim de setq
      (vl-remove-if
        'null
        (mapcar '(lambda (p3 p4) (inters JoyPtBase JoyPtAux p3 p4))
                vl
                (if (= 1 (logand 1 (cdr (assoc 70 en))))
                  (append (cdr vl) (list (car vl)))
                  (cdr vl)
                ) ;_ fim de if
        ) ;_ fim de mapcar
      ) ;_ fim de vl-remove-if
    ) ;_ fim de defun
    (alert "cheguei")
    (setq JoyPointList (GetPointsInBoundingBox)
          JoyPtAux     (LM:ProjectPointToLine JoyPtBase (car JoyPointList) (cadr JoyPointList))
    ) ;_ fim de setq
;;;
    (car (_polyinters))
  ) ;_ fim de defun


squirreldip

  • Newt
  • Posts: 114
Re: Help with intersection
« Reply #17 on: July 21, 2015, 08:22:04 PM »
You can step through the vertices of the polyline, and use the built in Lisp function 'inters' with the four points ( two from the polyline, per segment, and the two you show as yellow in your image ) you have.

1+

Here's an example:
http://www.theswamp.org/index.php?topic=43630.msg489026#msg489026

2+

Complication will be if the polyline has curves (or splines).  If not, I would cycle through the vertices and use the inters command - each time an intersection is found I would check to see that the point was between the polyline pair.

Another complication is that there may be more than one intersection - you'd then need add some logic to which one you want to return.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Help with intersection
« Reply #18 on: July 22, 2015, 04:12:45 AM »
@ mianbaoche:
... The (inters) function has an optional 5th argument:
http://docs.autodesk.com/ACD/2011/ENU/filesALR/WS1a9193826455f5ff1a32d8d10ebc6b7ccc-69d4.htm
Velasquez what you are doing is similar to what mianbaoche proposed. There is no need for your two nested functions.

If the onseg argument is present and is nil, inters returns the point where the lines intersect, even if that point is off the end of one or both of the lines. If the onseg argument is omitted or is not nil, the intersection point must lie on both lines or inters returns nil. The inters function returns nil if the two lines do not intersect.
« Last Edit: July 22, 2015, 04:16:54 AM by roy_043 »

velasquez

  • Newt
  • Posts: 195
Re: Help with intersection
« Reply #19 on: July 22, 2015, 08:27:56 AM »
@ mianbaoche:
... The (inters) function has an optional 5th argument:
http://docs.autodesk.com/ACD/2011/ENU/filesALR/WS1a9193826455f5ff1a32d8d10ebc6b7ccc-69d4.htm
Velasquez what you are doing is similar to what mianbaoche proposed. There is no need for your two nested functions.

If the onseg argument is present and is nil, inters returns the point where the lines intersect, even if that point is off the end of one or both of the lines. If the onseg argument is omitted or is not nil, the intersection point must lie on both lines or inters returns nil. The inters function returns nil if the two lines do not intersect.

The image shows the amount points of intersection with the polyline, points can not be extended need to be exact. I could not do it otherwise.
« Last Edit: July 22, 2015, 08:35:46 AM by velasquez »

velasquez

  • Newt
  • Posts: 195
Re: Help with intersection
« Reply #20 on: July 22, 2015, 08:33:15 AM »
@ mianbaoche:
... The (inters) function has an optional 5th argument:
http://docs.autodesk.com/ACD/2011/ENU/filesALR/WS1a9193826455f5ff1a32d8d10ebc6b7ccc-69d4.htm
Velasquez what you are doing is similar to what mianbaoche proposed. There is no need for your two nested functions.

If the onseg argument is present and is nil, inters returns the point where the lines intersect, even if that point is off the end of one or both of the lines. If the onseg argument is omitted or is not nil, the intersection point must lie on both lines or inters returns nil. The inters function returns nil if the two lines do not intersect.

Hello Roy,
Working this way the point can occur outside the polyline for the search loop. This should not happen to me.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Help with intersection
« Reply #21 on: July 22, 2015, 09:24:38 AM »
Hello Roy,
Working this way the point can occur outside the polyline for the search loop. This should not happen to me.
The solution that I would use:
Call the (inters) function with nil as the 5th argument.
If an intersection is found check the intersection with:
Code: [Select]
(equal ptFound (vlax-curve-getclosestpointto polylineObj ptFound T) 1e-8)

velasquez

  • Newt
  • Posts: 195
Re: Help with intersection
« Reply #22 on: July 22, 2015, 12:33:40 PM »
Hello Roy,
Working this way the point can occur outside the polyline for the search loop. This should not happen to me.
The solution that I would use:
Call the (inters) function with nil as the 5th argument.
If an intersection is found check the intersection with:
Code: [Select]
(equal ptFound (vlax-curve-getclosestpointto polylineObj ptFound T) 1e-8)
Thank you Roy,
I will try your suggestion.

velasquez

  • Newt
  • Posts: 195
Re: Help with intersection
« Reply #23 on: July 24, 2015, 06:48:15 PM »
Hello Roy,
Working this way the point can occur outside the polyline for the search loop. This should not happen to me.
The solution that I would use:
Call the (inters) function with nil as the 5th argument.
If an intersection is found check the intersection with:
Code: [Select]
(equal ptFound (vlax-curve-getclosestpointto polylineObj ptFound T) 1e-8)

Hello Roy
I tested your suggestion and it worked well in my case.
Thanks

Code: [Select]
;;;List of polyline segments in 3D points
;;;Args <LWPolyline-entity> Index of the first vertex
;;;(f en 0))

(defun f (en i)
  (if (<= i (vlax-curve-getendparam en))
    (cons
      (vlax-curve-getpointatparam en i)
      (f en (1+ i))
    ) ;_ fim de cons
  ) ;_ fim de if
) ;_ fim de defun

;;;Args -> <LWPolyline-entity> <Entity name: 7ffffc6cc40> <point1> <point2>
;;; (setq IntersPt (JoyFindIntersec en pt1 pt2)) ==> (792.205 374.229)
(defun JoyFindIntersec (JoyEnt p1 p2 / JoyPlSegments)
;;;
  (vl-load-com)
;;; 
;;;List of segments
  (setq JoySegments (f JoyEnt 0))
;;;Creates a list of possible intersections
  (setq JoyListPts
         (vl-remove-if
           'null
           (mapcar '(lambda (p3 p4) (inters p1 p2 p3 p4 nil))
                   lista
                   (cdr lista)
           ) ;_ fim de mapcar
         ) ;_ fim de vl-remove-if
  ) ;_ fim de setq
;;;
;;;Checks the position of the point in polyline
  (car (vl-remove-if
         'null
         (mapcar '(lambda (x)
                    (if (equal x (vlax-curve-getclosestpointto JoyEnt x t) 1e-8)
                      x
                    ) ;_ fim de if
                  ) ;_ fim de lambda
                 JoyListPts
         ) ;_ fim de mapcar
       ) ;_ fim de vl-remove-if
  ) ;_ fim de car"