I';我正试图编写一个递归java程序来创建Serpinski三角形

I';我正试图编写一个递归java程序来创建Serpinski三角形,java,recursion,bufferedimage,graphics2d,fractals,Java,Recursion,Bufferedimage,Graphics2d,Fractals,我遇到的问题是输出png只在左下角显示递归,我不知道为什么。有人能给我指一下正确的方向吗?对不起,我的评论太乱了。 多谢各位 import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.ArrayList; import javax.imageio.I

我遇到的问题是输出png只在左下角显示递归,我不知道为什么。有人能给我指一下正确的方向吗?对不起,我的评论太乱了。 多谢各位

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;

public class Assignment12 {
static int WIDTH = 0;
static int HEIGHT = 0;
static ArrayList<Double> points = new ArrayList<Double>();

public static void main(String[] args) {
    if (args.length == 0) {
        WIDTH = 800;
        HEIGHT = 693;
    } else {
        Integer W = Integer.valueOf(args[0]);
        Integer H = Integer.valueOf(args[0]);
        WIDTH = W;
        HEIGHT = H;
    }
    // DONE //*********stuff to do ***********create the final ordered pairs
    // of the biggest triangle
    // done// *********stuff to do *********** set the values of the ordered
    // pairs
    // to the dimensions of the rectangle
    final double topX = WIDTH / 2;
    final double topY = 0;
    final double leftX = 0;
    final double leftY = HEIGHT - 1;
    final double rightX = WIDTH - 1;
    final double rightY = HEIGHT - 1;
    sierpinski(topX, topY, leftX, leftY, rightX, rightY);
    // ##############jeffs code#########
    // BufferedImage creates memory space for storing image data
    BufferedImage img = new BufferedImage(WIDTH, HEIGHT,
            BufferedImage.TYPE_INT_RGB);
    // Graphics2D provides a canvas on which to draw shapes, text, other
    // images
    Graphics2D g2d = img.createGraphics();
    // *********stuff to do *********** do the back ground stuffs
    // Clear background to white
    g2d.setColor(Color.white);
    g2d.fillRect(0, 0, WIDTH, HEIGHT);
    // start drawing lines in the correct color.
    // Red line from where to where?
    g2d.setColor(Color.red);
    for (int i = 0; i < points.size(); i = i + 4) {
        g2d.drawLine(points.get(i).intValue(),
                points.get(i + 1).intValue(), points.get(i + 2).intValue(),
                points.get(i + 3).intValue());
    }
    // g2d.drawLine(0, 0, WIDTH - 1, HEIGHT - 1);
    // done// *********stuff to do *********** initialize the recursive
    // function to
    // done// *********stuff to do *********** get rid of these oval things
    // there
    // just for reference,
    // Draw some random ovals
    // for (int i = 0; i < 100; i++) {
    // g2d.setColor(new Color((float) Math.random(),
    // (float) Math.random(), (float) Math.random()));
    // g2d.fillOval((int) (Math.random() * WIDTH),
    // (int) (Math.random() * HEIGHT), (int) (Math.random() * 50),
    // (int) (Math.random() * 50));
    // }
    // Finalize the canvas
    g2d.dispose();
    // Write the image out as a PNG-format file
    try {
        ImageIO.write(img, "png", new File("out.png"));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

// *********stuff to do *********** create the recursive function for the
// triangles
private static void sierpinski(double topX, double topY, double leftX,
        double leftY, double rightX, double rightY) {
    // base case area of the triangle reaches x
    // (Ax(By-Cy)+Bx(Cy-Ay)+Cx(Ay-By))/2
    if (((leftX * (topY - rightY)) + (topX * (rightY - leftY)) + (rightX * (leftY - topY)) / 2) > 10) {// <--
        // that 10 should be a static value or just pulled directly from
        // args
        // start recursive for all three respective points
        double leftMidX = (topX + leftX) / 2;
        double leftMidY = (topY + leftY) / 2;
        double rightMidX = (topX + rightX) / 2;
        double rightMidY = (topY + rightY) / 2;
        // ***these are just called topMid for convenience. they are the mid
        // point values to the bottom line of the triangle.
        double botMidX = (leftX + rightX) / 2;
        double botMidY = (leftY + rightY) / 2;
        // top ... top stays the same
        sierpinski(topX, topY, leftMidX, leftMidY, rightMidX, rightMidY);
        // left
        sierpinski(leftMidX, leftMidY, leftX, leftY, botMidX, botMidY);
        // right
        sierpinski(rightMidX, rightMidY, botMidX, botMidY, rightX, rightY);
    } else {
        points.add(topX);
        points.add(topY);
        points.add(rightX);
        points.add(rightY);
        points.add(topX);
        points.add(topY);
        points.add(leftX);
        points.add(leftY);
        points.add(leftX);
        points.add(leftY);
        points.add(rightX);
        points.add(rightY);
        // draw the lines
        // g2d.drawLine(topX, topY, rightX, rightY);// right
        // g2d.drawLine(topX, topY, leftX, leftY);// left
        // g2d.drawLine(leftX, leftY, rightX, rightY);// bot
    }
}
导入java.awt.Color;
导入java.awt.Graphics2D;
导入java.awt.image.buffereImage;
导入java.io.File;
导入java.io.IOException;
导入java.util.ArrayList;
导入javax.imageio.imageio;
公共课堂作业12{
静态整数宽度=0;
静态整数高度=0;
静态ArrayList points=新建ArrayList();
公共静态void main(字符串[]args){
如果(args.length==0){
宽度=800;
高度=693;
}否则{
整数W=Integer.valueOf(args[0]);
整数H=Integer.valueOf(args[0]);
宽度=W;
高度=H;
}
//完成//**********要做的事情**********创建最终的有序对
//最大三角形的
//done//**********要执行的内容**********设置已排序的
//成对的
//到矩形的尺寸
最终双顶部x=宽度/2;
最终的双topY=0;
最终双左X=0;
最终双左=高度-1;
最终双右X=宽度-1;
最终双右=高度-1;
瑟宾斯基(托普,托普,左,左,右,右);
//杰夫斯代码#########
//BuffereImage为存储图像数据创建内存空间
BuffereImage img=新的BuffereImage(宽度、高度、,
BuffereImage.TYPE_INT_RGB);
//Graphics2D提供了一个画布,可以在上面绘制形状、文本和其他内容
//图像
Graphics2D g2d=img.createGraphics();
//**********要做的事情**********做幕后工作
//将背景清除为白色
g2d.setColor(Color.white);
g2d.fillRect(0,0,宽度,高度);
//开始以正确的颜色绘制线条。
//从哪里到哪里的红线?
g2d.setColor(Color.red);
对于(int i=0;i如果((leftX*(topY-rightY))+(topX*(rightY-leftY))+(rightX*(leftY-topY))/2>10{/要正确绘制一个Sierpinski三角形,请计算点。下面的代码演示了它。可以找到更详细的示例

对于参照,可以查看计算三角形点的不同方法(计算Pascal三角形,并使用其中奇数值的位置作为sierpinski三角形的点)

导入java.awt.Color;
导入java.awt.Graphics2D;
导入java.awt.image.buffereImage;
导入java.io.File;
导入java.io.IOException;
导入java.util.ArrayList;
导入javax.imageio.imageio;
导入java.util.List;
公共类谢尔宾斯基
{ 
公共静态整数宽度=800;
公共静态内部高度=693;
公共静态列表点=新建ArrayList();
公共静态void main(字符串[]args)
{
如果(0!=参数长度)
{
宽度=整数.valueOf(args[0]);
高度=整数.valueOf(args[0]);
}
//完成//**********要做的事情**********创建最终的有序对
//最大三角形的
//done//**********要执行的内容**********设置已排序的
//成对的
//到矩形的尺寸
最终双顶部x=宽度/2;
最终的双topY=0;
最终双左X=0;
最终双左=高度-1;
最终双右X=宽度-1;
最终双右=高度-1;
瑟宾斯基(托普,托普,左,左,右,右);
//杰夫斯代码#########
//BuffereImage为存储图像数据创建内存空间
BuffereImage img=新的BuffereImage(宽度、高度、,
BuffereImage.TYPE_INT_RGB);
//Graphics2D提供了一个画布,可以在上面绘制形状、文本和其他内容
//图像
Graphics2D g2d=img.createGraphics();
//**********要做的事情**********做幕后工作
//将背景清除为白色
g2d.setColor(Color.white);
g2d.fillRect(0,0,宽度,高度);
//开始以正确的颜色绘制线条。
//从哪里到哪里的红线?
g2d.setColor(Color.red);
对于(int i=0;iimport java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import java.util.List;

public class Sierpinski
{ 
    public static int WIDTH  = 800;
    public static int HEIGHT = 693;
    public static List<Double> points = new ArrayList<>();

    public static void main(String[] args)
    {
        if (0 != args.length)
        {
            WIDTH  = Integer.valueOf(args[0]);
            HEIGHT = Integer.valueOf(args[0]);
        }
        // DONE //*********stuff to do ***********create the final ordered pairs
        // of the biggest triangle
        // done// *********stuff to do *********** set the values of the ordered
        // pairs
        // to the dimensions of the rectangle
        final double topX   = WIDTH / 2;
        final double topY   = 0;
        final double leftX  = 0;
        final double leftY  = HEIGHT - 1;
        final double rightX = WIDTH  - 1;
        final double rightY = HEIGHT - 1;
        sierpinski(topX, topY, leftX, leftY, rightX, rightY);
        // ##############jeffs code#########
        // BufferedImage creates memory space for storing image data
        BufferedImage img = new BufferedImage(WIDTH, HEIGHT,
                                              BufferedImage.TYPE_INT_RGB);
        // Graphics2D provides a canvas on which to draw shapes, text, other
        // images
        Graphics2D g2d = img.createGraphics();
        // *********stuff to do *********** do the back ground stuffs
        // Clear background to white
        g2d.setColor(Color.white);
        g2d.fillRect(0, 0, WIDTH, HEIGHT);
        // start drawing lines in the correct color.
        // Red line from where to where?
        g2d.setColor(Color.red);
        for (int i = 0; i < points.size(); i = i + 4)
        {
            g2d.drawLine(
                          points.get(i).intValue(),
                          points.get(i + 1).intValue(), 
                          points.get(i + 2).intValue(),
                          points.get(i + 3).intValue());
        }
        // Finalize the canvas
        g2d.dispose();
        // Write the image out as a PNG-format file
        try
        {
            ImageIO.write(img, "png", new File("out.png"));
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    /**
     * Triangle base = (Ax(By-Cy)+Bx(Cy-Ay)+Cx(Ay-By))/2
     * @return base for the triangle
     */
    public static double base(
                                double topX  , double topY  ,
                                double leftX , double leftY , 
                                double rightX, double rightY )
    {
        return (  (topX  * (rightY - leftY)) 
                + (rightX* (leftY  - topY))
                + (leftX * (topY   - rightY ))) / 2;
    }
    // *********stuff to do *********** create the recursive function for the
    // triangles
    private static void sierpinski(
                                    double topX  , double topY  ,
                                    double leftX , double leftY , 
                                    double rightX, double rightY )
    {
        if (10 < base(topX, topY, leftX, leftY, rightX, rightY))
        {// <--
            // that 10 should be a static value or just pulled directly from
            // args
            // start recursive for all three respective points
            /**
             *
            double leftMidX  = (topX + leftX ) / 2;
            double leftMidY  = (topY + leftY ) / 2;
            double rightMidX = (topX + rightX) / 2;
            double rightMidY = (topY + rightY) / 2;
             */
            double leftMidX  = leftX + (topX   - leftX) / 2;
            double leftMidY  = topY  + (leftY  - topY ) / 2;
            double rightMidX = topX  + (rightX - topX ) / 2;
            double rightMidY = leftMidY                  ;
            // ***these are just called topMid for convenience. they are the mid
            // point values to the bottom line of the triangle.
            double botMidX = topX  ;
            double botMidY = leftY ;
            /*
            double botMidX = (leftX + rightX) / 2;
            double botMidY = (leftY + rightY) / 2;
            */
            // top ... top stays the same
            sierpinski(topX, topY, leftMidX, leftMidY, rightMidX, rightMidY);
            // left
            sierpinski(leftMidX, leftMidY, leftX, leftY, botMidX, botMidY);
            // right
            sierpinski(rightMidX, rightMidY, botMidX, botMidY, rightX, rightY);
        }
        else
        {
            points.add(topX);
            points.add(topY);
            points.add(rightX);
            points.add(rightY);
            points.add(topX);
            points.add(topY);
            points.add(leftX);
            points.add(leftY);
            points.add(leftX);
            points.add(leftY);
            points.add(rightX);
            points.add(rightY);
        }
    }
}