这是矩形程序的3D版本。我想我会有一些乐趣,并使用向量方法。叉积用于确定与平面垂直的向量,然后使用叉积确定与p3方向上的线p1p2垂直的向量。注意,包括点积和叉积函数。
- (defun c:rect3d (/ p1 p2 p3 p4 p5 n u m d h)
- ; get three points
- (setq p1 (getpoint "\nPoint 1: "))
- (setq p2 (getpoint p1 "\n2nd Point: "))
- (setq p3 (getpoint p2 "\Opposing edge Point: "))
- ; compute normal to plane defined by p1 p2 p3
- (setq n (cross (mapcar '- p3 p1) (mapcar '- p2 p1)))
- ; compute vector perpendicular to line p1 p2
- (setq u (cross (mapcar '- p2 p1) n))
- ; compute magnitude of u
- (setq m (distance '(0 0 0) u))
- ;convert u to unit vector
- (setq u (mapcar '/ u (list m m m)))
- ; get perpendicular length from line p1p2 to point p3
- (setq d (dot (mapcar '- p3 p1) u))
- ;convert length to a vector
- (setq h (mapcar '* (list d d d) u))
- ;define other two corners of the rectangle
- (setq p4 (mapcar '+ p1 h))
- (setq p5 (mapcar '+ p2 h))
- (command "3dpoly" p1 p2 p5 p4 p1 "")
- (princ)
- )
- ; Compute the dot product of 2 vectors a and b
- (defun dot (a b / dd)
- (setq dd (mapcar '* a b))
- (setq dd (+ (nth 0 dd) (nth 1 dd) (nth 2 dd)))
- ) ;end of dot
- ; Compute the cross product of 2 vectors a and b
- (defun cross (a b / crs)
- (setq crs (list
- (- (* (nth 1 a) (nth 2 b))
- (* (nth 1 b) (nth 2 a))
- )
- (- (* (nth 0 b) (nth 2 a))
- (* (nth 0 a) (nth 2 b))
- )
- (- (* (nth 0 a) (nth 1 b))
- (* (nth 0 b) (nth 1 a))
- )
- ) ;end list
- ) ;end setq c
- ) ;end cross
|