我在编写打印出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) {
}
}
答案 0 :(得分:1)
Sierpinski三角形图形化是一个对象,其中包含较小的对象,依此类推。每个三角形有三个坐标顶部,左下角,右下角,您可以绘制三角形。 您从最大尺寸的三角形( triangle1 )开始。接下来,您会发现此三角形的每个边缘都是中点。一旦你有中点 midpoint1:从(topx; topy)到(leftx; lefty) midpoint2:从(topx; topy)到(rightx; righty) midpoint3:从(leftx; lefty)到(rightx; righty) 你通过点(中点1,中点2,中点3)绘制另一个三角形。此三角形以图形方式将 triangle1 划分为三个相等大小的较小三角形。现在,您为这三个三角形中的每一个重复此过程。依此类推,直到你处于不能进一步划分三角形的情况下(比如你用三角形边长1)。
结果是这样的
我有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);
}
}
}