1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
void setup() {
size(768, 256);
}
float th = 0;
void draw() {
PImage img = loadImage("lena.png");
img.resize(256, 256);
image(img, 0, 0);
image(rotate_forward(img, radians(th)), 256, 0);
image(inverse_rotate(img, radians(th)), 512, 0);
}
void keyPressed() {
if (key == '+') {
th++;
} else if (key == '-') {
th--;
}
}
PImage rotate_forward(PImage I, float theta) {
PImage out = createImage(I.width, I.height, RGB);
out.loadPixels();
for (int i = 0; i < out.pixels.length; ++i) {
out.pixels[i] = color(0);
}
float x, y;
float u1, v1;
for (int u = 0; u < I.width; ++u) {
for (int v = 0; v < I.height; ++v) {
u1 = u-I.width/2;
v1 = v-I.height/2;
x = u1*cos(theta)-v1*sin(theta);
y = u1*sin(theta)+v1*cos(theta);
x+=I.width/2;
y+=I.height/2;
out.set(round(x), round(y), I.get(u, v));
}
}
out.updatePixels();
return out;
}
PImage inverse_rotate(PImage I, float theta) {
PImage out = createImage(I.width, I.height, RGB);
out.loadPixels();
for (int i = 0; i < out.pixels.length; ++i) {
out.pixels[i] = color(0);
}
float u, v;
float x1, y1;
for (int x = 0; x < I.width; ++x) {
for (int y = 0; y < I.height; ++y) {
x1 = x-I.width/2;
y1 = y-I.height/2;
u = x1*cos(theta)+y1*sin(theta);
v = -x1*sin(theta)+y1*cos(theta);
u+=I.width/2;
v+=I.height/2;
out.set(x, y, I.get(round(u), round(v)));
}
}
out.updatePixels();
return out;
}
|