TP02 terminé

This commit is contained in:
HORVILLE 2022-10-12 13:29:36 +02:00
parent 7f720447c3
commit a3cf02fac4
6 changed files with 239 additions and 4 deletions

View File

@ -1,5 +1,4 @@
public class Fibbonaci { public class Fibbonaci {
public static int fibbo(int n, int r) { public static int fibbo(int n, int r) {
switch (n) { switch (n) {
case 0: case 0:
@ -10,7 +9,8 @@ public class Fibbonaci {
return fibbo(n-2, r) + fibbo(n-1, r); return fibbo(n-2, r) + fibbo(n-1, r);
} }
} }
public static void main(String[] args) { public static void main(String[] args) {
System.out.println(fibbo(Integer.parseInt(args[0]), 0));
} }
} }

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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 + "]";
}
}

View File

@ -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));
}
}

View File

@ -1,5 +1,3 @@
import java.util.Arrays;
public class Tableaux { public class Tableaux {
public static int[] getTab(String[] args, int index, int[] tab) { public static int[] getTab(String[] args, int index, int[] tab) {