diff --git a/src/AudioClient.java b/src/AudioClient.java index a119649..ca350c4 100644 --- a/src/AudioClient.java +++ b/src/AudioClient.java @@ -10,17 +10,9 @@ public class AudioClient { AudioPlayer player = new AudioPlayer(); AudioPlayer.FORMAT = new XtFormat(new XtMix(sampleRate, XtSample.FLOAT32), 0, 0, 2, 0); - // Draws a square. - Line l1 = new Line(-0.5, -0.5, 0.5, -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); - + for (int i = 2; i < 7; i++) { + AudioPlayer.addLines(generatePolygon(i, 0.5)); + } AudioPlayer.setRotateSpeed(0.4); player.start(); @@ -32,8 +24,19 @@ public class AudioClient { public static List generatePolygon(int sides, double scale) { List 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; } } \ No newline at end of file diff --git a/src/AudioPlayer.java b/src/AudioPlayer.java index 99d1a1a..d609cb8 100644 --- a/src/AudioPlayer.java +++ b/src/AudioPlayer.java @@ -53,6 +53,10 @@ public class AudioPlayer extends Thread { AudioPlayer.lines.add(line); } + public static void addLines(List lines) { + AudioPlayer.lines.addAll(lines); + } + private static Line currentLine() { return lines.get(currentLine % lines.size()); } diff --git a/src/Line.java b/src/Line.java index dd4e4de..fb2d9d7 100644 --- a/src/Line.java +++ b/src/Line.java @@ -20,6 +20,10 @@ public class Line { return new Line(getA().rotate(theta), getB().rotate(theta)); } + public Line copy() { + return new Line(getA().copy(), getB().copy()); + } + public Point getA() { return a; } diff --git a/src/Point.java b/src/Point.java index 3c3c6a0..ded181d 100644 --- a/src/Point.java +++ b/src/Point.java @@ -38,14 +38,15 @@ public class Point { } } + public Point copy() { + return new Point(x, y); + } + public Point rotate(double theta) { - double oldX = getX(); - double oldY = getY(); + Point point = new Point(getX(), getY()); - Point point = new Point(oldX, oldY); - - point.setX(oldX * Math.cos(theta) - oldY * Math.sin(theta)); - point.setY(oldX * Math.sin(theta) + oldY * Math.cos(theta)); + point.setX(getX() * Math.cos(theta) - getY() * Math.sin(theta)); + point.setY(getX() * Math.sin(theta) + getY() * Math.cos(theta)); return point; }