summaryrefslogtreecommitdiff
path: root/Year_2/IandM
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2021-02-17 21:52:26 +0100
committerSanto Cariotti <santo@dcariotti.me>2021-02-17 21:52:26 +0100
commitd04b4f36115e9b4e89b0b6f33c4ae53718d2eb48 (patch)
tree93044c5aef0634b2b94999f87d4b5b91b2610e8f /Year_2/IandM
parent321b268bb74512289f9eb361560e8ceb9d30fe59 (diff)
i&m: add rotation algorithm
Diffstat (limited to 'Year_2/IandM')
-rw-r--r--Year_2/IandM/rotation/data/lena.pngbin0 -> 473831 bytes
-rw-r--r--Year_2/IandM/rotation/rotation.pde75
2 files changed, 75 insertions, 0 deletions
diff --git a/Year_2/IandM/rotation/data/lena.png b/Year_2/IandM/rotation/data/lena.png
new file mode 100644
index 0000000..59ef68a
--- /dev/null
+++ b/Year_2/IandM/rotation/data/lena.png
Binary files differ
diff --git a/Year_2/IandM/rotation/rotation.pde b/Year_2/IandM/rotation/rotation.pde
new file mode 100644
index 0000000..33f79ad
--- /dev/null
+++ b/Year_2/IandM/rotation/rotation.pde
@@ -0,0 +1,75 @@
+void setup() {
+ size(768, 256);
+ PImage img = loadImage("lena.png");
+ img.resize(256, 256);
+ img.filter(GRAY);
+
+ image(img, 0, 0);
+ image(rotate_forward(img, radians(45)), 256, 0);
+
+ image(inverse_rotate(img, radians(45)), 512, 0);
+}
+
+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;
+}