diff --git a/tp6/.classpath b/tp6/.classpath new file mode 100644 index 0000000000000000000000000000000000000000..4a0e6a5053a5ac8bd9d63c12bff1f02fee03ea8d --- /dev/null +++ b/tp6/.classpath @@ -0,0 +1,11 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="src" path=""/> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"> + <attributes> + <attribute name="module" value="true"/> + </attributes> + </classpathentry> + <classpathentry kind="lib" path="/Users/teobk/programmation2/tp6/tp2.jar"/> + <classpathentry kind="output" path=""/> +</classpath> diff --git a/tp6/.gitignore b/tp6/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..1612b0f2bf88a5c78218da43f0865af02660f94e --- /dev/null +++ b/tp6/.gitignore @@ -0,0 +1,6 @@ +/Main.class +/Point.class +/Rectangle.class +/Shape.class +/Triangle.class +/Turtle.class diff --git a/tp6/.project b/tp6/.project new file mode 100644 index 0000000000000000000000000000000000000000..9b0e9f6cdd841c468f140fc5ea2fa6abc45a66c3 --- /dev/null +++ b/tp6/.project @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>tp6</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.jdt.core.javanature</nature> + </natures> +</projectDescription> diff --git a/tp6/Main.java b/tp6/Main.java new file mode 100755 index 0000000000000000000000000000000000000000..b0cc438661740208115348a602c9d5a328ce55a1 --- /dev/null +++ b/tp6/Main.java @@ -0,0 +1,103 @@ +/** + * Created by Arnaud Labourel on 20/09/2018. + */ + +import tp2.lib.Painter; + +import java.awt.Color; +import java.util.Random; + +import static tp2.lib.Tools.sleep; + +public class Main { + static final int WIDTH = 800; + static final int HEIGHT = 600; + + + public static void main(String[] args){ + Random random= new Random(); + Painter painter = new Painter(WIDTH, HEIGHT); + + + // Point.test1_Point(painter); + // Point.test2_Point(painter); + + //Rectangle.test_rectangle(painter); + // dessin_Roue(); + // drawFractale(); + test_turtle(); + + } + + public static void dessin_Roue() { + int numberOfPoint = 10; + + Painter painter = new Painter(WIDTH,HEIGHT); + Point center = new Point(WIDTH/2,HEIGHT/2 ); + Point point = new Point(100,0); + for (int i = 0; i < numberOfPoint; i++) { + Point p1 = point.rotate((360/numberOfPoint)*i); + Point nextPoint = point.rotate((360/numberOfPoint)*(i+1)); + + p1 = p1.translate(center.x,center.y); + nextPoint = nextPoint.translate(center.x,center.y); + + nextPoint.draw(painter,Color.red); + + center.drawLine(p1,painter,Color.blue); + p1.drawLine(nextPoint,painter,Color.black); + + + } + + } + + public static void drawSquare(Turtle turtle, int size) { + for (int i = 0; i < 4; i++) { + turtle.moveForward(size); + turtle.turnLeft(90); + } + } + + + public static void test_turtle() { + Turtle turtle = new Turtle(800,600); + turtle.setColor(Color.black); + turtle.setPenDown(); + int n = 20; + for (int i = 0; i < n; i++) { + turtle.turnRight(360.0/n); + drawSquare(turtle, 10); + } + + + } + +/* + private static void drawFractale(){ + int width = 590, height = 580, nbIterations = 4; + Turtle turtle = new Turtle(width, height); + + // Déplacement de la tortue en bas à gauche. + turtle.setPenUp(); + turtle.turnLeft(90); turtle.moveForward(width/2-10); + turtle.turnLeft(90); turtle.moveForward(height/2-10); + turtle.turnLeft(180); + turtle.setPenDown(); + + // Définition des règles + Rule[] rules = { new Rule('X', "XAYAX+A+YAXAY-A-XAYAX"), + new Rule('Y', "YAXAY-A-XAYAX+A+YAXAY") }; + SetOfRules setOfRules = new SetOfRules(rules); + + // Application des règles nbIterations fois + String sequence = "X"; + for (int i = 0; i < nbIterations; i++) + sequence = setOfRules.apply(sequence); + + // Dessin de la séquence par la tortue + turtle.drawString(sequence, 7, 90); + } +*/ + +} diff --git a/tp6/Point.java b/tp6/Point.java new file mode 100755 index 0000000000000000000000000000000000000000..9b902ea5d8f5d4aafadadd9d1547bb8be99815f5 --- /dev/null +++ b/tp6/Point.java @@ -0,0 +1,77 @@ +/** + * Created by Arnaud Labourel on 20/09/2018. + */ +import tp2.lib.Painter; + +import java.awt.*; + +public class Point { + public final double x; + public final double y; + + public Point(double x, double y) { + this.y = y; + this.x = x; + // Add code here + } + + void draw(Painter painter, Color color){ + // Add code here + painter.addPoint(this.x, this.y, color); + } + + void drawLine(Point p, Painter painter, Color color){ + painter.addLine(this.x, this.y, p.x, p.y, color); + // Add code here + } + + Point translate(double dx, double dy) { + return new Point(this.x+dx, this.y+dy); + } + + Point rotate(double angle) { + double a = (Math.PI * angle)/180; + + double xr = (this.x*Math.cos(a)) - (this.y*Math.sin(a)); + double yr = (this.x*Math.sin(a)) + (this.y*Math.cos(a)); + + + return new Point(xr,yr); + } + + public double distance (Point point){ + return (Math.sqrt( Math.pow(this.x-point.x, 2) + Math.pow((this.y-point.y), 2) )); + } + + +public static void test1_Point(Painter painter){ + Point p1 = new Point(100,100); + Point p2 = new Point(300,100); + Point p3 = new Point(300,300); + Point p4 = new Point(100,300); + p1.drawLine(p2, painter, Color.black); + p2.drawLine(p3, painter, Color.black); + p3.drawLine(p4, painter, Color.black); + p4.drawLine(p1, painter, Color.black); + p1.draw(painter, Color.red); + p2.draw(painter, Color.red); + p3.draw(painter, Color.red); + p4.draw(painter, Color.red); +} + +public static void test2_Point(Painter painter){ + Point p1 = new Point(100,100); + Point p2 = p1.translate(200, 0); + Point p3 = p2.translate(0, 200); + Point p4 = p3.translate(-200,0); + p1.drawLine(p2, painter, Color.black); + p2.drawLine(p3, painter, Color.black); + p3.drawLine(p4, painter, Color.black); + p4.drawLine(p1, painter, Color.black); + p1.draw(painter, Color.red); + p2.draw(painter, Color.red); + p3.draw(painter, Color.red); + p4.draw(painter, Color.red); +} + +} diff --git a/tp6/Rectangle.java b/tp6/Rectangle.java new file mode 100755 index 0000000000000000000000000000000000000000..82552ade2176b1998b462ae7ca794eb9d2e969a2 --- /dev/null +++ b/tp6/Rectangle.java @@ -0,0 +1,82 @@ +import tp2.lib.Painter; + + +import java.awt.*; +import java.util.Random; +import static tp2.lib.Tools.sleep; + +/** + * Created by Arnaud Labourel on 17/10/2018. + */ + +public class Rectangle implements Shape{ + + Point p1, p2; + + public Rectangle(Point p1, Point p2){ + // Add code here + this.p1 = p1; + this.p2 = p2; + + } + + private double height(){ + return Math.abs(p2.y-p1.y); + } + + private double width(){ + return Math.abs(p2.x-p1.x); + } + + + + + public static void test_rectangle(Painter painter){ + Point p1 = new Point(100,100); + Point p2 = new Point(200,100); + Point p3 = new Point(200,300); + Random random= new Random(); + + Shape r = new Rectangle(p1, p3); + r.draw(painter, Color.black); + + for(int i =30; i<400; i+=30) { + Shape t2 = r.translate(i, i); + sleep(100); + t2.draw(painter, new Color(random.nextFloat(), + random.nextFloat(), random.nextFloat())); + } + } + + + @Override + public double getPerimeter() { + return height()*2 + width()*2; + } + + @Override + public void draw(Painter painter, Color color) { + Point upRight = p1.translate(width(),0); + Point downLeft = p1.translate(0,height()); + p1.drawLine(upRight,painter,color); + p1.drawLine(downLeft,painter,color); + p2.drawLine(upRight,painter,color); + p2.drawLine(downLeft,painter,color); + } + + @Override + public Shape translate(int dx, int dy) { + return new Rectangle( + p1.translate(dx,dy), + p2.translate(dx,dy) + ); + } + + @Override + public double getArea() { + return height()*width(); + } + + + +} diff --git a/tp6/Shape.java b/tp6/Shape.java new file mode 100755 index 0000000000000000000000000000000000000000..2890dd54d78f3db6c9bb47d31976d6193359655f --- /dev/null +++ b/tp6/Shape.java @@ -0,0 +1,15 @@ +import tp2.lib.Painter; + +import java.awt.*; + +/** + * Created by Arnaud Labourel on 15/10/2018. + */ + +public interface Shape { + + double getPerimeter(); + void draw(Painter painter, Color color); + Shape translate(int dx, int dy); + double getArea(); +} diff --git a/tp6/Triangle.java b/tp6/Triangle.java new file mode 100755 index 0000000000000000000000000000000000000000..709f89a7c4d433919d2f3927de393db2c0940aa4 --- /dev/null +++ b/tp6/Triangle.java @@ -0,0 +1,73 @@ +import tp2.lib.Painter; +import static tp2.lib.Tools.sleep; +import java.awt.*; +import java.util.Random; + +/** + * Created by Arnaud Labourel on 15/10/2018. + */ +public class Triangle implements Shape { + private final Point[] vertices; + + public Triangle(Point p1, Point p2, Point p3) { + vertices = new Point[]{p1, p2, p3}; + } + + private Triangle(Point[] vertices){ + this.vertices = vertices; + } + + + public static void test_triangle(Painter painter){ + Point p1 = new Point(100,100); + Point p2 = new Point(200,100); + Point p3 = new Point(200,200); + + Point point[] = {p1,p2,p3}; + + Random random= new Random(); + Shape r = new Triangle(point); + System.out.println(r.getArea()); + for(int i =30; i<400; i+=30) { + Shape t2 = r.translate(i, i); + sleep(100); + t2.draw(painter, new Color(random.nextFloat(), + random.nextFloat(), random.nextFloat())); + } + } + + + @Override + public double getPerimeter() { + return (vertices[0].distance(vertices[1]) + + vertices[1].distance(vertices[2]) + + vertices[2].distance(vertices[0])); + } + + @Override + public void draw(Painter painter, Color color) { + for (int i = 0; i < 3; i++) { + vertices[i].drawLine(vertices[ (i+1) % 3],painter,color); + } + } + + @Override + public Shape translate(int dx, int dy) { + return new Triangle( + vertices[0].translate(dx,dy), + vertices[1].translate(dx,dy), + vertices[2].translate(dx,dy) + ); + } + + @Override + public double getArea() { + double p = getPerimeter()/2; + double a = vertices[0].distance(vertices[1]); + double b = vertices[1].distance(vertices[2]); + double c = vertices[2].distance(vertices[0]); + return Math.sqrt( p * (p-a) * (p-b) * (p-c) ); + } + + +} diff --git a/tp6/Turtle.java b/tp6/Turtle.java new file mode 100644 index 0000000000000000000000000000000000000000..9c2e79c661971e43f7208d3a13f2e4dd56376d8e --- /dev/null +++ b/tp6/Turtle.java @@ -0,0 +1,67 @@ +import java.awt.Color; +import static tp2.lib.Tools.sleep; +import tp2.lib.Painter; + +public class Turtle { +Color penColor; +double angleDirection; +Point position; +boolean penIsDown; +Painter painter; + +public Turtle(int width, int height) { + penColor = null; + angleDirection = 270; + painter = new Painter(width, height); + penIsDown = false; + position = new Point(width/2, height/2); + +} + +public void moveForward(double distance) { + sleep(500); + double move = -(Math.sqrt(distance)-position.x); + Point newPosition = position.translate(position.x + move, position.y ); + newPosition = newPosition.rotate(angleDirection); + + if (penIsDown){ + position.drawLine(newPosition, painter, penColor); + } + + + position = newPosition; +} + +public void setColor(Color color) { + penColor = color; +} + +public void turnLeft(double angle) { + angleDirection = angleDirection - angle; + while (angleDirection < 0) { + angleDirection = 360 - angleDirection; + + } +} + +public void turnRight(double angle) { + angleDirection = angleDirection + angle; + while (angleDirection > 360) { + angleDirection = angleDirection - 360; + } +} + +public void setPenDown() { + penIsDown = true; + +} + +public void setPenUp() { + penIsDown = false; + +} + + + + +} diff --git a/tp6/tp2.jar b/tp6/tp2.jar new file mode 100755 index 0000000000000000000000000000000000000000..dbf171ecd818c65bf2aa67aeefd910370bfbab4c Binary files /dev/null and b/tp6/tp2.jar differ diff --git a/tp6/tp2/META-INF/MANIFEST.MF b/tp6/tp2/META-INF/MANIFEST.MF new file mode 100755 index 0000000000000000000000000000000000000000..58630c02ef423cffd6dd6aafd946eb8512040c37 --- /dev/null +++ b/tp6/tp2/META-INF/MANIFEST.MF @@ -0,0 +1,2 @@ +Manifest-Version: 1.0 + diff --git a/tp6/tp2/tp2/lib/Line.class b/tp6/tp2/tp2/lib/Line.class new file mode 100755 index 0000000000000000000000000000000000000000..be9729a696402565a3f262ee6dc069d7bf9dab4a Binary files /dev/null and b/tp6/tp2/tp2/lib/Line.class differ diff --git a/tp6/tp2/tp2/lib/Painter$1.class b/tp6/tp2/tp2/lib/Painter$1.class new file mode 100755 index 0000000000000000000000000000000000000000..378cfcba6901281c57a8d5deede45c4bb6da9b2a Binary files /dev/null and b/tp6/tp2/tp2/lib/Painter$1.class differ diff --git a/tp6/tp2/tp2/lib/Painter$Canvas.class b/tp6/tp2/tp2/lib/Painter$Canvas.class new file mode 100755 index 0000000000000000000000000000000000000000..b3101cd6f5b1fe41cc7ccdaa9cab904686bdb28a Binary files /dev/null and b/tp6/tp2/tp2/lib/Painter$Canvas.class differ diff --git a/tp6/tp2/tp2/lib/Painter.class b/tp6/tp2/tp2/lib/Painter.class new file mode 100755 index 0000000000000000000000000000000000000000..ede1b6569325971de48b5f7158cf32352181070e Binary files /dev/null and b/tp6/tp2/tp2/lib/Painter.class differ diff --git a/tp6/tp2/tp2/lib/PainterElement.class b/tp6/tp2/tp2/lib/PainterElement.class new file mode 100755 index 0000000000000000000000000000000000000000..bc59f4a0146b025fa5e8baeec404cef5e4386b84 Binary files /dev/null and b/tp6/tp2/tp2/lib/PainterElement.class differ diff --git a/tp6/tp2/tp2/lib/Point.class b/tp6/tp2/tp2/lib/Point.class new file mode 100755 index 0000000000000000000000000000000000000000..a819a8f514fdf2730726c4017a7b66684d5afbb4 Binary files /dev/null and b/tp6/tp2/tp2/lib/Point.class differ diff --git a/tp6/tp2/tp2/lib/Tools.class b/tp6/tp2/tp2/lib/Tools.class new file mode 100755 index 0000000000000000000000000000000000000000..38a25c7192c043b2f2746fa22aafc5abe9920b4f Binary files /dev/null and b/tp6/tp2/tp2/lib/Tools.class differ