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

Tâches 1 et 2

parent 14cad9b2
No related branches found
No related tags found
No related merge requests found
package fr.univamu;
import fr.univamu.geo.Disc;
import fr.univamu.geo.Shape;
import fr.univamu.geo.Star;
import fr.univamu.geo.Vector2D;
import fr.univamu.svg.SvgElement;
import java.io.FileNotFoundException;
......@@ -26,6 +29,6 @@ public class App {
}
public static void main(String[] args) throws FileNotFoundException {
// write(new Circle(1),"out/circle.svg");
write(new Star(7,3),"out/star.svg");
}
}
package fr.univamu.geo;
import java.util.List;
public record Arbitrary(List<Vector2D> vertices) implements Polygon {
@Override
public int nbVertices() {
return vertices.size();
}
@Override
public Vector2D getVertex(int index) {
return vertices.get(index);
}
}
package fr.univamu.geo;
import fr.univamu.svg.SvgElement;
import static fr.univamu.svg.SvgAttribute.tag;
public record Disc(double radius, Vector2D center) implements Shape {
@Override
public SvgElement toSvg() {
return new SvgElement("circle")
.add(tag("r", radius))
.add(tag("cx", center.x()))
.add(tag("cy", center.y()));
}
}
package fr.univamu.geo;
import fr.univamu.svg.SvgAttribute;
import fr.univamu.svg.SvgElement;
public sealed interface Polygon extends Shape
permits Regular, Star, Arbitrary {
int nbVertices();
Vector2D getVertex(int index);
@Override
default SvgElement toSvg() {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < nbVertices(); i++) {
builder.append(getVertex(i)).append(" ");
}
return new SvgElement("polygon")
.add(SvgAttribute.tag("points", builder.toString()));
}
}
package fr.univamu.geo;
public record Regular(int nbVertices) implements Polygon {
@Override
public Vector2D getVertex(int index) {
return Vector2D.polar(index * 2 * Math.PI / nbVertices(), 1);
}
}
package fr.univamu.geo;
import fr.univamu.svg.SvgElement;
public sealed interface Shape
permits Disc, Polygon {
SvgElement toSvg();
}
package fr.univamu.geo;
public record Star(int nbVertices, int step) implements Polygon {
@Override
public Vector2D getVertex(int index) {
return Vector2D.polar(step * index * 2 * Math.PI / nbVertices(), 1);
}
}
package fr.univamu.geo;
public record Vector2D(double x, double y) {
public static final Vector2D ZERO = new Vector2D(0,0);
public static final Vector2D I = new Vector2D(1,0);
public static final Vector2D J = new Vector2D(0,1);
private static final double EPSILON = 1e-9;
@Override
public String toString() {
return this.x + "," + this.y;
}
public static Vector2D cartesian(double x, double y) {
return new Vector2D(x, y);
}
public static Vector2D polar(double radian, double radius) {
return new Vector2D(
Math.cos(radian) * radius,
Math.sin(radian) * radius
);
}
public Vector2D plus(Vector2D vec) {
return new Vector2D(this.x + vec.x, this.y + vec.y);
}
public Vector2D minus(Vector2D vec) {
return new Vector2D(this.x - vec.x, this.y - vec.y);
}
public Vector2D opposite() {
return new Vector2D(-this.x, -this.y);
}
public Vector2D scale(double factor) {
return new Vector2D(factor * this.x, factor * this.y);
}
public double dot(Vector2D vec) {
return this.x * vec.x + this.y * vec.y;
}
public Vector2D rotate(double radian) {
return new Vector2D(
Math.cos(radian) * this.x - Math.sin(radian) * this.y,
Math.sin(radian) * this.x + Math.cos(radian) * this.y
);
}
@Override
public boolean equals(Object o) {
if (o == this) { return true; }
if (!(o instanceof Vector2D vec)) {
return false;
}
return Math.abs(this.x - vec.x) < EPSILON
&& Math.abs(this.y - vec.y) < EPSILON;
}
}
package fr.univamu.geo;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class DiscTest {
@Test
void toSvg() {
assertEquals("<circle r=\"1.0\" cx=\"0.0\" cy=\"0.0\"/>\n",
new Disc(1,Vector2D.ZERO).toSvg().toString()
);
assertEquals("<circle r=\"0.5\" cx=\"3.0\" cy=\"-2.0\"/>\n",
new Disc(0.5,Vector2D.cartesian(3,-2)).toSvg().toString()
);
}
}
\ No newline at end of file
package fr.univamu.geo;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class RegularTest {
@Test
void toSvg() {
assertEquals(
"<polygon points=\"1.0,0.0 -0.4999999999999998,0.8660254037844387 -0.5000000000000004,-0.8660254037844384 \"/>\n"
, new Regular(3).toSvg().toString());
assertEquals(
"<polygon points=\"1.0,0.0 6.123233995736766E-17,1.0 -1.0,1.2246467991473532E-16 -1.8369701987210297E-16,-1.0 \"/>\n"
, new Regular(4).toSvg().toString());
}
}
\ No newline at end of file
package fr.univamu.geo;
import org.junit.jupiter.api.Test;
import static fr.univamu.geo.Vector2D.I;
import static fr.univamu.geo.Vector2D.J;
import static org.junit.jupiter.api.Assertions.*;
class Vector2DTest {
@Test
void plus() {
assertEquals(Vector2D.cartesian(1,1), I.plus(J));
}
@Test
void minus() {
assertEquals(Vector2D.cartesian(1,-1), I.minus(J));
}
@Test
void rotate() {
assertEquals(J, I.rotate(Math.PI/2));
assertEquals(I, J.rotate(-Math.PI/2));
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment