jaibreak icon indicating copy to clipboard operation
jaibreak copied to clipboard

Bug in overlaps rect ?

Open azergante opened this issue 1 year ago • 0 comments

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

azergante avatar Mar 08 '24 22:03 azergante