This uses a library called StdDraw to draw a line. I'm trying to make a spiral drawing method that takes in the center coordinates, the number of line segments to use, the maximum radius and the "tightness" of the spiral (spinRate).
Here is my code so far:
public void spiral(double x0, double y0, double maxRadius, int
numSegments, double spinRate) {
double angle = 0;
double x = x0;
double y = y0;
double R = maxRadius/(2*Math.PI);
for (int i = 0; i < numSegments; ++i) {
double x1 = x;
double y1 = y;
angle += spinRate/maxRadius;
x = x0 + R*angle*Math.cos(angle);
y = y0 + R*angle*Math.sin(angle);
StdDraw.line(x, y, x1, y1);
}
}
Right now it will draw a spiral, but if I set the spinRate to 1, I get this:
And if I set the spinRate to 2, I get this:
So the radius is changing depending on spinRate instead of remaining constant. And the angles aren't getting tighter, obviously.
Trig isn't my thing at all and I'm kind of confused by this problem.
Here is an example of what I want it to look like:
Spin rate 1:
Spin rate 2:
So you see, the radius is constant but it's a tighter spiral.
Edit:
Here is the solution.
public static void spiral(double x0, double y0, double maxRadius, int
numSegments, double spinRate) {
double radius = 0;
double x = x0;
double y = y0;
double deltaR = maxRadius/numSegments;
for (int i = 0; i < numSegments; ++i) {
double x1 = x;
double y1 = y;
radius += deltaR;
x = x0 + radius*Math.cos((2*Math.PI*i/numSegments) * spinRate);
y = y0 + radius*Math.sin((2*Math.PI*i/numSegments) * spinRate);
StdDraw.line(x, y, x1, y1);
}
}