Author Topic: Arcs in 3D  (Read 2008 times)

0 Members and 1 Guest are viewing this topic.

Jeremy

  • Guest
Arcs in 3D
« on: July 21, 2012, 06:51:33 PM »
Having trouble figuring out how to place an arc in 3D. I have a center cp, radius r and two vectors v1, v2 that point to the endpoints of the arc. I want to draw the smaller of the two arcs defined by the sweep of the vectors. I know how to get the normal to the two vectors but I have no idea how one determines the proper start and end angles. So I am trying to write a function of the form (draw-3D-arc cp r v1 v2). Has anyone done something like this?

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: Arcs in 3D
« Reply #1 on: July 22, 2012, 12:08:06 PM »
Try this
Code - Auto/Visual Lisp: [Select]
  1. (defun C:TEST ( / c u r v)
  2.   (if
  3.     (and
  4.       (setq c (getpoint   "\nCenter: "))
  5.       (setq u (getpoint c "\nStart direction: "))
  6.       (setq v (getpoint c "\nEnd direction: "))
  7.       (setq r (getdist  c "\nRadius: "))
  8.       )
  9.     (progn
  10.       (setq c (trans c 1 0)
  11.             u (trans u 1 0)
  12.             v (trans v 1 0)
  13.             )
  14.       (arc3d c r (mapcar '- u c) (mapcar '- v c))
  15.     )
  16.   )
  17.   (princ)
  18. )
  19. ;3d Arc
  20. ;c - center
  21. ;r - radius
  22. ;u - start direction
  23. ;v - end direction
  24. ;Stefan M. 22.07.2012
  25. (defun arc3d (c r u v / n s e)
  26.   (if
  27.     (equal (setq n (cross_prod u v)) '(0.0 0.0 0.0) 1e-8)      ;180deg. arcs require extra point
  28.     (princ "\nColinear directions... Unable to find a solution")
  29.     (progn
  30.       (setq c (trans c 0 n)
  31.             u (trans u 0 n)
  32.             v (trans v 0 n)
  33.             s (apply 'atan (list (cadr u) (car u)))
  34.             e (apply 'atan (list (cadr v) (car v)))
  35.             )
  36.       (entmake
  37.         (list
  38.           '(0 . "ARC")
  39.           (cons 10 c)
  40.           (cons 40 r)
  41.           (cons 210 n)
  42.           (cons 50 s)
  43.           (cons 51 e)
  44.           )
  45.         )
  46.       )
  47.     )
  48.   )
  49.  
  50. (defun cross_prod (a b)
  51.   (list
  52.     (- (* (cadr  a) (caddr b)) (* (caddr a) (cadr  b)))
  53.     (- (* (caddr a) (car   b)) (* (car   a) (caddr b)))
  54.     (- (* (car   a) (cadr  b)) (* (cadr  a) (car   b)))
  55.     )
  56.   )

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: Arcs in 3D
« Reply #2 on: July 22, 2012, 02:17:04 PM »
Another, for a 3-point Arc:

Code - Auto/Visual Lisp: [Select]
  1. (defun c:test ( / p1 p2 p3 )
  2.     (if
  3.         (and
  4.             (setq p1 (getpoint "\n1st Point: "))
  5.             (setq p2 (getpoint "\n2nd Point: " p1))
  6.             (setq p3 (getpoint "\n3rd Point: " p2))
  7.         )
  8.         (LM:3P-Arc
  9.             (trans p1 1 0)
  10.             (trans p2 1 0)
  11.             (trans p3 1 0)
  12.         )
  13.     )
  14. )
  15.  
  16. (defun LM:3P-Arc ( p1 p2 p3 / cn m1 m2 nv )
  17.     (setq nv (v^v (mapcar '- p1 p2) (mapcar '- p3 p2))
  18.           m1 (_mid p1 p2)
  19.           m2 (_mid p2 p3)
  20.     )
  21.     (if (setq cn
  22.             (inters
  23.                 (trans m1 0 1)
  24.                 (trans (mapcar '+ m1 (v^v (mapcar '- p1 p2) nv)) 0 1)
  25.                 (trans m2 0 1)
  26.                 (trans (mapcar '+ m2 (v^v (mapcar '- p3 p2) nv)) 0 1)
  27.                 nil
  28.             )
  29.         )
  30.         (entmake
  31.             (append
  32.                 (list
  33.                    '(0 . "ARC")
  34.                     (cons 010 (trans cn 1 nv))
  35.                     (cons 040 (distance (trans cn 1 0) p1))
  36.                     (cons 210 nv)
  37.                 )
  38.                 (if (LM:Clockwise-p p1 p2 p3)
  39.                     (list
  40.                         (cons 50 (angle (trans cn 1 nv) (trans p3 0 nv)))
  41.                         (cons 51 (angle (trans cn 1 nv) (trans p1 0 nv)))
  42.                     )
  43.                     (list
  44.                         (cons 50 (angle (trans cn 1 nv) (trans p1 0 nv)))
  45.                         (cons 51 (angle (trans cn 1 nv) (trans p3 0 nv)))
  46.                     )
  47.                 )
  48.             )
  49.         )
  50.     )
  51. )
  52.                                          
  53. (defun _mid ( a b )
  54.     (mapcar '(lambda ( a b ) (/ (+ a b) 2.0)) a b)
  55. )
  56.  
  57. ;; Vector Cross Product  -  Lee Mac
  58. ;; Args: u,v - vectors in R^3
  59.  
  60. (defun v^v ( u v )
  61.     (list
  62.         (- (* (cadr u) (caddr v)) (* (cadr v) (caddr u)))
  63.         (- (* (car  v) (caddr u)) (* (car  u) (caddr v)))
  64.         (- (* (car  u) (cadr  v)) (* (car  v) (cadr  u)))
  65.     )
  66. )
  67.  
  68. ;; Clockwise-p  -  Lee Mac
  69. ;; Returns T if p1,p2,p3 are clockwise oriented
  70.  
  71. (defun LM:Clockwise-p ( p1 p2 p3 )
  72.     (< (* (- (car  p2) (car  p1)) (- (cadr p3) (cadr p1)))
  73.        (* (- (cadr p2) (cadr p1)) (- (car  p3) (car  p1)))
  74.     )
  75. )

Jeremy

  • Guest
Re: Arcs in 3D
« Reply #3 on: July 22, 2012, 02:49:21 PM »
Thank you both! I'm writing a routine that draws polyhedra and one of the options is to draw the edges of the solid on the surface of a sphere, this will make life much easier. :-)