2010-06-14 27 views
2

我試圖建立社交圈的金字塔到我的遊戲,尋找類似這樣的金字塔:式畫圓

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; 
    } 
+0

看起來更像是一個三角形而不是金字塔對我來說 – Artelius 2010-06-14 02:41:40

+0

首先,你沒有在其for循環內的任何地方引用j。你多次重新分配給字段[i]。 – 2010-06-14 02:51:33

+0

也許還有一些關於Field()參數的信息會有所幫助。此外,你是否嘗試使用一維或二維數組來存儲字段? – 2010-06-14 03:32:52

回答

2

我看到的錯誤是:

  • 不正確的數組大小的公式。
  • 包括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; 
}

記住,只能以此爲基準固定自己的代碼,如果這是家庭作業。

+0

+1 - 另一個問題是OP忽略了表達式中局部變量名稱和空格的公認Java風格約定。 – 2010-06-14 05:37:40