From a3cf02fac43090fff22c89313bd851c821476997 Mon Sep 17 00:00:00 2001 From: HORVILLE Ewen Date: Wed, 12 Oct 2022 13:29:36 +0200 Subject: [PATCH] =?UTF-8?q?TP02=20termin=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DEV 3.2/TP02/Fibonnaci/Fibbonaci.java | 4 +- DEV 3.2/TP02/Flocon/Flocon.java | 86 ++++++++++++++++++++++ DEV 3.2/TP02/Flocon/FloconPanel.java | 21 ++++++ DEV 3.2/TP02/Flocon/Vector2.java | 102 ++++++++++++++++++++++++++ DEV 3.2/TP02/Parité/Parity.java | 28 +++++++ DEV 3.2/TP02/Tableaux/Tableaux.java | 2 - 6 files changed, 239 insertions(+), 4 deletions(-) create mode 100644 DEV 3.2/TP02/Flocon/Flocon.java create mode 100644 DEV 3.2/TP02/Flocon/FloconPanel.java create mode 100644 DEV 3.2/TP02/Flocon/Vector2.java create mode 100644 DEV 3.2/TP02/Parité/Parity.java diff --git a/DEV 3.2/TP02/Fibonnaci/Fibbonaci.java b/DEV 3.2/TP02/Fibonnaci/Fibbonaci.java index bbb0ae4..800ba51 100644 --- a/DEV 3.2/TP02/Fibonnaci/Fibbonaci.java +++ b/DEV 3.2/TP02/Fibonnaci/Fibbonaci.java @@ -1,5 +1,4 @@ public class Fibbonaci { - public static int fibbo(int n, int r) { switch (n) { case 0: @@ -10,7 +9,8 @@ public class Fibbonaci { return fibbo(n-2, r) + fibbo(n-1, r); } } + public static void main(String[] args) { - + System.out.println(fibbo(Integer.parseInt(args[0]), 0)); } } diff --git a/DEV 3.2/TP02/Flocon/Flocon.java b/DEV 3.2/TP02/Flocon/Flocon.java new file mode 100644 index 0000000..7789f97 --- /dev/null +++ b/DEV 3.2/TP02/Flocon/Flocon.java @@ -0,0 +1,86 @@ +import java.awt.*; +import javax.swing.JFrame; + +public class Flocon extends Polygon { + + Vector2[] ptable; + + public Flocon() { + super(); + } + + private int floc(int n, int offset) { + if (n < 0) return offset+1; + Vector2 local = ptable[offset].toLocal(ptable[offset+1]); + local.div(3); + + Vector2 s1 = Vector2.add(ptable[offset], local); + ptable[offset+1] = s1; + + if (n > 0) { + offset = floc(n-1, offset); + } else offset++; + + Vector2 s2 = Vector2.add(s1, Vector2.rotate(local, -60)); + ptable[offset+1] = s2; + + if (n > 0) { + offset = floc(n-1, offset); + } else offset++; + + Vector2 s3 = Vector2.add(s2, Vector2.rotate(local, 60)); + ptable[offset+1] = s3; + + if (n > 0) { + offset = floc(n-1, offset); + } else offset++; + + Vector2 s4 = Vector2.add(s3, local); + ptable[offset+1] = s4; + + if (n > 0) { + offset = floc(n-1, offset); + } else offset++; + + return offset; + } + + public void createFloc(int x, int y, int r, int n) { + int size = 4; + for (int i = 0; i < n; i++) size = size * 4 - 3; + this.ptable = new Vector2[size]; + int offset = 0; + + ptable[0] = Vector2.rotate(new Vector2(0, -r),-60).add(new Vector2(x, y)); + ptable[1] = Vector2.rotate(new Vector2(0, -r), 60).add(new Vector2(x, y)); + offset = floc(n-1, offset); + + ptable[offset+1] = Vector2.rotate(new Vector2(0, -r), 180).add(new Vector2(x, y)); + offset = floc(n-1, offset); + + ptable[offset+1] = Vector2.rotate(new Vector2(0, -r), -60).add(new Vector2(x, y)); + floc(n-1, offset); + + + for (Vector2 v : ptable) { + addPoint(v.x, v.y); + } + } + + public static void main(String[] args) { + + + JFrame f = new JFrame(); + f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + f.setLocation(100, 100); + f.setSize(1200, 1200); + + Flocon p = new Flocon(); + p.createFloc(f.getWidth() / 2, f.getHeight() / 2 - 100, 500, 5); + + + f.add(new FloconPanel(p)); + + f.setVisible(true); + } +} diff --git a/DEV 3.2/TP02/Flocon/FloconPanel.java b/DEV 3.2/TP02/Flocon/FloconPanel.java new file mode 100644 index 0000000..8b1da14 --- /dev/null +++ b/DEV 3.2/TP02/Flocon/FloconPanel.java @@ -0,0 +1,21 @@ +import javax.swing.JPanel; +import java.awt.*; + +public class FloconPanel extends JPanel { + private Polygon p; + public FloconPanel(Polygon p) { + this.p = p; + } + + @Override + protected void paintComponent(Graphics g) { + Graphics gg = g; + if (this.isOpaque()) { + gg.setColor(this.getBackground()); + gg.fillRect(this.getX(), this.getY(), this.getWidth(), this.getHeight()); + } + + gg.setColor(Color.black); + gg.drawPolygon(p); + } +} diff --git a/DEV 3.2/TP02/Flocon/Vector2.java b/DEV 3.2/TP02/Flocon/Vector2.java new file mode 100644 index 0000000..f91c83d --- /dev/null +++ b/DEV 3.2/TP02/Flocon/Vector2.java @@ -0,0 +1,102 @@ +import java.awt.Point; + +public class Vector2 extends Point { + public Vector2(int x, int y) { + super(x, y); + } + + public Vector2 add(Point p) { + this.x += p.x; + this.y += p.y; + return this; + } + + public Vector2 sub(Point p) { + this.x -= p.x; + this.y -= p.y; + return this; + } + + public Vector2 mul(Point p) { + this.x *= p.x; + this.y *= p.y; + return this; + } + + public Vector2 mul(int n) { + this.x *= n; + this.y *= n; + return this; + } + + public Vector2 div(double n) { + this.x = (int)((double)this.x / n); + this.y = (int)((double)this.y / n); + return this; + } + + public static Vector2 add(Vector2 a, Vector2 b) { + return new Vector2(a.x + b.x, a.y + b.y); + } + + public static Vector2 rotate(Vector2 v, double a) { + a = Math.toRadians(a); + + double x1 = (double)v.x; + double y1 = (double)v.y; + return new Vector2((int)(Math.cos(a) * x1 - Math.sin(a) * y1), (int)(Math.sin(a) * x1 + Math.cos(a) * y1)); + } + + public void rotate(double a) { + a = Math.toRadians(a); + + double x1 = (double)this.x; + double y1 = (double)this.y; + this.x = (int)(Math.cos(a) * x1 - Math.sin(a) * y1); + this.y = (int)(Math.sin(a) * x1 + Math.cos(a) * y1); + } + + public static Vector2 lerp(Point a, Point b, double t) { + double ax = (double)a.x; + double bx = (double)b.x; + double ay = (double)a.y; + double by = (double)b.y; + + return new Vector2((int)(ax + (bx - ax) * t), (int)(ay + (by - ay) * t)); + } + + public void lerpTo(Point a, double t) { + double ax = (double)this.x; + double bx = (double)a.x; + double ay = (double)this.y; + double by = (double)a.y; + + this.x = (int)(ax + (bx - ax) * t); + this.y = (int)(ay + (by - ay) * t); + } + + public double length() { + double x = (double)this.x; + double y = (double)this.y; + + return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)); + } + + public void normalize() { + double x = (double)this.x; + double y = (double)this.y; + double length = this.length(); + + this.x = (int)(x / length); + this.y = (int)(y / length); + } + + public Vector2 toLocal(Point p) { + return new Vector2(p.x - this.x, p.y - this.y); + } + + @Override + public String toString() { + return "[" + this.x + ", " + this.y + "]"; + } +} diff --git a/DEV 3.2/TP02/Parité/Parity.java b/DEV 3.2/TP02/Parité/Parity.java new file mode 100644 index 0000000..acb036c --- /dev/null +++ b/DEV 3.2/TP02/Parité/Parity.java @@ -0,0 +1,28 @@ +public class Parity { + + public static boolean even(int n) { + switch (n) { + case 0: + return true; + case 1: + return false; + default: + return even(n-2); + } + } + + public static boolean odd(int n) { + switch (n) { + case 0: + return false; + case 1: + return true; + default: + return even(n-2); + } + } + + public static void main(String[] args) { + System.out.println(even(64)); + } +} diff --git a/DEV 3.2/TP02/Tableaux/Tableaux.java b/DEV 3.2/TP02/Tableaux/Tableaux.java index a86639e..d19bb54 100644 --- a/DEV 3.2/TP02/Tableaux/Tableaux.java +++ b/DEV 3.2/TP02/Tableaux/Tableaux.java @@ -1,5 +1,3 @@ -import java.util.Arrays; - public class Tableaux { public static int[] getTab(String[] args, int index, int[] tab) {