Author Topic: Working with angles  (Read 4200 times)

0 Members and 1 Guest are viewing this topic.

velasquez

  • Newt
  • Posts: 195
Working with angles
« on: June 06, 2014, 02:18:31 PM »
Hi
I'm using the tool below to check whether the angle of p1 and p2 is in the first or fourth quadrant.
Code: [Select]
(defun pitest (p1 p2)
  (setq ang (angle (trans p1 1 0) (trans p2 1 0)))
  (if
    (or (> ang (/ (* 3 pi) 2)) (< ang (/ pi 2)))
     4 ;_ right
     6 ;_ left
  ) ;_ fim de if
) ;_ fim de defun

I will use the result to determine the alignment of a text.
Can anyone tell me if there is a better way for this

Thanks


ymg

  • Guest
Re: Working with angles
« Reply #1 on: June 06, 2014, 03:39:42 PM »
Code: [Select]
;; Make Angle Readable                                                        ;
 (defun MakeReadable (a)
     (setq a (rem (+ a pi pi) (+ pi pi)))
     (rem (if (< (* pi 0.5) a (* pi 1.5))(+ a pi) a) (+ pi pi)) 
  )

Use the above, you supply the angle for your text and it applies the correction
to keep it in a readable orientation.

velasquez

  • Newt
  • Posts: 195
Re: Working with angles
« Reply #2 on: June 06, 2014, 04:54:37 PM »
Code: [Select]
;; Make Angle Readable                                                        ;
 (defun MakeReadable (a)
     (setq a (rem (+ a pi pi) (+ pi pi)))
     (rem (if (< (* pi 0.5) a (* pi 1.5))(+ a pi) a) (+ pi pi)) 
  )

Use the above, you supply the angle for your text and it applies the correction
to keep it in a readable orientation.

Thanks ymg.
I did not explain well.
I'll use the code during a drag operation with grread.
The text should be horizontal and always left or right of the line.

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: Working with angles
« Reply #3 on: June 07, 2014, 04:12:18 AM »
Hi
I'm using the tool below to check whether the angle of p1 and p2 is in the first or fourth quadrant.
Code: [Select]
(defun pitest (p1 p2)
  (setq ang (angle (trans p1 1 0) (trans p2 1 0)))
  (if
    (or (> ang (/ (* 3 pi) 2)) (< ang (/ pi 2)))
     4 ;_ right
     6 ;_ left
  ) ;_ fim de if
) ;_ fim de defun

I will use the result to determine the alignment of a text.
Can anyone tell me if there is a better way for this

Thanks

Code: [Select]
(defun pitest1 (p1 p2)
  (if (< (car p1) (car p2)) 4  6)
)
Call it with WCS points

hanhphuc

  • Newt
  • Posts: 64
Re: Working with angles
« Reply #4 on: June 07, 2014, 05:04:28 AM »
;dear
;my version is used for block insertion with vertical & horizontal annotations
;which i made 4 readable annotation blocks for 4 different quadrants, then i used the code to suit the best insertion angle.

;i just test with ymj's code, there differences of orientation with mine.
;thank you

;hp#009 07/06/2014
Code: [Select]

;MAR by hanhphuc

;thanx roy
(defun MAR (rad ) ; argument: rad, radians 'REAL
  (if(< rad 0.)(setq rad (+ (* pi 2.) rad)))
(+(rem (fix rad) (/ pi 2))(- rad (fix rad)))
)

;; Make Angle Readable by: ymg                                                     ;
 (defun MakeReadable (a)
     (setq a (rem (+ a pi pi) (+ pi pi)))
     (rem (if (< (* pi 0.5) a (* pi 1.5))(+ a pi) a) (+ pi pi)) 
  )


(defun piTEST (sym inc / dtor rtod) ; argument: sym=function , inc= Increment, 'REAL to fill a circle in degree

(setq dtor (lambda (deg /)(* pi (/ deg 180.)))
rtod (lambda (rad /)(/ (* rad 180.) pi )))

 
  (princ "\nFrom Deg | Rad --> Rad | To Degrees")
  (foreach x
             (reverse
               ((lambda (i / a b)
                  (setq a 0.)
                  (while (> (* 2 pi) (abs(setq a (+ (dtor i) a ))))
                    (setq b (cons a b))
                  ) ;_  while
                ) ;_  lambda
                                                  inc
               );
             ) ;_  reverse
    (princ (strcat "\n"
                   (rtos (rtod x))
                   " "
                   (rtos x)
                   "-->"
                   (rtos ((eval sym) x))
                   " "
                   (rtos (rtod ((eval sym) x)))
           ) ;_  strcat
    ) ;_  princ

  ) ;_  foreach
  (princ)
) ;_  defun

Test:
(piTEST MakeReadable 30); <-- ymg
(piTEST MakeReadable -30)

;Test result:
Code: [Select]

(piTEST MakeReadable 30) ; Range min 30 -> max 330
;|
Input Deg | Rad --> Rad | Output Deg
30.0000 0.5236-->0.5236 30.0000
60.0000 1.0472-->1.0472 60.0000
90.0000 1.5708-->1.5708 90.0000
120.0000 2.0944-->5.2360 300.0000
150.0000 2.6180-->5.7596 330.0000
180.0000 3.1416-->0.0000 0.0000
210.0000 3.6652-->0.5236 30.0000
240.0000 4.1888-->1.0472 60.0000
270.0000 4.7124-->4.7124 270.0000
300.0000 5.2360-->5.2360 300.0000
330.0000 5.7596-->5.7596 330.0000
|;

(piTEST MakeReadable -30); Range min 30 -> max 330
;|
Input Deg | Rad --> Rad | Output Deg
-30.0000 -0.5236-->5.7596 330.0000
-60.0000 -1.0472-->5.2360 300.0000
-90.0000 -1.5708-->4.7124 270.0000
-120.0000 -2.0944-->1.0472 60.0000
-150.0000 -2.6180-->0.5236 30.0000
-180.0000 -3.1416-->0.0000 0.0000
-210.0000 -3.6652-->5.7596 330.0000
-240.0000 -4.1888-->5.2360 300.0000
-270.0000 -4.7124-->1.5708 90.0000
-300.0000 -5.2360-->1.0472 60.0000
-330.0000 -5.7596-->0.5236 30.0000
|;

(piTEST MAR 30.); <-- hanhphuc
(piTEST MAR -30.);

;Test result:
Code: [Select]
(piTEST MAR 30); Range min 30 -> max 120
;|
Input Deg | Rad --> Rad | Output Deg
30.0000 0.5236-->2.0944 120.0000
60.0000 1.0472-->1.0472 60.0000
90.0000 1.5708-->1.5708 90.0000
120.0000 2.0944-->0.5236 30.0000
150.0000 2.6180-->1.0472 60.0000
180.0000 3.1416-->1.5708 90.0000
210.0000 3.6652-->2.0944 120.0000
240.0000 4.1888-->1.0472 60.0000
270.0000 4.7124-->1.5708 90.0000
300.0000 5.2360-->0.5236 30.0000
330.0000 5.7596-->1.0472 60.0000
|;

(piTEST MAR -30); Range min 30 -> max 120
;|
Input Deg | Rad --> Rad | Output Deg
-30.0000 -0.5236-->1.0472 60.0000
-60.0000 -1.0472-->0.5236 30.0000
-90.0000 -1.5708-->1.5708 90.0000
-120.0000 -2.0944-->1.0472 60.0000
-150.0000 -2.6180-->2.0944 120.0000
-180.0000 -3.1416-->1.5708 90.0000
-210.0000 -3.6652-->1.0472 60.0000
-240.0000 -4.1888-->0.5236 30.0000
-270.0000 -4.7124-->1.5708 90.0000
-300.0000 -5.2360-->1.0472 60.0000
-330.0000 -5.7596-->0.5236 30.0000
  |;
