6
import java.io.*;
import java.awt.*;

// Logarithmic spiral example
public class Spiral extends Frame
{// The spiral consists of n line segments. Line segment 1
// has starting point (hc, vc). Line segment k, for 1<=k<=n,
// has length k*d. Each line segment makes an angle of turn
// degrees with the previous line segment. The colors of the
// lines alternate between red, blue, and green.

final static int hc= 500;   // Center of spiral is (hc,vc)
final static int vc= 350;
final static int n= 2;    // Number of sides to draw
final static int turn= 45;  // The turn factor
final static double d= 1;   // Length of leg k is k*d

public void paint(Graphics g)
    {int h= hc;
    int v= vc;
    int k= 1;
    //Invariant: legs 1..k-1 have been drawn, and leg k is
    //           to be drawn with start point (hc,vc)
    while (k<=n)
        {//Draw line k
            if (k%3==0) g.setColor(Color.red);
            if (k%3==1) g.setColor(Color.blue);
            if (k%3==2) g.setColor(Color.green);

            int theta= k*turn %360;
            double L= k*d;
            // Calculate the end point (h_next,v_next) of
            // the line
                int h_next= (int) Math.round(
                        h+L*Math.cos(theta*Math.PI/180));
                int v_next= (int) Math.round(
                        v+L*Math.sin(theta*Math.PI/180));
            g.drawLine(h,v,h_next, v_next);

        h= h_next; v= v_next;
        k= k+1;
        }
    }

}

public class spiralMain {

public static void main(String args[]) {
            Spiral d = new Spiral();
    d.resize(10,10);
    d.move(0,50);
    d.setTitle("Logarithmic spiral");
    d.show();
    d.toFront();
    }
}

I'm attempting to create a logarithmic spiral using line segments. When I compile the code I get this:

actual

But I'm trying to get something with a lot less lines. It should look something like this:

expected

I'm not sure what I should change the values to in order to get it to that point.

Alain O'Dea
  • 21,033
  • 1
  • 58
  • 84
Aizzle
  • 133
  • 1
  • 9
  • 4
    Your result has a psychedelic effect. If you stare at it long enough, you start seeing the lines move, and then eventually it produces hallucinations and you can imagine any shape you want. So what's the problem? ;-) – Klitos Kyriacou Nov 12 '15 at 22:48
  • 1
    Code snippets are for Javascript. – David Conrad Nov 12 '15 at 23:00
  • 1
    I fixed a bunch of random things (mainly code style which is very important for clarity) in your question. I hope you find the changes to be positive. – Alain O'Dea Nov 12 '15 at 23:48
  • I also made the images embed which really helps make the question easier to consume. You can upload images directly to questions and they embed nicely. – Alain O'Dea Nov 12 '15 at 23:49
  • 1
    Switching to the State Pattern disentangled the graphics code from the algorithm significantly here. I saw it and couldn't not follow it. – Alain O'Dea Nov 13 '15 at 00:20
  • I've rolled back to @DavidConrad's version here. I went way overboard and broke the rule of thumb which is only to edit to fix up style or cosmetic issues. – Alain O'Dea Nov 13 '15 at 20:01

1 Answers1

11

If I remember correctly, you should set d to the golden ratio:

/**
 * Length of leg k is k * D
 */
private final static double D = 1.618;
Alain O'Dea
  • 21,033
  • 1
  • 58
  • 84
zteffi
  • 660
  • 5
  • 18