且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Java 中组合区域时舍入不准确?

更新时间:2023-12-02 20:51:16

这里:

    for (int i = 0; i < 3; i++) {
        triangle.moveTo(random.nextInt(400) + 50, random.nextInt(400) + 50);
        triangle.lineTo(random.nextInt(400) + 50, random.nextInt(400) + 50);
        triangle.lineTo(random.nextInt(400) + 50, random.nextInt(400) + 50);
        triangle.closePath();
        area.add(new Area(triangle));
    }       

你实际上是在添加第一个循环中的 1 个三角形第二个循环中的 2 个三角形第三个循环中的3个三角形

you are adding in fact 1 triangle in the first loop 2 triangles in the second loop 3 triangles in the third loop

这就是您的不准确之处.试试这个,看看你的问题是否仍然存在.

This is where your inaccuracies come from. Try this and see if your problem still persists.

    for (int i = 0; i < 3; i++) {
        triangle.moveTo(random.nextInt(400) + 50, random.nextInt(400) + 50);
        triangle.lineTo(random.nextInt(400) + 50, random.nextInt(400) + 50);
        triangle.lineTo(random.nextInt(400) + 50, random.nextInt(400) + 50);
        triangle.closePath();
        area.add(new Area(triangle));
        triangle.reset();
    }    

注意每次循环后路径重置.

Note the path reset after each loop.

更多地解释不准确之处来自您尝试组合的三个路径.这使得可能出现错误的位置一目了然.

to explain more where the inaccuracies come from here the three paths you try to combine. Which makes it obvious where errors might arise.