【问题标题】:Making a logarithmic spiral in Java用Java制作对数螺旋
【发布时间】:2015-11-12 22:38:14
【问题描述】:
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();
    }
}

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

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

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

【问题讨论】:

  • 您的结果具有迷幻效果。如果你盯着它看足够长的时间,你会开始看到线条在移动,然后最终它会产生幻觉,你可以想象任何你想要的形状。所以有什么问题? ;-)
  • 代码 sn-ps 用于 Javascript。
  • 我在你的问题中修复了一堆随机的东西(主要是代码风格,这对于清晰来说非常重要)。我希望您发现这些变化是积极的。
  • 我还嵌入了图像,这确实有助于使问题更易于理解。您可以将图片直接上传到问题中,它们可以很好地嵌入。
  • 切换到状态模式显着地从算法中解开了图形代码。我看到了,不能不关注。

标签: java spiral


【解决方案1】:

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

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

【讨论】:

  • 谢谢。我将这些 d 更改为 1.618,将 N 更改为 45,这似乎成功了。
  • @Aizzle 你也应该支持这个答案。尽可能多地给那些帮助的人:)
猜你喜欢
  • 1970-01-01
  • 2023-03-28
  • 2015-06-20
  • 2022-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-21
  • 1970-01-01
相关资源
最近更新 更多