如何绘制双阿基米德螺旋线?

时间:2019-03-07 18:21:07

标签: java swing awt java-2d

根据我们的老师所说,这张图片是一个阿基米德式的螺旋: archimidean

问题是,在互联网上,我搜索了绘制阿基米德螺线的方法,而我只发现了以下内容: enter image description here

所以我不知道如何绘制像第一个图像那样的东西,我已经尝试过以某种方式构建一个螺旋,然后以相同的方式放置螺旋,但以另一种方式,但是它没有用,我使用的代码来自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();
    }

}

但是,如果我尝试以相反的方式放置相同的螺旋线,那么它将不起作用,所以我迷路了。

1 个答案:

答案 0 :(得分:2)

我要实现的技巧是使用directionMuliplier来使每个部分的螺旋线朝不同的方向(顺时针/逆时针)移动。它用于调整螺旋中点的x / y值。例如。一个螺旋中中心点右上的值将在另一个螺旋中左下。

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);
}

这就是调用该方法以生成可以在paint方法中使用的GeneralPath的方式。

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());
}

这是它的外观:

enter image description here