Implemeted ellipse rotation

pull/35/head
James Ball 2020-02-07 11:33:04 +00:00
rodzic e5460bf096
commit dd24a19447
2 zmienionych plików z 20 dodań i 11 usunięć

Wyświetl plik

@ -14,7 +14,8 @@ public class AudioClient {
AudioPlayer.FORMAT = AudioPlayer.defaultFormat(SAMPLE_RATE);
AudioPlayer.addShapes(Shapes.generatePolygram(5, 3, 0.5, 60));
AudioPlayer.addShape(new Ellipse(0.7, 0.7));
//AudioPlayer.addShapes(Shapes.generatePolygon(500, 0.5, 10));
AudioPlayer.addShape(new Ellipse(0.5, 1));
AudioPlayer.setRotateSpeed(0.8);
AudioPlayer.setTranslation(2, new Vector(0.5, 0.5));

Wyświetl plik

@ -5,50 +5,58 @@ public class Ellipse implements Shape {
private final double b;
private final double weight;
private final double length;
private final double rotation;
private final Vector position;
public Ellipse(double a, double b, double weight, Vector position) {
public Ellipse(double a, double b, double weight, double rotation, Vector position) {
this.a = a;
this.b = b;
this.weight = weight;
this.rotation = rotation;
this.position = position;
// Approximation of length.
this.length = 2 * Math.PI * Math.sqrt((this.a * this.a + this.b * this.b) / 2);
}
public Ellipse(double a, double b, Vector position) {
this(a, b, 100, position);
this(a, b, 100, 0, position);
}
public Ellipse(double a, double b) {
this(a, b, 100, new Vector());
this(a, b, 100, 0, new Vector());
}
@Override
public float nextX(double drawingProgress) {
return (float) (position.getX() + a * Math.sin(2 * Math.PI * drawingProgress));
return (float) (position.getX()
+ a * Math.cos(2 * Math.PI * drawingProgress) * Math.cos(rotation)
- b * Math.sin(2 * Math.PI * drawingProgress) * Math.sin(rotation));
}
@Override
public float nextY(double drawingProgress) {
return (float) (position.getY() + b * Math.cos(2 * Math.PI * drawingProgress));
return (float) (position.getY()
+ a * Math.cos(2 * Math.PI * drawingProgress) * Math.sin(rotation)
+ b * Math.sin(2 * Math.PI * drawingProgress) * Math.cos(rotation));
}
// TODO: Implement ellipse rotation.
@Override
public Shape rotate(double theta) {
return this;
if (theta + rotation > 2 * Math.PI) {
theta -= 2 * Math.PI;
}
return new Ellipse(a, b, weight, theta, position);
}
@Override
public Shape scale(double factor) {
return new Ellipse(a * factor, b * factor, weight, position.scale(factor));
return new Ellipse(a * factor, b * factor, weight, rotation, position.scale(factor));
}
// TODO: Implement ellipse translation.
@Override
public Shape translate(Vector vector) {
return new Ellipse(a, b, weight, position.add(vector));
return new Ellipse(a, b, weight, rotation, position.add(vector));
}
@Override