You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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))
The text was updated successfully, but these errors were encountered:
The overlap function looks funny:
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:
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) thensides
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))The text was updated successfully, but these errors were encountered: