java - 在Java中制作对数螺旋

标签 java spiral

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

我正在尝试使用线段创建对数螺线。当我编译代码时,我得到了这个:

actual

但我正在尝试用更少的线条来获得一些东西。它应该看起来像这样:

expected

我不确定我应该将这些值更改为什么才能达到这一点。

最佳答案

如果我没记错的话,你应该将 d 设置为 golden ratio :

/**
 * Length of leg k is k * D
 */
private final static double D = 1.618;

关于java - 在Java中制作对数螺旋,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33682763/

相关文章:

c - C 执行螺旋矩阵实现中的段错误运行时错误

scala - 在 Scala 中生成惰性 "spiral"

java - 如何将 Eclipse 插件中现有 View 的上下文弹出菜单重用到新创建的 View ?

java - weblogic上下文查找错误: java. rmi.UnmarshalException:解码参数时出错

java - 加工过程中的不同颜色

Java:用drawLine绘制矩形螺旋线

java - 将语句传递给方法

java - 直接分配给老年代

java.lang.NoClassDefFoundError : org/json/JSONObject when Java Class runs on OAM Server

c++ - 以螺旋方式修改数组