java - Java 中毕达哥拉斯树的可视化表示

标签 java graphics2d fractals

我想使用Java对毕达哥拉斯树进行可视化表示,代码输出一个PNG固定图像。

我首先定义 Vector 类,它从两个 vector 分量 (x,y) 开始可以旋转 vector 、缩放 vector 或将其添加到另一个 vector 。

public class Vector {
    public double x;
    public double y;

    public Vector(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public Vector rotated(double alpha) {
        double x1 = Math.cos(alpha) * x - Math.sin(alpha) * y;
        double y1 = Math.sin(alpha) * x + Math.cos(alpha) * y;
        Vector vRotated = new Vector(x1, y1);
        return vRotated;
    }

    public Vector scaled(double s) {
        double x1 = x * s;
        double y1 = y * s;
        Vector vScaled = new Vector(x1, y1);
        return vScaled;
    }

   public Vector added(Vector v) {
       double x1 = this.x+v.x;
       double y1 = this.y+v.y;
       Vector vAdded = new Vector(x1,y1);
       return vAdded;
   }
}

我还编写了创建初始图像和背景并将其保存到所需路径的方法

  public static void createPythagorasTreeImage(int startSize) throws IOException {
    // Creation of the image object
    int height = 5 * startSize;
    int width = 8 * startSize;
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    // Create a Graphics2D object from the image and set a white background
    Graphics2D g = image.createGraphics();
    g.setColor(new Color(255, 255, 255));
    g.fillRect(0, 0, width, height);

    // Initial position and orientation of the first segment
    Vector startPos = new Vector(width / 2, startSize);
    Vector up = new Vector(0, 1);

    // Start the recursion.
    drawSegment(g, startPos, up, startSize, height);

    // Save the image as PNG
    String OS = System.getProperty("os.name").toLowerCase(); // different for win and unix
    String filePath = System.getProperty("user.dir") + (OS.indexOf("win") >= 0 ? "\\" : "/") + "pythagorasTree.png";
    System.out.println("Writing pythagoras-tree image to: " + filePath);
    ImageIO.write(image, "png", new File(filePath));
    }

我已经在维基百科上阅读了有关树如何工作的信息,并且现在想要实现该算法。 我需要帮助的是使用 Graphics2D (我不太熟悉)实现这两种方法:

public static void drawRotatedRect(Graphics2D g, Vector pos, Vector up, int a, int height) {
    }

此方法应该使用 Graphics2D 绘制一个正方形(也许使用 g.fi llPolygon()?),在位置 pos 处,向上表示正方形旋转的 vector ,通过指示正方形的向上方向,a 是正方形的边,高度是绘图空间的高度。

 public static void drawSegment(Graphics2D g, Vector pos, Vector up, int a, int height) {
    }

此方法应该使用前面的方法绘制第一个正方形,然后计算两个新正方形的位置和旋转并绘制它们,递归地重复此操作,直到正方形的边长非常小(2px)。

这是我对毕达哥拉斯树的理解,我设法编写了大部分代码,并且只有当我让两个缺少的方法起作用时,这个想法似乎才是正确的。

最佳答案

您可以通过绘制浮点(或 double )精度的 Path2D 来使用 Graphics2D 上下文。我推荐这一点,因为您会注意到使用 int 精度可能会给您带来奇怪的效果。

要绘制路径,请执行以下操作:

Path2D.Double rectangle = new Path2D.Double();

rectangle.moveTo(0, 0);
// ... basically draw the four points of the rectangle here. 
rectangle.closePath();

g.setColor(yourColorOfChoice);
g.fill(rectangle);

请注意,您需要手动绘制矩形,因为它们不需要旋转,而 Graphics2D 不能很好地处理旋转。您可以尝试使用固有旋转,但您会使上下文像素化,并且您不会喜欢它。

我非常期待您的结果。完成后,您可以将最终图像粘贴到您的问题中吗:)?

关于java - Java 中毕达哥拉斯树的可视化表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56736601/

相关文章:

java - 如何使用 iText 将图形绘制为 PDF?

opengl - 分形的连续着色

java - Maven - maven-remote-resources-plugin 不包括所有资源目录

java - 使用 Java HTTP URL 连接进行暴力破解

java - 在paintComponent中重绘形状时无法保存描边/线条粗细

java - 将 BufferedImage 设置为 Java 中的颜色

algorithm - 什么术语描述了在不知道其他坐标的情况下生成坐标的过程/分形生成?

image-processing - 估计二维图像的分形维数

java - 不同jar中的相同功能

java 8 , tamcat 8 : Invalid byte tag in constant pool: 19