Code Red > AutoLISP (Vanilla / Visual)

Find the angle degree between three points

(1/2) > >>

Coder:
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:
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: ---;; Get Inside Angle  -  Lee Mac;; Returns the smaller angle subtended by three points with vertex at p2 (defun LM:GetInsideAngle ( p1 p2 p3 )    (   (lambda ( a ) (min a (- (+ pi pi) a)))        (rem (+ pi pi (- (angle p2 p1) (angle p2 p3))) (+ pi pi))    ))
Conversion to degrees is simple:


--- Code - Auto/Visual Lisp: ---(defun rtd ( r ) (* 180.0 (/ r pi)))

Lee Mac:
Or here's another using vector calculus:


--- Code - Auto/Visual Lisp: ---;; 3-Point Angle  -  Lee Mac;; Returns the smaller angle subtended by three points with vertex at p2 (defun LM:3pAngle ( p1 p2 p3 )    (   (lambda ( a b ) (acos (/ (vxv a b) (* (norm a) (norm b)))))        (mapcar '- p3 p2)        (mapcar '- p1 p2)    )) ;; Vector Dot Product  -  Lee Mac;; Args: u,v - vectors in R^n (defun vxv ( u v )    (apply '+ (mapcar '* u v))) ;; Vector Norm (R^n)  -  Lee Mac;; Args: v - vector in R^n (defun norm ( v )    (sqrt (apply '+ (mapcar '* v v)))) ;; ArcCosine  -  Lee Mac;; Args: -1 <= x <= 1 (defun acos ( x )    (if (<= -1.0 x 1.0)        (atan (sqrt (- 1.0 (* x x))) x)    ))

CAB:
Lets see if I can state the problem for the OP.
Is this what you are looking for?

CAB:
If so maybe this will get you started.  See drawing attached.


--- Code: ---(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)
)
--- End code ---

Navigation

[0] Message Index

[#] Next page

Go to full version