- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Community,
The AutoCAD CAL command offers the VEC1 function to calculate a unit vector.
https://help.autodesk.com/view/ACDLT/2025/ENU/?guid=GUID-28103F9A-424C-402B-9B10-A45147D07B96
For example, (1.0 0.0 0.0) or (0.0 1.0 0.0) or (0.707107 0.707107 0)
Is there a more efficient way to calculate the unit vector in AutoLISP or Visual LISP than my method below?
(defun C:TEST ()
(graphscr)
(if (setq pt1 (getpoint "\nFirst point: "))
(progn
(if (setq pt2 (getpoint "\nSecond point: "))
(progn
;; vector
(setq dx (- (car pt2) (car pt1))
dy (- (cadr pt2) (cadr pt1))
dz (- (caddr pt2) (caddr pt1))
);_setq
;; unit vector
(setq vx (atof (rtos (cos (angle pt1 pt2)) 2 8))
vy (atof (rtos (sin (angle pt1 pt2)) 2 8))
vz nil ;;???
);_setq
;; report
(princ "\nAutoLISP Vec: ") (princ (list dx dy dz))
(princ "\nAutoLISP Vec1: ")(princ (list vx vy vz))
(textscr)
);_progn
);_if
);_progn
);_if
(princ)
);_C:TEST
I have searched references and online, but have not found an equivalent VEC1 function in AutoLISP or Visual LISP?
I am aware of (mapcar '- pt1 pt2) method to get a vector, but I need a unit vector. This will be used for an XLine.
BTW - I used ATOF and RTOS functions to round of values such as (6.12323e-17 1.0 nil).
Also, struggling to calculate the Z vector. Most times points will be 2D, but wanted option in case of 3D points.
Likely must calculate the point's incline from a plane, (presume XY of WCS). Assistance would be appreciated.
Thank you for your time and attention. I look forward to your replies.
Regards,
Jerry
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
Solved! Go to Solution.