谢尔宾斯基三角形 Java
Sierpinski Triangle Java
我在编写打印谢尔宾斯基三角形的程序时遇到问题。我在开始实际代码时遇到了麻烦,我在下面写了一些基本方法。谢谢
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 三角形在图形上是一个包含较小对象的对象,依此类推。每个三角形都有三个坐标 top、bottom-left、bottom-right,您可以从中绘制三角形。
您从最大尺寸的三角形 (triangle1) 开始。接下来你找到这个三角形的每条边的中点。一旦你有了中点
中点 1:从 (topx ; topy ) 到 (leftx ; lefty )
中点 2:从 (topx ; topy ) 到 (rightx; righty)
中点 3:从 (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);
}
}
}
我在编写打印谢尔宾斯基三角形的程序时遇到问题。我在开始实际代码时遇到了麻烦,我在下面写了一些基本方法。谢谢
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 三角形在图形上是一个包含较小对象的对象,依此类推。每个三角形都有三个坐标 top、bottom-left、bottom-right,您可以从中绘制三角形。 您从最大尺寸的三角形 (triangle1) 开始。接下来你找到这个三角形的每条边的中点。一旦你有了中点 中点 1:从 (topx ; topy ) 到 (leftx ; lefty ) 中点 2:从 (topx ; topy ) 到 (rightx; righty) 中点 3:从 (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);
}
}
}