Search code examples
javaswingawtjava-2d

How can I draw a double archimedean spiral?


According to our teacher, this image is an archimidean spiral: archimidean

The problem is that on internet I search methods to draw an archimidean spiral and I only find something like this: enter image description here

So I have no clue of how to draw something like the first image, what I already tried is to build an spiral in a way and then put the same spiral but on the other way, but it didn´t worked,the code I used is from Java: Draw a circular spiral using drawArc

public class ArchimideanSpiral extends JFrame {

    public ArchimideanSpiral()
    {
        super("Archimidean Spiral");
        setSize(500,500);
        setVisible(true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }
    public void paint(Graphics g)
    {
         int x = getSize().width / 2 - 10;
    int y = getSize().height/ 2 - 10;
    int width = 20;
    int height = 20;
    int startAngle = 0;
    int arcAngle = 180;
    int depth = 10;
    for (int i = 0; i < 10; i++) {
        width = width + 2 * depth;
         y = y - depth;
        height = height + 2 * depth;
        if (i % 2 == 0) {



            g.drawArc(x, y, width, height, startAngle, -arcAngle);
        } else {

            x = x - 2 * depth;
            g.drawArc(x, y, width, height, startAngle, arcAngle);
        }
    }









            }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        new ArchimideanSpiral();
    }

}

But If I try to put the same spiral in a reverse way it doesnt work so I´m lost.


Solution

  • The trick I'd use to implement that would be to use a directionMuliplier to get the spiral to go in a different direction (clockwise / anticlockwise) for each part. It is used to adjust the x/y values of points in the spiral. E.G. a value in the upper right of the center point in one spiral, will become lower left in the other.

    private Point2D getPoint(double angle, int directionMuliplier) {
        double l = angle*4;
        double x = directionMuliplier * Math.sin(angle)*l;
        double y = directionMuliplier * Math.cos(angle)*l;
        return new Point2D.Double(x, y);
    }
    

    This is how that method might be called to produce a GeneralPath which can be used in a paint method.

    GeneralPath gp = new GeneralPath();
    
    gp.moveTo(0, 0);
    // create the Archimmedian spiral in one direction
    for (double a = 0d; a < Math.PI * 2 * rotations; a += step) {
        Point2D p = getPoint(a, 1);
        gp.lineTo(p.getX(), p.getY());
    }
    
    gp.moveTo(0, 0);
    // now reverse the direction
    for (double a = 0d; a < Math.PI * 2 * rotations; a += step) {
        Point2D p = getPoint(a, -1);
        gp.lineTo(p.getX(), p.getY());
    }
    

    Here is what it might look like:

    enter image description here