Skip to content

Suspicious handling of a number (result of area) as a boolean #141

Description

@OndrejSpanel

Is handling the number result of the area call as a boolean value intended in the snippet below?

earcut/src/earcut.js

Lines 476 to 481 in 36742a5

function isValidDiagonal(a, b) {
return a.next.i !== b.i && a.prev.i !== b.i && !intersectsPolygon(a, b) && // dones't intersect other edges
(locallyInside(a, b) && locallyInside(b, a) && middleInside(a, b) && // locally visible
(area(a.prev, a, b.prev) || area(a, b.prev, b)) || // does not create opposite-facing sectors
equals(a, b) && area(a.prev, a, a.next) > 0 && area(b.prev, b, b.next) > 0); // special zero-length case
}

As I understand the code now, it checks that at least one of the triangles a.prev, a, b.prev and a, b.prev, b has non zero area. I do not understand the intent well enough, the comment says "does not create opposite-facing sectors".

If the code is correct, perhaps an explicit comparion using !== 0 would make the intent clearer?

(Originally reported as mrdoob/three.js#20200)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions