2012-02-15 76 views
2

我創建了一個sierpinski三角形代碼。但是我想根據代碼所做的三角形陰影三角形。在我的情況下,我只打印沒有陰影的三角形。我想要類似這樣的東西! http://en.wikipedia.org/wiki/File:Sierpinski_triangle_evolution.svg我如何遮蔽sierpinski三角形?

組2:Kulplex

學生:亨利蕩

我應該怎麼辦?

import java.applet.*; 
import java.awt.*; 

public class Sierpinski extends Applet { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    Graphics g; 
    Point a1,b1,c1, a2,b2,c2, a3,b3,c3; 

    int deep = 0; 

    public void paint() { 
     setBackground(new Color(255,255,255)); 
    } 

    public boolean mouseDown(Event ev, int x, int y) { 
     if (!ev.metaDown()) deep += 1; 
     else if (deep>0) deep -= 1; 
     repaint(); 
     return true; 
    } 


    public void paint(Graphics g) { 
     // Create triangle 
     int px[] = {20, 400, 210}; 
     int py[] = {400, 400, 20}; 
     g.drawPolygon(px, py, 3); 

     paintTriangle(g, new Point(20,400),new Point(400,400),new Point(210,20), deep); 
    } 

    public void paintTriangle(Graphics g, Point a, Point b, Point c, int lvl) { 


     if (lvl==0) return; 

     lvl -= 1; 

     // In the given triangle, amended to include an upside-down triangle 
     int px[] = {c.x, (c.x+b.x)/2, (a.x+c.x)/2}; 
     int py[] = {b.y, (c.y+a.y)/2, (c.y+a.y)/2}; 

     g.drawPolygon(px, py, 3); 

     // 3 new triangles 
     a1 = a; 
     b1 = new Point(c.x, b.y); 
     c1 = new Point((a.x+c.x)/2, (c.y+a.y)/2); 
     paintTriangle(g, a1, b1, c1, lvl); 

     a2 = new Point(c.x, b.y); 
     b2 = b; 
     c2 = new Point((c.x+b.x)/2, (c.y+a.y)/2); 
     paintTriangle(g, a2, b2, c2, lvl); 

     a3 = new Point((a.x+c.x)/2, (c.y+a.y)/2); 
     b3 = new Point((c.x+b.x)/2, (c.y+a.y)/2); 
     c3 = c; 
     paintTriangle(g, a3, b3, c3, lvl); 
    } 
} 
+1

代碼忽略 2012-02-15 14:33:27

+0

什麼是「蔭」呢?塗上不同的顏色? – DRCB 2012-02-15 14:44:20

+0

在這種情況下,陰影意味着我想陰影我繪製的tracingles,因爲這裏的三角形是透明的。 – 2012-02-15 14:49:41

回答

3

當您處於遞歸調用的底部時,是否只使用對g.drawPolygon(px, py, 3);的調用?

代碼對我來說現在的樣子,您將始終繪製第一級的大三角形,這會使您對較小圖層的圖形變得模糊。

我想說的是,如果水平爲零,你應該只對g.drawPolygon()進行調用,因爲所有較低水平只是遞歸產生較小的三角形。這是如果你想繪製黑色三角形,而不是繪製空白。有點不清楚你想完成什麼。

編輯:根據你的評論你真正想要填充多邊形,對不對?在這種情況下,更換drawPolygonfillPolygon和三角形將被填滿。

+0

謝謝各位,我用fillpolygon替換了drawpolygon並插入了新的顏色。 :) 任務完成 – 2012-02-15 15:01:39

0

這應該工作。

import java.applet.*; 
import java.awt.*; 

public class Sierpinski extends Applet { 

/** 
* 
*/ 
private static final long serialVersionUID = 1L; 
Graphics g; 
Point a1,b1,c1, a2,b2,c2, a3,b3,c3; 

int deep = 0; 

public void paint() { 
    setBackground(new Color(255,255,255)); 
} 

public boolean mouseDown(Event ev, int x, int y) { 
    if (!ev.metaDown()) deep += 1; 
    else if (deep>0) deep -= 1; 
    repaint(); 
    return true; 
} 


public void paint(Graphics g) { 
    // Create triangle 
    int px[] = {20, 400, 210}; 
    int py[] = {400, 400, 20}; 
    g.fillPolygon(px, py, 3); 

    paintTriangle(g, new Point(20,400),new Point(400,400),new Point(210,20), deep); 
} 

public void paintTriangle(Graphics g, Point a, Point b, Point c, int lvl) { 


    if (lvl==0) { 
     return; 
    } 

    lvl -= 1; 

    // In the given triangle, amended to include an upside-down triangle 
    int px[] = {c.x, (c.x+b.x)/2, (a.x+c.x)/2}; 
    int py[] = {b.y, (c.y+a.y)/2, (c.y+a.y)/2}; 

    // 3 new triangles 
    a1 = a; 
    b1 = new Point(c.x, b.y); 
    c1 = new Point((a.x+c.x)/2, (c.y+a.y)/2); 
    paintTriangle(g, a1, b1, c1, lvl); 

    a2 = new Point(c.x, b.y); 
    b2 = b; 
    c2 = new Point((c.x+b.x)/2, (c.y+a.y)/2); 
    paintTriangle(g, a2, b2, c2, lvl); 

    a3 = new Point((a.x+c.x)/2, (c.y+a.y)/2); 
    b3 = new Point((c.x+b.x)/2, (c.y+a.y)/2); 
    c3 = c; 
    paintTriangle(g, a3, b3, c3, lvl); 
    g.setColor(Color.WHITE); 
    g.fillPolygon(px, py, 3); 
} 

}

1

你必須開始用黑色實心三角形,然後填寫三角形白色:

public void paint(Graphics g) { 
    // Create triangle 
    int px[] = {20, 400, 210}; 
    int py[] = {400, 400, 20}; 
    g.setColor(Color.black); 
    g.fillPolygon(px, py, 3); 
    g.setColor(Color.white); 
    paintTriangle(g, new Point(20,400),new Point(400,400),new Point(210,20), deep); 
} 

替換所有drawPolygonfillPolygon

不要用鼠標點擊來增加深度。