Author Topic: Apply "vlax-curve-..." to coordinates, not to objects?  (Read 2033 times)

0 Members and 1 Guest are viewing this topic.

Peter2

  • Swamp Rat
  • Posts: 653
Apply "vlax-curve-..." to coordinates, not to objects?
« 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?
Peter

AutoCAD Map 3D 2023 German (so some technical terms will be badly retranslated to English)
BricsCAD V23

VovKa

  • Water Moccasin
  • Posts: 1632
  • Ukraine
Re: Apply "vlax-curve-..." to coordinates, not to objects?
« Reply #1 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))
  )
)

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Apply "vlax-curve-..." to coordinates, not to objects?
« Reply #2 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
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

Peter2

  • Swamp Rat
  • Posts: 653
Re: Apply "vlax-curve-..." to coordinates, not to objects?
« Reply #3 on: April 04, 2017, 06:31:40 AM »
 8-) Thanks!!
Peter

AutoCAD Map 3D 2023 German (so some technical terms will be badly retranslated to English)
BricsCAD V23

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Apply "vlax-curve-..." to coordinates, not to objects?
« Reply #4 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. )

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Apply "vlax-curve-..." to coordinates, not to objects?
« Reply #5 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:
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

VovKa

  • Water Moccasin
  • Posts: 1632
  • Ukraine
Re: Apply "vlax-curve-..." to coordinates, not to objects?
« Reply #6 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)
)

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Apply "vlax-curve-..." to coordinates, not to objects?
« Reply #7 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. :)
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg