Rounding Inaccuracies When Combining Areas in Java?

前端 未结 3 2127
迷失自我
迷失自我 2020-12-21 05:08

I\'m working with Areas in Java.

My test program draws three random triangles and combines them to form one or more polygons. After the Areas

3条回答
  •  粉色の甜心
    2020-12-21 05:57

    Here:

        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));
        }       
    

    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.

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

    First path

    Second path

    Third path

提交回复
热议问题