Added generic polygon drawing

pull/35/head
James Ball 2020-01-29 20:00:15 +00:00
rodzic 33f867dba4
commit d2c668d057
4 zmienionych plików z 31 dodań i 19 usunięć

Wyświetl plik

@ -10,17 +10,9 @@ public class AudioClient {
AudioPlayer player = new AudioPlayer(); AudioPlayer player = new AudioPlayer();
AudioPlayer.FORMAT = new XtFormat(new XtMix(sampleRate, XtSample.FLOAT32), 0, 0, 2, 0); AudioPlayer.FORMAT = new XtFormat(new XtMix(sampleRate, XtSample.FLOAT32), 0, 0, 2, 0);
// Draws a square. for (int i = 2; i < 7; i++) {
Line l1 = new Line(-0.5, -0.5, 0.5, -0.5); AudioPlayer.addLines(generatePolygon(i, 0.5));
Line l2 = new Line(0.5, -0.5, 0.5, 0.5); }
Line l3 = new Line(0.5, 0.5, -0.5, 0.5);
Line l4 = new Line(-0.5, 0.5, -0.5, -0.5);
AudioPlayer.addLine(l1);
AudioPlayer.addLine(l2);
AudioPlayer.addLine(l3);
AudioPlayer.addLine(l4);
AudioPlayer.setRotateSpeed(0.4); AudioPlayer.setRotateSpeed(0.4);
player.start(); player.start();
@ -32,8 +24,19 @@ public class AudioClient {
public static List<Line> generatePolygon(int sides, double scale) { public static List<Line> generatePolygon(int sides, double scale) {
List<Line> polygon = new ArrayList<>(); List<Line> polygon = new ArrayList<>();
Point start = new Point(scale, scale); double theta = 2 * Math.PI / sides;
return null; Point start = new Point(scale, scale);
Point rotated = start.rotate(theta);
polygon.add(new Line(start, rotated));
while (!rotated.equals(start)) {
polygon.add(new Line(rotated.copy(), rotated.rotate(theta)));
rotated = rotated.rotate(theta);
}
return polygon;
} }
} }

Wyświetl plik

@ -53,6 +53,10 @@ public class AudioPlayer extends Thread {
AudioPlayer.lines.add(line); AudioPlayer.lines.add(line);
} }
public static void addLines(List<Line> lines) {
AudioPlayer.lines.addAll(lines);
}
private static Line currentLine() { private static Line currentLine() {
return lines.get(currentLine % lines.size()); return lines.get(currentLine % lines.size());
} }

Wyświetl plik

@ -20,6 +20,10 @@ public class Line {
return new Line(getA().rotate(theta), getB().rotate(theta)); return new Line(getA().rotate(theta), getB().rotate(theta));
} }
public Line copy() {
return new Line(getA().copy(), getB().copy());
}
public Point getA() { public Point getA() {
return a; return a;
} }

Wyświetl plik

@ -38,14 +38,15 @@ public class Point {
} }
} }
public Point copy() {
return new Point(x, y);
}
public Point rotate(double theta) { public Point rotate(double theta) {
double oldX = getX(); Point point = new Point(getX(), getY());
double oldY = getY();
Point point = new Point(oldX, oldY); point.setX(getX() * Math.cos(theta) - getY() * Math.sin(theta));
point.setY(getX() * Math.sin(theta) + getY() * Math.cos(theta));
point.setX(oldX * Math.cos(theta) - oldY * Math.sin(theta));
point.setY(oldX * Math.sin(theta) + oldY * Math.cos(theta));
return point; return point;
} }