Jump to content

Collinear Angles


j2lstaples

Recommended Posts

It's intuitive to know that 90 degrees and 270 degrees are collinear assuming the same reference point and 0 degrees.  It gets harder to test for other angles though. This gets even more frustrating to test if your angle is negative, or more than 2*pi in radians. 

 

;********************************************************;
;; MA:str8_ang - Check if two angles are straight (collinear)
;; Arguments:
;; - ang1 (float): First angle in radians
;; - ang2 (float): Second angle in radians
;; Returns:
;; - test (bool): True if the angles are collinear, False otherwise
;; Description:
;; This function checks if two angles are collinear (straight) by calculating the cross product of their corresponding unit vectors.
;; The provided angles are translated to positive equivalents and normalized to the range of 0 to 2π.
;; The function then calculates the unit vectors u and v from the angles and calculates their cross product.
;; If the cross product is nearly zero (within a tolerance), the angles are considered collinear.
;; The function returns True if the angles are collinear, and False otherwise.
;; Usage: (MA:str8_ang ang1 ang2)
(defun MA:str8_ang (ang1 ang2 / u1 u2 u3 v1 v2 v3 cross_prod test) 
  (if (and ang1 ang2) 
    (progn 
      ;; Translate negative angles to positive equivalents
      (if (< ang1 0) 
        (setq ang1 (+ (* 2 pi) (rem ang1 (- (* 2 pi)))))
      )
      (if (< ang2 0) 
        (setq ang2 (+ (* 2 pi) (rem ang2 (- (* 2 pi)))))
      )

      ;; Normalize angles to the range of 0 to 2π
      (setq ang1 (rem ang1 (* 2 pi)))
      (setq ang2 (rem ang2 (* 2 pi)))

      (setq u1 (cos ang1)
            u2 (sin ang1)
            u3 0.0
            v1 (cos ang2)
            v2 (sin ang2)
            v3 0.0
      )
      (setq cross_prod (list (- (* u2 v3) (* u3 v2)) 
                             (- (* u3 v1) (* u1 v3))
                             (- (* u1 v2) (* u2 v1))
                       )
      )
      (if 
        (and (= (car cross_prod) 0.0) 
             (= (cadr cross_prod) 0.0)
             (< (abs (caddr cross_prod)) 0.005)
        )
        (setq test T)
        (setq test nil)
      )
      test ; return the test result
    ) ; end progn
  ) ; end if
)

 

This basically outputs T if the two angles have the same angle or reflection of each other.

 

Do you guys have an easier way of checking this? A refactor would be nice.

Link to comment
Share on other sites

And from polyface.de ?

 

Exemple:

;;functions VectorProduct & collinear 
;;Armin Antkowiak, Berlin
;;http://www.polyface.de/general.html
;;mailto:info@polyface.de
(defun vectorProduct (v1 v2 / )
	(list
		(- (* (cadr  v1) (caddr v2)) (* (caddr v1) (cadr  v2)))
		(- (* (caddr v1) (car   v2)) (* (car   v1) (caddr v2)))
		(- (* (car   v1) (cadr  v2)) (* (cadr  v1) (car   v2)))
	)
)
(defun collinear (p1 p2 p3 p4 / tol)
	(setq tol 1E-12)
	(equal
		'(0.0 0.0 0.0)
		(vectorProduct (mapcar '- p2 p1) (mapcar '- p3 p4))
		tol
	)
)
;*********************************
(defun c:colineaire ( / e1 e2 pt1 pt2 pt3 pt4)
	(setq e1 (entget (car (entsel "\nSelect first line: "))))
	(setq e2 (entget (car (entsel "\nSelect second line: "))))
	(setq
		pt1 (cdr (assoc 10 e1))
		pt2 (cdr (assoc 11 e1))
		pt3 (cdr (assoc 10 e2))
		pt4 (cdr (assoc 11 e2))
	)
	(if (collinear pt1 pt2 pt3 pt4)
		(princ "\nLine are collinear")
		(princ "\nLine aren't collinear")
	)
	(princ)
)

 

Link to comment
Share on other sites

(defun collinear-p ( p1 p p2 )
  (equal (distance p1 p2) (+ (distance p1 p) (distance p p2)) 1e-6)
)

 

;; Collinear-p  -  Lee Mac
;; Returns T if p1,p2,p3 are collinear

(defun LM:Collinear-p ( p1 p2 p3 )
  (
    (lambda ( a b c )
      (or
        (equal (+ a b) c 1e-8)
        (equal (+ b c) a 1e-8)
        (equal (+ c a) b 1e-8)
      )
    )
    (distance p1 p2) (distance p2 p3) (distance p1 p3)
  )
)

 

Edited by marko_ribar
Link to comment
Share on other sites

  • 3 weeks later...
On 7/16/2023 at 6:31 AM, Tsuky said:

And from polyface.de ?

 

 

I don't even know that website. Nobody owns Linear Algebra afaik. Maybe I should get some good code from there as well. Seems neat and nice. Thanks for sharing.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...