1

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:

img

And if I set the spinRate to 2, I get this:

img

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:

img

Spin rate 2:

img

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);
            } 
    }
weztex
  • 53
  • 6
  • 1
    While it's not using StdDraw, [this example](https://stackoverflow.com/questions/22530660/java-graphics-drawing-custom-circular/22531102#22531102) might give you some ideas – MadProgrammer Jan 29 '18 at 00:57
  • I have no idea how to solve your problem, but I just have to say this is the clearest, most beautiful question I have seen in a while. I have been reading a lot of crappy questions lately. This one is very refreshing. I am suitably impressed. – Keara Jan 29 '18 at 02:57
  • 1
    Thanks for the comments guys! I actually figured it out. Since it's a homework project question I'm not going to post the solution until the assignment is past due but I'm really happy about figuring it out on my own. Taking a break then coming back to it really helped. – weztex Jan 29 '18 at 06:40
  • your angle should be `0...2PI*number_of_spins` ... so your angle step and `R` is most likely wrong – Spektre Jan 29 '18 at 08:50
  • If you can add your solution to this weztex, that would be great! – halfer Apr 15 '18 at 17:47
  • added solution! – weztex Apr 17 '18 at 05:20

0 Answers0