Skip to content

Bug in overlaps rect ? #19

@azergante

Description

@azergante

The overlap function looks funny:

sides :: (using rect: Rect) -> float, float, float, float {
    return x, x + w, y, y + h;
}

overlaps :: (a: Rect, b: Rect) -> bool {
    La, Ra, Ta, Ba := sides(a);
    Lb, Rb, Tb, Bb := sides(b);
    return !(Ra < Lb || Rb < La || Ba < Tb || Bb < Ta);
}

The top of a rectangle has to be less than the bottom of the other rectangle for them not to overlap, so the return line should be return !(Ra < Lb || Rb < La || Tb < Ba || Ta < Bb);

The following drawing illustrates the point:

Screenshot from 2024-03-08 23 28 39

Though the sides function may also be wrong in a way that makes the whole code work as expected: if (x, y) is the bottom left of the rectangle instead of the top left (using Simp) then sides actually returns Left, Right, Bottom, Top. Top and Bottom would be reversed making the return condition work fine, and the code extremely confusing.

nit: you might want to use <= rather than < so you can easily define rectangles that touch each other without overlapping (like RectA(x=0, y=0, w=10, h=10) and RectB(10, 0, 10, 10))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No 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