绘制圆圈金字塔的公式

时间:2010-06-14 02:33:35

标签: java drawing formula

我正在尝试为我的游戏创建一个圆形金字塔,看起来与此相似:

alt text http://img266.imageshack.us/img266/3094/lab1213c.jpg

但我无法正确打印。不断地,我得到了非常奇怪的螺旋,但没有接近这一点。谁能给我一些关于正确配方的小费?我的窗口是600x600,金字塔的底座是8。

    fields = new Field[BASE*(BASE/2)+4];
    int line_count = BASE;
    int line_tmp = line_count;
    for(int i=0; i< fields.length; i++){
        for( int j=line_tmp; j <= line_count; j++){
            fields[i] = new Field(0, (150+(line_tmp*5)),(600+line_tmp*5));
        }
        line_count--;
        line_tmp = line_count;
    }

1 个答案:

答案 0 :(得分:2)

我看到的错误是:

  • 数组大小公式不正确。
  • y 表达式中加入line_tmp(似乎是您的列计数器)。
  • 有两个变量,line_countline_temp始终相等。
  • 让您的外部循环按节点计数而不是按行计数。
  • 一般都有无意义的变量名和魔法数字。
// I use java.util.ArrayList because using its add(..) method is convenient here.
// The proper forumula for anticipated number of nodes is: base×(base+1)÷2
final List<Field> fields = new ArrayList<Field>(BASE*(BASE+1)/2);
// I use a java.awt.Point to store the (x,y) value of the first node of the row.
// This clarifies the meaning, rather than using ints or long inline expressions.
final Point rowStart = new Point(PANEL_WIDTH/2, DIAMETER);

// The number of rows equals the number of nodes on the final row.
for (int row = 1; row <= BASE; row++) {
    // The nth row has n nodes.
    for (int circle = 0; circle < row; circle++) {
        // Each row starts at rowStart and each subsequent circle is offset to 
        // the right by two times the circle diameter.
        fields.add(new Field(0, rowStart.x + circle*DIAMETER*2, rowStart.y));
    }
    // Each subsequent row starts a little down and to the left of the previous.
    rowStart.x -= DIAMETER;
    rowStart.y += DIAMETER;
}

如果这是作业,请记住仅将其用作修复自己代码的参考。