Thread: get angle between two points

1. get angle between two points

Registered forum members do not see this ad.

after searching around I finally found the following code to find the angle between two points:

Code:
(setq p1 (getpoint "\nSelect First Point: "))
(setq p2 (getpoint "\nSelect Second Point: "))
(setq ang1 (angle p1 p2))
However, when I test this by either priting ang1, or by adding the following line of code for text,
Code:
(command "_text" "_J" "_tc" "_non" p1 6.0 ang1 "testing angle")
the angle seems to only vary from .5 to about 2.0 and never matches what you'd expect from the p1 and p2 angle (perhaps 48 or 89 degrees).
Could it be that the angle it's grabbing is in some other form and I need to convert it?

2. Please pay attention that ANGLE function return the angle in radians, while that command is expecting it in degrees.

3. That was it, thanks!!!

Since you will encounter such case many times from now, I strongly suggest you to define a pair of dedicated functions for Radians-to-Degrees, respectively Degrees-to-Radians conversion.

5. Heres the two rtd dtr you want also you need to remember that whilst the answer will always be radians it also ignores the UNITS direction setting so 0 is always horizontal to right so sometimes need a bit extra to take into account the north brg as up page at 0.

Code:
;The dtr function converts degrees to radians
;The rtd function converts radians to degrees
(defun dtr (a)
(* pi (/ a 180.0))
)
;
(defun rtd (a)
(/ (* a 180.0) pi)
)

6. Building on Mircea and Bigal 's suggestions and answers .

Code:
(command "_.text" "_J" "_tc" "_non" p1 6.0 (/ (* ang1 180.0) pi) "testing angle")

7. Factors you need to consider when using command approach
Current UCS
Code:
(setq p1 (getpoint "\nSelect First Point: "))
(setq p2 (getpoint p1 "\nSelect Second Point: "))
(setq ang1 (angle (trans p1 1 0)
(trans p2 1 0)))
Current textsyle
If Textstyle height is defined on the current TEXTSYLE, instead of "testing angle" you will get the angle value for string and 6.0 as the angle

HTH

8. Registered forum members do not see this ad.

Originally Posted by pBe
If Textstyle height is defined on the current TEXTSYLE, instead of "testing angle" you will get the angle value for string and 6.0 as the angle
That's a good point, pBe; a possible workaround:
Code:
(command "_TEXT" textPoint)
(if (= (cdr (assoc 40 (entget (tblobjname "STYLE" (getvar "TEXTSTYLE"))))) 0.0)
(command textHeight)
)
(command textAngle textLabel)

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts