lilypond-devel
[Top][All Lists]
Advanced

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

Re: Simplify coord-rotate (issue 263690043 by address@hidden)


From: dak
Subject: Re: Simplify coord-rotate (issue 263690043 by address@hidden)
Date: Thu, 15 Oct 2015 12:46:52 +0000

Reviewers: thomasmorley651,

Message:
On 2015/10/15 12:34:17, thomasmorley651 wrote:
Please disregard my code from comment #1, it's fishy.

More appropiate would be (dropping the idea to rotate around other
ponts than
zero):

#(define (get-PI/4-rotated-quadrants radians)
   (cond ((>= radians TWO-PI)
          (get-PI/4-rotated-quadrants (- radians TWO-PI)))
         ((< radians 0)
          (get-PI/4-rotated-quadrants (+ radians TWO-PI )))
         (else
           (let* (;; get the octants
                  (oct (truncate (/ radians (/ PI 4)))))
             ;; get the rotated quadrants
             (cond ((= oct 7) 0)
                   ((even? oct)
                    (/ oct 2))
                   (else (/ (1+ oct) 2)))))))

(define-public (coord-rotate-p-II 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))
          (quadrant (get-PI/4-rotated-quadrants angle))
          (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)))))


Maybe in a follow up?
The patch itself LGTM

I prefer a followup since this is a pretty hefty change with a different
focus and warranting a separate Rietveld review.

Description:
Simplify coord-rotate

This does not fix the problem that (sin PI) is not really zero because
of the MPU working with higher precision than PI is specified, but at
least it makes the code nicer to read and modify.

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

Affected files (+7, -14 lines):
  M scm/lily-library.scm


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

-(define-public (coord-rotate coordinate degrees-in-radians)
-  (let*
-      ((coordinate
-        (cons
-         (exact->inexact (coord-x coordinate))
-         (exact->inexact (coord-y coordinate))))
-       (radius
-        (sqrt
-         (+ (* (coord-x coordinate) (coord-x coordinate))
-            (* (coord-y coordinate) (coord-y coordinate)))))
- (angle (angle-0-2pi (atan (coord-y coordinate) (coord-x coordinate)))))
-    (cons
-     (* radius (cos (+ angle degrees-in-radians)))
-     (* radius (sin (+ angle degrees-in-radians))))))
+(define-public (coord-rotate coordinate angle)
+  (let ((c (cos angle))
+        (s (sin angle))
+        (x (coord-x coordinate))
+        (y (coord-y coordinate)))
+    (cons (- (* c x) (* s y))
+          (+ (* s x) (* c y)))))

 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; trig





reply via email to

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