TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Peter2 on April 04, 2017, 04:59:03 AM

Title: Apply "vlax-curve-..." to coordinates, not to objects?
Post by: Peter2 on April 04, 2017, 04:59:03 AM
(Question is low priority; it's more because of curiosity ..)

All vlax-curve functions apply to objects. But is it possible to apply them to (list of) coordinates?

Example:
- I have coordinates 0,0 and 10,10 which define a line.
- The line does not exist yet.
- Is it possible to apply "vlax-curve-getClosestPointTo" to the coordinates or to I have to draw the line temporarily?
Title: Re: Apply "vlax-curve-..." to coordinates, not to objects?
Post by: VovKa on April 04, 2017, 05:12:53 AM
no, not possible
you'll have to write your own function
Code: [Select]
(defun vk_GetNearestPointOnLine (p1 p2 p / d v)
  (setq d (distance p1 p2)
v (/ (+ (- (expt (distance p1 p) 2) (expt (distance p2 p) 2)) (expt d 2)) (* 2 d))
  )
  (cond ((<= v 0) p1)
((>= v d) p2)
(t (polar p1 (angle p1 p2) v))
  )
)
Title: Re: Apply "vlax-curve-..." to coordinates, not to objects?
Post by: Grrr1337 on April 04, 2017, 05:47:09 AM

Another, doesn't work with bulges tho:
Code - Auto/Visual Lisp: [Select]
  1. ; (NearestToLine ((lambda (a / b) (if (and a (setq b (getpoint a))) (list a b))) (getpoint)) (getpoint))
  2. (defun NearestToLine ( 2p p / ang px r )
  3.   (and
  4.     (= 2 (length 2p))
  5.     (setq ang (apply 'angle 2p))
  6.     (setq px (polar p (+ (/ PI 2.) ang) (apply 'distance 2p)))
  7.     (setq r (apply 'inters (append 2p (list p px) '(()))))
  8.     (progn (apply 'grdraw (append 2p '(2))) ; this (progn) is used just to check, can be erased later
  9.       (entmakex (list (cons 0 "POINT") (cons 62 3) (cons 10 p)))
  10.       (entmakex (list (cons 0 "POINT") (cons 62 4) (cons 10 px)))
  11.       (entmakex (list (cons 0 "POINT") (cons 62 1) (cons 10 r)))
  12.     ); progn
  13.   ); and
  14.   r
  15. ); defun NearestToLine
Title: Re: Apply "vlax-curve-..." to coordinates, not to objects?
Post by: Peter2 on April 04, 2017, 06:31:40 AM
 8-) Thanks!!
Title: Re: Apply "vlax-curve-..." to coordinates, not to objects?
Post by: Lee Mac on April 04, 2017, 01:15:42 PM
Nice algebraic solution VovKa.

Here's another using vectors:
Code - Auto/Visual Lisp: [Select]
  1. (defun nearestpointonline ( p1 p2 p / d v x )
  2.     (setq d (distance p1 p2)
  3.           v (mapcar '(lambda ( a b ) (/ (- b a) d)) p1 p2)
  4.           x (apply '+ (mapcar '* v (mapcar '- p p1)))
  5.     )
  6.     (cond
  7.         (   (<= x 0) p1)
  8.         (   (<= d x) p2)
  9.         (   (mapcar '(lambda ( a b ) (+ a (* b x))) p1 v))
  10.     )
  11. )
Title: Re: Apply "vlax-curve-..." to coordinates, not to objects?
Post by: Grrr1337 on April 04, 2017, 02:59:22 PM
Interesting... now I'm curious how you could write inters function, that uses math & vectors only:
Code: [Select]
(defun _inters ( p1 p2 p3 p4 ) ...) :thinking:
Title: Re: Apply "vlax-curve-..." to coordinates, not to objects?
Post by: VovKa on April 04, 2017, 03:10:56 PM
Interesting... now I'm curious how you could write inters function, that uses math & vectors only:
Code: [Select]
(defun _inters ( p1 p2 p3 p4 ) ...) :thinking:
going back to school :)
Code: [Select]
(defun vk_Inters (p1 p2 p3 p4 / x y x1 x2 y1 y2 k1 k2 l1 l2)
  (setq x1 (car p1)
x2 (car p2)
y1 (cadr p1)
y2 (cadr p2)
k1 (/ (- y2 y1) (- x2 x1))
l1 (- y1 (* x1 k1))
x1 (car p3)
x2 (car p4)
y1 (cadr p3)
y2 (cadr p4)
k2 (/ (- y2 y1) (- x2 x1))
l2 (- y1 (* x1 k2))
x  (/ (- l2 l1) (- k1 k2))
y  (+ (* k1 x) l1)
  )
  (list x y)
)
Title: Re: Apply "vlax-curve-..." to coordinates, not to objects?
Post by: Grrr1337 on April 04, 2017, 03:51:32 PM
going back to school :)

Thats a nice one!
I'd wish we've spend some more time studying about vectors and coordinates in our math classes back at school.
Now when working with Autocad, its easier to visualise it. :)