Author Topic: Find the angle degree between three points  (Read 9551 times)

0 Members and 1 Guest are viewing this topic.

Coder

  • Swamp Rat
  • Posts: 827
Find the angle degree between three points
« on: May 02, 2012, 06:41:30 AM »
Hello

I am not doing good at maths , and I need to find the angle degree ( or radians ) between tow lines to insert an elbow ( Block ) at the angle
according to its correct degree .

Hope that make sense .

Thank you all

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Find the angle degree between three points
« Reply #1 on: May 02, 2012, 06:45:57 AM »
Of course, there are two angles between three points, the sum of which equals 2pi rads (360 degs). The following function will return the smaller of these angles (in radians):

Code - Auto/Visual Lisp: [Select]
  1. ;; Get Inside Angle  -  Lee Mac
  2. ;; Returns the smaller angle subtended by three points with vertex at p2
  3.  
  4. (defun LM:GetInsideAngle ( p1 p2 p3 )
  5.     (   (lambda ( a ) (min a (- (+ pi pi) a)))
  6.         (rem (+ pi pi (- (angle p2 p1) (angle p2 p3))) (+ pi pi))
  7.     )
  8. )

Conversion to degrees is simple:

Code - Auto/Visual Lisp: [Select]
  1. (defun rtd ( r ) (* 180.0 (/ r pi)))

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Find the angle degree between three points
« Reply #2 on: May 02, 2012, 06:59:11 AM »
Or here's another using vector calculus:

Code - Auto/Visual Lisp: [Select]
  1. ;; 3-Point Angle  -  Lee Mac
  2. ;; Returns the smaller angle subtended by three points with vertex at p2
  3.  
  4. (defun LM:3pAngle ( p1 p2 p3 )
  5.     (   (lambda ( a b ) (acos (/ (vxv a b) (* (norm a) (norm b)))))
  6.         (mapcar '- p3 p2)
  7.         (mapcar '- p1 p2)
  8.     )
  9. )
  10.  
  11. ;; Vector Dot Product  -  Lee Mac
  12. ;; Args: u,v - vectors in R^n
  13.  
  14. (defun vxv ( u v )
  15.     (apply '+ (mapcar '* u v))
  16. )
  17.  
  18. ;; Vector Norm (R^n)  -  Lee Mac
  19. ;; Args: v - vector in R^n
  20.  
  21. (defun norm ( v )
  22.     (sqrt (apply '+ (mapcar '* v v)))
  23. )
  24.  
  25. ;; ArcCosine  -  Lee Mac
  26. ;; Args: -1 <= x <= 1
  27.  
  28. (defun acos ( x )
  29.     (if (<= -1.0 x 1.0)
  30.         (atan (sqrt (- 1.0 (* x x))) x)
  31.     )
  32. )

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Find the angle degree between three points
« Reply #3 on: May 02, 2012, 10:14:19 AM »
Lets see if I can state the problem for the OP.
Is this what you are looking for?
« Last Edit: May 02, 2012, 06:00:21 PM by CAB »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Find the angle degree between three points
« Reply #4 on: May 02, 2012, 06:02:41 PM »
If so maybe this will get you started.  See drawing attached.

Code: [Select]
(defun c:Insert90 (/ p1 p2 p3 BlkName)
 
  (setq BlkName "ELL90deg"
        PipeSize 6.0)

  (defun lm:getinsideangle (p1 p2 p3)
    ((lambda (a) (min a (- (+ pi pi) a)))
      (rem (+ pi pi (- (angle p2 p1) (angle p2 p3))) (+ pi pi))
    )
  )

  ;;  Convert  Radians to Degrees
  (defun rtod (r) (* 180.0 (/ r pi)))
 
  ;;D. C. Broad, Jr.
  ;;(sideof <ray-origin> <another-point-on-ray> <point-to-be-tested>)
  (defun sideof (p1 p2 p / r)
    (setq r (cond
              ((equal p1 p 1e-10) 0)
              (t (sin (- (angle p1 p) (angle p1 p2))))
            )
    )
    (if (equal r 0 1e-10) 0 r)
  )
  ;;return values
  ;;negative = point is to the right side of the ray
  ;;0 = point is on the ray
  ;;otherwise point is on the left side of the ray.
  ;;P1 should not equal P2 for meaningful results.

  (if (and
        (setq p1 (getpoint "\nPick first point."))
        (setq p2 (getpoint "\nPick intersecting point."))
        (setq p3 (getpoint "\nPick third point."))
      )
    (cond
      ((or (zerop (setq result (sideof p1 p2 p3)))
           (not (equal (lm:getinsideangle p1 p2 p3) (/ pi 2) 0.001))
       )
       (princ "\nError - not 90 degrees")
      )
      ((minusp result) ; right turn ell
       (command "-insert" BlkName "_non" p2 PipeSize "" (rtod(angle p3 p2)))
      )
      (t ; Left turn ell
       (command "-insert" BlkName "_non" p2 PipeSize "" (rtod(angle p1 p2)))
      )
    )
  )
  (princ)
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Coder

  • Swamp Rat
  • Posts: 827
Re: Find the angle degree between three points
« Reply #5 on: May 03, 2012, 12:23:50 AM »
Hi .

Lee , that's really nice , and it works very good . But I can't understand what the second codes for (post #2) ?  :-)
They seem very hard to me .

CAB , that's exactly what I am after , and I would like to understand how codes are running ( to understand coding ) . :-) 

Many thanks .

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Find the angle degree between three points
« Reply #6 on: May 03, 2012, 06:19:09 AM »
Lee , that's really nice , and it works very good . But I can't understand what the second codes for (post #2) ?  :-)
They seem very hard to me .

The function in post#2 is solving the problem using vector calculus, rather than the in-built AutoLISP angle function, which is dependent on the current construction plane.

The function merely uses the definition of the Vector Dot Product:

Code: [Select]
a·b = |a||b|cosθ
rearranged for theta:

Code: [Select]
θ = acos(a·b/|a||b|)