Added .equals to point and line and created testsuite

pull/35/head
James Ball 2020-01-27 21:39:46 +00:00
rodzic 2094d9a926
commit 3765a1e681
5 zmienionych plików z 80 dodań i 25 usunięć

Wyświetl plik

@ -9,5 +9,15 @@
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="lib" level="project" />
<orderEntry type="module-library" scope="TEST">
<library name="JUnit4">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>

Wyświetl plik

@ -21,11 +21,11 @@ public class AudioPlayer extends Thread {
line.rotate(nextTheta(stream.getFormat().mix.rate, 0.000001));
int framesToDraw = (int) (line.length() * 100);
//int neg = line.getX1() < line.getX2() || line.getY1() < line.getY2() ? 1 : -1;
int neg = line.getX1() < line.getX2() || line.getY1() < line.getY2() ? 1 : -1;
for (int c = 0; c < format.outputs; c++) {
((float[]) output)[f * format.outputs] = line.getX1() + Math.abs(line.getX2() - line.getX1()) * (float) (framesDrawn) / framesToDraw;
((float[]) output)[f * format.outputs + 1] = line.getY1() + Math.abs(line.getY2() - line.getY1()) * (float) (framesDrawn) / framesToDraw;
((float[]) output)[f * format.outputs] = (float) (line.getX1() + Math.abs(line.getX2() - line.getX1()) * (neg * framesDrawn) / framesToDraw);
((float[]) output)[f * format.outputs + 1] = (float) (line.getY1() + Math.abs(line.getY2() - line.getY1()) * (neg * framesDrawn) / framesToDraw);
}
framesDrawn++;

Wyświetl plik

@ -7,20 +7,20 @@ public class Line {
this.b = b;
}
public Line(float x1, float y1, float x2, float y2) {
public Line(double x1, double y1, double x2, double y2) {
this.a = new Point(x1, y1);
this.b = new Point(x2, y2);
}
public float length() {
return (float) Math.sqrt(Math.pow(getX1() - getX2(), 2) + Math.pow(getY1() - getY2(), 2));
public double length() {
return Math.sqrt(Math.pow(getX1() - getX2(), 2) + Math.pow(getY1() - getY2(), 2));
}
public void rotate(double theta) {
setX1((float) (getX1() * Math.cos(theta) - getY1() * Math.sin(theta)));
setY1((float) (getX1() * Math.sin(theta) + getY1() * Math.cos(theta)));
setX2((float) (getX2() * Math.cos(theta) - getY2() * Math.sin(theta)));
setY2((float) (getX2() * Math.sin(theta) + getY2() * Math.cos(theta)));
setX1(getX1() * Math.cos(theta) - getY1() * Math.sin(theta));
setY1(getX1() * Math.sin(theta) + getY1() * Math.cos(theta));
setX2(getX2() * Math.cos(theta) - getY2() * Math.sin(theta));
setY2(getX2() * Math.sin(theta) + getY2() * Math.cos(theta));
}
public Point getA() {
@ -31,35 +31,48 @@ public class Line {
return b;
}
public float getX1() {
public double getX1() {
return a.getX();
}
public float getY1() {
public double getY1() {
return a.getY();
}
public float getX2() {
public double getX2() {
return b.getX();
}
public float getY2() {
public double getY2() {
return b.getY();
}
public void setX1(float x1) {
public void setX1(double x1) {
a.setX(x1);
}
public void setY1(float y1) {
public void setY1(double y1) {
a.setY(y1);
}
public void setX2(float x2) {
public void setX2(double x2) {
b.setX(x2);
}
public void setY2(float y2) {
public void setY2(double y2) {
b.setY(y2);
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
} else if (obj instanceof Line) {
Line line = (Line) obj;
return line.getA().equals(getA()) && line.getB().equals(getB());
} else {
return false;
}
}
}

Wyświetl plik

@ -1,25 +1,44 @@
public class Point {
private float x;
private float y;
private double x;
private double y;
public Point(float x, float y) {
private static final double EPSILON = 0.0001;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public float getX() {
public double getX() {
return x;
}
public float getY() {
public double getY() {
return y;
}
public void setX(float x) {
public void setX(double x) {
this.x = x;
}
public void setY(float y) {
public void setY(double y) {
this.y = y;
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
} else if (obj instanceof Point) {
Point point = (Point) obj;
return approxEquals(point.getX(), getX()) && approxEquals(point.getY(), getY());
} else {
return false;
}
}
private boolean approxEquals(double m, double n) {
return Math.abs(m - n) < EPSILON;
}
}

Wyświetl plik

@ -0,0 +1,13 @@
import org.junit.Test;
import static org.junit.Assert.*;
public class TestSuite {
@Test
public void lineRotationTest() {
Line line = new Line(-0.5, 0.5, 0.5, 0.5);
line.rotate(Math.PI / 2);
assertEquals(new Line(0.5, 0.5, 0.5, -0.5), line);
}
}