Skip to content
Snippets Groups Projects
Commit d087c022 authored by Teo Blaise Kaplanski's avatar Teo Blaise Kaplanski
Browse files

turtle

parent 25bb1e25
No related branches found
No related tags found
No related merge requests found
<?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>
/Main.class
/Point.class
/Rectangle.class
/Shape.class
/Triangle.class
/Turtle.class
<?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>
/**
* 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);
}
*/
}
/**
* 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);
}
}
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();
}
}
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();
}
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) );
}
}
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;
}
}
File added
Manifest-Version: 1.0
File added
File added
File added
File added
File added
File added
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment