Changed shape to abstract class to reduce code duplication

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

Wyświetl plik

@ -1,10 +1,8 @@
package shapes; package shapes;
public class Ellipse implements Shape { public class Ellipse extends Shape {
private final double a; private final double a;
private final double b; private final double b;
private final double weight;
private final double length;
private final double rotation; private final double rotation;
private final Vector position; private final Vector position;
@ -15,7 +13,7 @@ public class Ellipse implements Shape {
this.rotation = rotation; this.rotation = rotation;
this.position = position; this.position = position;
// Approximation of length. // Approximation of length.
this.length = 2 * Math.PI * Math.sqrt((this.a * this.a + this.b * this.b) / 2); this.length = 2 * Math.PI * Math.sqrt((a * a + b * b) / 2);
} }
public Ellipse(double a, double b, Vector position) { public Ellipse(double a, double b, Vector position) {
@ -58,14 +56,4 @@ public class Ellipse implements Shape {
public Shape translate(Vector vector) { public Shape translate(Vector vector) {
return new Ellipse(a, b, weight, rotation, position.add(vector)); return new Ellipse(a, b, weight, rotation, position.add(vector));
} }
@Override
public double getWeight() {
return weight;
}
@Override
public double getLength() {
return length;
}
} }

Wyświetl plik

@ -1,12 +1,8 @@
package shapes; package shapes;
public class Line implements Shape { public class Line extends Shape {
private final Vector a; private final Vector a;
private final Vector b; private final Vector b;
private final double weight;
// Storing the length to save on repeat calculations.
private final double length;
public static final double DEFAULT_WEIGHT = 100; public static final double DEFAULT_WEIGHT = 100;
@ -102,16 +98,6 @@ public class Line implements Shape {
return new Line(getX1(), getY1(), getX2(), y2); return new Line(getX1(), getY1(), getX2(), y2);
} }
@Override
public double getWeight() {
return weight;
}
@Override
public double getLength() {
return length;
}
public Line setWeight(double weight) { public Line setWeight(double weight) {
return new Line(getX1(), getY1(), getX2(), getY2(), weight); return new Line(getX1(), getY1(), getX2(), getY2(), weight);
} }

Wyświetl plik

@ -1,11 +1,20 @@
package shapes; package shapes;
public interface Shape { public abstract class Shape {
float nextX(double drawingProgress); protected double weight;
float nextY(double drawingProgress); protected double length;
Shape rotate(double theta);
Shape scale(double factor); public abstract float nextX(double drawingProgress);
Shape translate(Vector vector); public abstract float nextY(double drawingProgress);
double getWeight(); public abstract Shape rotate(double theta);
double getLength(); public abstract Shape scale(double factor);
public abstract Shape translate(Vector vector);
public double getWeight() {
return weight;
}
public double getLength() {
return length;
}
} }