Closed. This question needs to be more focused。它当前不接受答案。












想改善这个问题吗?更新问题,使其仅通过editing this post专注于一个问题。

4年前关闭。





我在编写打印出sierpinkski三角形的程序时遇到了麻烦。我在开始使用实际代码时遇到了麻烦,我在下面编写了一些基本方法。谢谢

public class Assignment11 {

  private static Graphics2D g2d;

  public static void main(String[] args) throws Exception {
    BufferedImage img = new BufferedImage(800, 693, BufferedImage.TYPE_INT_RGB);
    g2d = img.createGraphics();
    g2d.setColor(new Color(255, 0, 0));
    sierpinski(400, 0, 0, 692, 799, 692);
    g2d.dispose();
    ImageIO.write(img, "png", new File("as11.png"));
  }

  private static void sierpinski(double topX, double topY, double leftX,
      double leftY, double rightX, double rightY) {

  }
}

最佳答案

图形化的Sierpinski三角形是其中包含较小对象的对象,依此类推。每个三角形在其上,左下,右下都有三个坐标,您可以绘制一个三角形。
您从最大大小的三角形(triangle1)开始。接下来,您将发现此三角形的每个边缘为中点。一旦达到中点
midpoint1:从(topx; topy)到(leftx; lefty)
midpoint2:从(topx; topy)到(rightx; righty)
midpoint3:从(leftx; lefty)到(rightx; righty)
您通过这些点(midpoint1,midpoint2,midpoint3)绘制了另一个三角形。该三角形以图形方式将triangle1分为三个大小相等的较小三角形。现在,对这三个三角形中的每一个重复该过程。依此类推,直到遇到三角形无法进一步分割的情况为止(假设您以1个长边的三角形结束)。

结果是这样的

java - Sierpinski Triangle Java-LMLPHP

我有一个demo for Sierpinski triangles,请参阅代码中的注释。

import java.util.List       ;
import java.util.LinkedList ;
import java.awt.geom.Point2D;
import java.awt.geom.Line2D ;

/**
 * Generates Sierpinski triangle (▲).
 *
 */
public class Sierpinski
{
    public int width ;
    public int height;
    /**
     * The smallest size of an area of the triangle.
     */
    public int limit ;

    /**
     * Lines of the triangle to be drawn.
     */
    public List<Line2D> lines = new LinkedList<>();

    public Sierpinski(int width, int height, int limit)
    {
        this.width  = width;
        this.height = height;
        this.limit  = limit;
    }

    /**
     * @return triangle area limit
     */
    public int getLimit()
    {
        return limit;
    }

    /**
     * @return width of the Sierpinski triangle
     */
    public int getWidth()
    {
        return width;
    }

    /**
     * @return height of the Sierpinski triangle
     */
    public int getHeight()
    {
        return height;
    }

    /**
     * @return lines of the generated triangles
     */
    public List<Line2D> getLines()
    {
        return lines;
    }

    /**
     * @param p1 Starting point of the line.
     * @param p1 Ending   point of the line.
     */
    public void addLine(Point2D p1, Point2D p2)
    {
        getLines().add(
                new Line2D.Double(p1,p2));
    }

    /**
     * @param top Top-most point of the triangle.
     * @param left Left-most point of the triangle.
     * @param right Right-most point of the triangle.
     *
     * @return area size of the triangle defined by supplied points.
     */
    public static double triangleArea(Point2D top, Point2D left, Point2D right)
    {
        return Math.abs(top.getX()*(right.getY()-left.getY()) + right.getX()*(left.getY() - top.getY()) + left.getX()*(top.getY()-right.getY())) / 2;
    }

    /**
     * Generates a Sierpinski triangle.
     * @see #generate(Point2D, Point2D)
     */
    public void generate()
    {
        generate(
                new Point2D.Double(getWidth() / 2, 0          ),
                new Point2D.Double(0             , getHeight() - 1 ),
                new Point2D.Double(getWidth() - 1, getHeight() - 1));
    }

    /**
     * Generates a Sierpinski triangle.
     */
    public void generate(Point2D top, Point2D left, Point2D right)
    {
        if (getLimit() < triangleArea(top,left,right))
        {
            Point2D leftMiddle   =
                new Point2D.Double(
                        left.getX() + (top.getX()  - left.getX()) / 2,
                        top.getY()  + (left.getY() - top.getY())  / 2);
            Point2D rightMiddle  =
                new Point2D.Double(
                        top.getX() + (right.getX() - top.getX()) / 2,
                        leftMiddle.getY());
            Point2D bottomMiddle =
                new Point2D.Double(
                        top.getX() ,
                        left.getY());

            generate(top        , leftMiddle  , rightMiddle );
            generate(leftMiddle , left        , bottomMiddle);
            generate(rightMiddle, bottomMiddle, right       );
        }
        else
        {
            addLine(top , right);
            addLine(top , left );
            addLine(left, right);
        }
    }
}

07-24 17:45