Skip to content

Instantly share code, notes, and snippets.

@chrisdel101
Created March 22, 2020 18:03
Show Gist options
  • Save chrisdel101/23b2b7ab80930cff9389e3a592a3a328 to your computer and use it in GitHub Desktop.
Save chrisdel101/23b2b7ab80930cff9389e3a592a3a328 to your computer and use it in GitHub Desktop.
bool Rectangle::isIntersection(const Rectangle &otherRect) const
{
assert(invariant());
// cout << "current " << getColumnCount() << endl;
// cout << "current " << getMinColumn() << endl;
// cout << "other " << otherRect.getColumnCount() << endl;
// cout << "other " << otherRect.getMinColumn() << endl;
int currentRectCheck = abs(getMinColumn()) + abs(getColumnCount()) - 2;
int otherRectCheck = abs(otherRect.getMinColumn()) - abs(otherRect.getColumnCount()) + 2;
// cout << "check 1 " << currentRectCheck << endl;
// cout << "check 2 " << otherRectCheck << endl;
;
// col with second rect can only occur in this range
if (abs(currentRectCheck) <= abs(otherRectCheck))
{
// cout << "row check 1 " << getMinRow() << endl;
// cout << "row check 1 " << getRowCount() << endl;
// cout << "row check 2 " << otherRect.getMinRow() << endl;
// cout << "row check 2 " << otherRect.getRowCount() << endl;
// cout << "row check 3 " << abs(otherRect.getMinRow()) - 1 + abs(otherRect.getRowCount()) << endl;
// cout << "row check 4 " << (abs(getMinRow()) + abs(getRowCount()) - 1) << endl;
// cout << (abs(getMinRow()) > (abs(otherRect.getMinRow()) - 1 + abs(otherRect.getRowCount()))) << endl;
// cout << ((abs(getMinRow()) + abs(getRowCount())) - 1 < abs(otherRect.getMinRow())) << endl;
// check if rows overlap
if (
(
abs(getMinRow()) > (abs(otherRect.getMinRow()) - 1 + abs(otherRect.getRowCount())) ||
(abs(getMinRow()) + abs(getRowCount())) - 1 < abs(otherRect.getMinRow())))
{
return false;
}
// cout << "true" << endl;
return true;
}
return false;
assert(invariant());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment