Skip to content

GeometryUtils#polygonArea(..) is broken #2974

Closed

Description

@dermetfan

GeometryUtils.polygonArea(..) returns wrong values if offset and count are not 0 and polygon.length.

Here's a method to test:

public static boolean isBroken() {
    float[] polygon = {0, 0, 1, 0, 1.5f, .5f, 1, 1, .5f, 1.25f, 0, 1};
    float area1 = com.badlogic.gdx.math.GeometryUtils.polygonArea(polygon, 0, polygon.length);

    float[] polygon2 = {99, 99, 0, 0, 1, 0, 1.5f, .5f, 1, 1, .5f, 1.25f, 0, 1, 99, 99};
    float area2 = com.badlogic.gdx.math.GeometryUtils.polygonArea(polygon2, 2, polygon.length);

    return area1 != area2;
}

Activity

  1. mk1x86 commented on Mar 25, 2015

    @mk1x86

    I guess this is about floating point precision although all your values should be represented by IEEE754.
    Use a comparison like return Math.Abs(area1-area2) < epsilon with a epsilon being a very small value

  2. dermetfan commented on Mar 25, 2015

    @dermetfan
    ContributorAuthor

    @mk1x86 I don't think so. Have a look at the output:

    area1: 1.375
    area2: -48.125

    Also, since polygonArea(..) is deterministic, the same floating point error should occur in both cases so area1 and area2 would be the same (inaccurate) number.

  3. added a commit that references this issue on Mar 25, 2015
    4b1cfd7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions