Skip to content
Snippets Groups Projects
Commit 1b811cd8 authored by guyslain.naves's avatar guyslain.naves
Browse files

Tâches 3 et 4

parent 60c9f67d
Branches correction
No related tags found
No related merge requests found
package fr.univamu; package fr.univamu;
import fr.univamu.geo.Disc; import fr.univamu.geo.*;
import fr.univamu.geo.Shape;
import fr.univamu.geo.Star;
import fr.univamu.geo.Vector2D;
import fr.univamu.svg.SvgElement; import fr.univamu.svg.SvgElement;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.util.List;
import static fr.univamu.svg.SvgAttribute.tag; import static fr.univamu.svg.SvgAttribute.tag;
...@@ -29,6 +27,14 @@ public class App { ...@@ -29,6 +27,14 @@ public class App {
} }
public static void main(String[] args) throws FileNotFoundException { public static void main(String[] args) throws FileNotFoundException {
write(new Star(7,3),"out/star.svg"); Shape shape1 = new Star(7, 3)
.scale(3, 2)
.rotate(Math.PI / 2)
.translate(new Vector2D(5, -5))
.rotate(Math.PI/4, new Vector2D(5,-5));
Shape circle = new Disc(3, new Vector2D(-3,1));
Group group1 = new Group(List.of(shape1, circle));
Shape group2 = group1.rotate(Math.PI/6);
write(new Group(List.of(group1,group2)),"out/group.svg");
} }
} }
package fr.univamu.geo;
import java.util.List;
public record Composition(List<Transformation> transforms) implements Transformation {
@Override
public String toTag() {
StringBuilder builder = new StringBuilder();
for (Transformation transformation : transforms) {
builder.append(transformation.toTag()).append(" ");
}
return builder.toString();
}
}
...@@ -13,4 +13,5 @@ public record Disc(double radius, Vector2D center) implements Shape { ...@@ -13,4 +13,5 @@ public record Disc(double radius, Vector2D center) implements Shape {
.add(tag("cx", center.x())) .add(tag("cx", center.x()))
.add(tag("cy", center.y())); .add(tag("cy", center.y()));
} }
} }
package fr.univamu.geo;
import fr.univamu.svg.SvgElement;
import java.util.List;
public record Group(List<Shape> shapes) implements Shape {
@Override
public SvgElement toSvg() {
SvgElement group = new SvgElement("g");
for (Shape shape : shapes) {
group.add(shape.toSvg());
}
return group;
}
}
package fr.univamu.geo;
public record Rotation(double angle) implements Transformation {
@Override
public String toTag() {
return "rotate(" + angle * 180 / Math.PI + ")";
}
}
package fr.univamu.geo;
public record Scale(double xFactor, double yFactor) implements Transformation {
@Override
public String toTag() {
return "scale(" + xFactor + "," + yFactor + ")";
}
}
...@@ -2,8 +2,45 @@ package fr.univamu.geo; ...@@ -2,8 +2,45 @@ package fr.univamu.geo;
import fr.univamu.svg.SvgElement; import fr.univamu.svg.SvgElement;
import java.util.List;
public sealed interface Shape public sealed interface Shape
permits Disc, Polygon { permits Disc, Polygon, Transformed, Group {
SvgElement toSvg(); SvgElement toSvg();
default Shape transform(Transformation transform) {
return new Transformed(this, transform);
}
default Shape translate(Vector2D move) {
return this.transform(new Translation(move));
}
default Shape translate(double x, double y) {
return this.translate(new Vector2D(x,y));
}
default Shape rotate(double radian) {
return this.transform(new Rotation(radian));
}
default Shape rotate(double radian, Vector2D center) {
return this.transform(new Composition(
List.of(
new Translation(center),
new Rotation(radian),
new Translation(center.opposite())
)
));
}
default Shape scale(double xFactor, double yFactor) {
return this.transform(new Scale(xFactor, yFactor));
}
default Shape scale(double factor) {
return this.scale(factor,factor);
}
} }
package fr.univamu.geo;
public sealed interface Transformation
permits Rotation, Translation, Scale, Composition {
String toTag();
}
package fr.univamu.geo;
import fr.univamu.svg.SvgElement;
import java.util.List;
import static fr.univamu.svg.SvgAttribute.tag;
public record Transformed(Shape shape, Transformation transform) implements Shape {
@Override
public SvgElement toSvg() {
return shape.toSvg().add(tag("transform", transform.toTag()));
}
@Override
public Shape transform(Transformation transform) {
return new Transformed(shape, new Composition(List.of(transform,this.transform)));
}
}
package fr.univamu.geo;
public record Translation(Vector2D move) implements Transformation {
@Override
public String toTag() {
return "translate(" + move.x() + "," + move.y() + ")";
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment