lilypond-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

## modify coord-rotate to get exact values for (sin PI) etc (issue 26953004

 From: thomasmorley65 Subject: modify coord-rotate to get exact values for (sin PI) etc (issue 269530043 by address@hidden) Date: Mon, 19 Oct 2015 23:15:04 +0000

```Reviewers: ,

Message:
please review

Description:
modify coord-rotate to get exact values for (sin PI) etc

issue 4640

Done by switching to appropiate values for the angle and/or switching
sin to cos and vice versa

Please review this at https://codereview.appspot.com/269530043/

Affected files (+41, -5 lines):
M scm/lily-library.scm

Index: scm/lily-library.scm
diff --git a/scm/lily-library.scm b/scm/lily-library.scm
```
index 0bfce9eb0c8a748099b3af053cf29d822de8a4b6..27be8220300eb3b5a8cc5f5eccdf55a2d610a960 100644
```--- a/scm/lily-library.scm
+++ b/scm/lily-library.scm
@@ -707,11 +707,29 @@ right (@var{dir}=+1)."
(define-public (coord-scale coordinate amount)
(coord-operation * amount coordinate))

-(define-public (coord-rotate coordinate angle)
-  (let ((c (cos angle))
-        (s (sin angle))
-        (x (coord-x coordinate))
-        (y (coord-y coordinate)))
+(define-public (coord-rotate coordinate angle-in-radians)
+  ;; getting around (sin PI) not being exactly zero by switching to cos at
```
+ ;; appropiate angles and/or taking the negative value (vice versa for cos)
```+  (let* ((angle (angle-0-2pi angle-in-radians))
+         (octant (get-octants angle))
+         ;; get the PI/4 rotated quadrants
+         (quadrant
+           (cond ((= octant 7) 0)
+                 ((even? octant) (/ octant 2))
+                 (else (/ (1+ octant) 2))))
+         (moved-angle (- angle (/ (* quadrant PI) 2)))
+         (cm (cos moved-angle))
+         (sm (sin moved-angle))
+         (c (cond ((= 1 quadrant) (- sm))
+                  ((= 2 quadrant) (- cm))
+                  ((= 3 quadrant) sm)
+                  (else cm)))
+         (s (cond ((= 1 quadrant) cm)
+                  ((= 2 quadrant) (- sm))
+                  ((= 3 quadrant) (- cm))
+                  (else sm)))
+         (x (coord-x coordinate))
+         (y (coord-y coordinate)))
(cons (- (* c x) (* s y))
(+ (* s x) (* c y)))))

```
@@ -768,6 +786,24 @@ as rectangular coordinates @ode{(x-length . y-length)}."
```      (real-part complex)
(imag-part complex))))

+(define (get-octants radians)
+  "Return octant of @var{radians}"
+  ;; 0PI/4 >= radians > 1PI/4  ->0
+  ;; 1PI/4 >= radians > 2PI/4  ->1
+  ;; 2PI/4 >= radians > 3PI/4  ->2
+  ;; 3PI/4 >= radians > 4PI/4  ->3
+  ;; 4PI/4 >= radians > 5PI/4  ->4
+  ;; 5PI/4 >= radians > 6PI/4  ->5
+  ;; 6PI/4 >= radians > 7PI/4  ->6
+  ;; 7PI/4 >= radians > 8PI/4  ->0
+  ;;
+  ;; other radians are transformed to fit into TWO-PI-range
+  (cond ((>= radians TWO-PI)
+         (get-octants (- radians TWO-PI)))
+        ((< radians 0)
+         (get-octants (+ radians TWO-PI )))
+        (else (truncate (/ radians (/ PI 4))))))
+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; string

```

reply via email to

 [Prev in Thread] Current Thread [Next in Thread]