如何遮蔽sierpinski三角形?

时间:2012-02-15 14:32:17

标签: java geometry

我创建了一个sierpinski三角形代码。但我想根据代码生成的三角形来对三角形进行着色。在我的情况下,我只打印没有阴影的三角形。我想要类似这样的东西! http://en.wikipedia.org/wiki/File:Sierpinski_triangle_evolution.svg

第2组:Kulplex

学生:Henry Dang

我该怎么办?

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

3 个答案:

答案 0 :(得分:3)

当你处于递归通话的最底层时,你不应该只使用g.drawPolygon(px, py, 3);的电话吗?

现在代码看向我的方式总是会绘制第一级的大三角形,这会遮盖你对较小层的绘图。

我想说的是,如果级别为零,您应该只调用g.drawPolygon(),因为所有较低级别只是递归以产生较小的三角形。如果您正在尝试绘制黑色三角形,而不是绘制白色空间。你想要完成的事情有点不清楚。

编辑:根据您的评论,你真正想要的是填充多边形,我是对的吗?在这种情况下,将drawPolygon替换为fillPolygon,三角形将被填入。

答案 1 :(得分: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);
}

将所有drawPolygon替换为fillPolygon

不要通过鼠标点击来增加深度。

答案 2 :(得分: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);
}

}