« Last Edit: June 07, 2014, 05:57:52 AM by hanhphuc »
( apply 'equal "hp" "happy" "hạnh phúc" "ハッピー" "幸福" "행복" ) ; error: too many arguments

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Working with angles
« Reply #5 on: June 07, 2014, 05:23:31 AM »
@ hanhphuc:
You have this in your code (code reformatted for clarity):
Code: [Select]
(if (< rad 0.0)
  (setq a (+ ( * pi 2.0) rad))
  (setq a (+ ( * pi 2.0) rad))
)
?


roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Working with angles
« Reply #6 on: June 07, 2014, 05:39:19 AM »
... And:
Code: [Select]
(MAR (* 0.6 pi)) => 0.314159Should be: 5.02655

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Working with angles
« Reply #7 on: June 07, 2014, 05:47:22 AM »
My version:
Code: [Select]
(defun readableAngle_2 (ang)
  (setq ang (rem (+ (rem ang (+ pi pi)) pi pi) (+ pi pi)))
  (cond
    ((< (* pi 0.5) ang pi)
      (+ ang pi)
    )
    ((<= pi ang (* pi 1.5))
      (- ang pi)
    )
    (T
      ang
    )
  )
)

Improved version of my previous function:
http://www.theswamp.org/index.php?topic=46728.msg517862#msg517862

hanhphuc

  • Newt
  • Posts: 64
Re: Working with angles
« Reply #8 on: June 07, 2014, 05:54:22 AM »
@ hanhphuc:
You have this in your code (code reformatted for clarity):
Code: [Select]
(if (< rad 0.0)
  (setq a (+ ( * pi 2.0) rad))
  (setq a (+ ( * pi 2.0) rad))
)
?

@hi Roy
:-o thanx for pointing my mistake, maybe it was copied from my old code which degrees input.

thank you roy.
« Last Edit: June 07, 2014, 07:32:57 AM by hanhphuc »
( apply 'equal "hp" "happy" "hạnh phúc" "ハッピー" "幸福" "행복" ) ; error: too many arguments

RAIN CODE

  • Guest
Re: Working with angles
« Reply #9 on: June 07, 2014, 06:01:44 AM »
Hi
I'm using the tool below to check whether the angle of p1 and p2 is in the first or fourth quadrant.
Code: [Select]
(defun pitest (p1 p2)
  (setq ang (angle (trans p1 1 0) (trans p2 1 0)))
  (if
    (or (> ang (/ (* 3 pi) 2)) (< ang (/ pi 2)))
     4 ;_ right
     6 ;_ left
  ) ;_ fim de if
) ;_ fim de defun

I will use the result to determine the alignment of a text.
Can anyone tell me if there is a better way for this

Thanks

velasquez

  • Newt
  • Posts: 195
Re: Working with angles
« Reply #10 on: June 07, 2014, 08:39:41 AM »
Many thanks for all the answers.
They helped a lot.

hanhphuc

  • Newt
  • Posts: 64
Re: Working with angles
« Reply #11 on: June 07, 2014, 09:41:20 AM »
@velasquez i apologize if my result didn't make sense?
actually the difference is due to relatively Clock-Wise & angbase.
example: North clockwise 30⁰ equals East Anti-ClockWise 60⁰,
and, 150⁰ & 60⁰ East_ACW vs 330 & 60⁰ North_CW are both readable.

so i prefer using theirs code for this "make-angle-readable" topic  :-)
thanks again roy & ymg
( apply 'equal "hp" "happy" "hạnh phúc" "ハッピー" "幸福" "행복" ) ; error: too many arguments

velasquez

  • Newt
  • Posts: 195
Re: Working with angles
« Reply #12 on: June 07, 2014, 09:56:49 AM »
@velasquez i apologize if my result didn't make sense?
actually the difference is due to relatively Clock-Wise & angbase.
example: North clockwise 30⁰ equals East Anti-ClockWise 60⁰,
and, 150⁰ & 60⁰ East_ACW vs 330 & 60⁰ North_CW are both readable.

so i prefer using theirs code for this "make-angle-readable" topic  :-)
thanks again roy & ymg

You do not have to apologize, all responses were important for what I need to learn.
Thanks

chlh_jd

  • Guest
Re: Working with angles
« Reply #13 on: June 08, 2014, 11:25:13 AM »
For text I used
Code - Auto/Visual Lisp: [Select]
  1. (setq _2pi 6.283185307179586476925286766559
  2.       _3pi2 4.7123889803846898576939650749193
  3.       _pi2 1.5707963267948966192313216916395
  4.       _pi3 1.047197551196597412820881124426
  5.       _pi4 0.7853981633974483096156608458197      
  6.       _1d 0.01745329251994329576923690768489
  7.       _r 0.57721566490153286060651209008240243104215933593992)
  8.  
  9. (defun ang<pi2 (ang)
  10.   (setq ang (rem ang _2pi)
  11.         ang (cond ((minusp ang) (+ ang _2pi))(ang)))
  12.   (cond ((equal ang _pi2 1e-3) _pi2)
  13.         ((equal ang pi 1e-3) 0.0)
  14.         ((equal ang _3pi2 1e-3) _pi2)
  15.         ((> pi ang _pi2) (+ ang pi))
  16.         ((>= _3pi2 ang pi) (- ang pi))
  17.         (ang)))
  18.