1 /*
2  * Copyright (c) 2016 Helmut Neemann
3  * Use of this source code is governed by the GPL v3 license
4  * that can be found in the LICENSE file.
5  */
6 package de.neemann.digital.draw.graphics;
7 
8 /**
9  * Implements a rotation and translation.
10  */
11 public class TransformRotate implements Transform {
12 
13     private final int sin;
14     private final int cos;
15     private final VectorInterface translation;
16 
17     /**
18      * Creates a new instance
19      *
20      * @param translation the translation
21      * @param rot         the rotation
22      */
TransformRotate(VectorInterface translation, int rot)23     public TransformRotate(VectorInterface translation, int rot) {
24         this.translation = translation;
25         switch (rot) {
26             case 1:
27                 sin = 1;
28                 cos = 0;
29                 break;
30             case 2:
31                 sin = 0;
32                 cos = -1;
33                 break;
34             case 3:
35                 sin = -1;
36                 cos = 0;
37                 break;
38             default:// 0
39                 sin = 0;
40                 cos = 1;
41                 break;
42         }
43     }
44 
45     @Override
transform(Vector v)46     public Vector transform(Vector v) {
47         return new Vector(v.getX() * cos + v.getY() * sin + translation.getX(),
48                 -v.getX() * sin + v.getY() * cos + translation.getY());
49     }
50 
51     @Override
transform(VectorFloat v)52     public VectorFloat transform(VectorFloat v) {
53         return new VectorFloat(v.getXFloat() * cos + v.getYFloat() * sin + translation.getXFloat(),
54                 -v.getXFloat() * sin + v.getYFloat() * cos + translation.getYFloat());
55     }
56 
57     @Override
getMatrix()58     public TransformMatrix getMatrix() {
59         return new TransformMatrix(cos, sin, -sin, cos, translation.getXFloat(), translation.getYFloat());
60     }
61 }
62