Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in overlaps rect ? #19

Open
azergante opened this issue Mar 8, 2024 · 0 comments
Open

Bug in overlaps rect ? #19

azergante opened this issue Mar 8, 2024 · 0 comments

Comments

@azergante
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant