public void checkInteraction(Rect otherRect) { //Is the other rect inside this rect? Are all four of its points contained? if(ContainsPoint(otherRect.topLeft) && ContainsPoint(otherRect.topRight) && ContainsPoint(otherRect.botLeft) && ContainsPoint(otherRect.botRight)) { containing = true; otherRect.contained = true; } //Is the other rect intersecting this rect? Is at least one corner contained? else if(ContainsPoint(otherRect.topLeft) || ContainsPoint(otherRect.topRight) || ContainsPoint(otherRect.botLeft) || ContainsPoint(otherRect.botRight)) { intersecting = true; otherRect.intersecting = true; determineIntersections(otherRect); } //Are the two overlapping in a T shape, where no corners are contained but four intersections still occur? else if(CheckForT(otherRect)) { intersecting = true; otherRect.intersecting = true; intPoints[0].changePos(topLeft.X, otherRect.topLeft.Y); intPoints[1].changePos(topRight.X, otherRect.topRight.Y); intPoints[2].changePos(botLeft.X, otherRect.botLeft.Y); intPoints[3].changePos(botRight.X, otherRect.botRight.Y); } //After all other checks, check each side for adjacency CheckForAdj(otherRect); }
bool CheckForT(Rect otherRect) { //If all the points of otherRect are below the top of this one, and above the bottom of this one, //and all the points of this one are to the right of otherRect's left, and to the left of otherRect's right, //the two are colliding in a T. //We're checking for this: /* _____ _______|___|__ | | | | | | | | -------|---|--| |___| */ bool otherBetweenTopAndBot; bool thisBetweenLeftAndRight; otherBetweenTopAndBot = (otherRect.topLeft.Y >= topLeft.Y && otherRect.botRight.Y >= topLeft.Y && otherRect.topLeft.Y <= botRight.Y && otherRect.botRight.Y <= botRight.Y); thisBetweenLeftAndRight = (topLeft.X >= otherRect.topLeft.X && topRight.X >= otherRect.topLeft.X && topLeft.X <= otherRect.botRight.X && topRight.X <= otherRect.botRight.X); return otherBetweenTopAndBot && thisBetweenLeftAndRight; }
void determineIntersections(Rect otherRect) { //First, we check for the top left corner. Then, check adjacent corners, and determine intersections //based on those adjacent corners. if(ContainsPoint(otherRect.topLeft)) { //These series of cases cover the first three of eight potential outcomes: //the top left and top right corners are inside, //and the top left and bottom left corners are inside, //and only the top left corner is inside, if (ContainsPoint(otherRect.topRight)) { intPoints[0].changePos(otherRect.topLeft.X, botLeft.Y); intPoints[1].changePos(otherRect.topRight.X, botLeft.Y); } else if(ContainsPoint(otherRect.botLeft)) { intPoints[0].changePos(topRight.X, otherRect.topLeft.Y); intPoints[1].changePos(topRight.X, otherRect.botLeft.Y); } else { intPoints[0].changePos(otherRect.topLeft.X, botRight.Y); intPoints[1].changePos(botRight.X, otherRect.topLeft.Y); } } //We do the same with the bottom right. This covers three more cases, //The bottom right and bottom left corners are inside, //the bottom right and top right corners are inside, //and only the bottom right corner is inside. else if(ContainsPoint(otherRect.botRight)) { if(ContainsPoint(otherRect.botLeft)) { intPoints[0].changePos(otherRect.botLeft.X, topLeft.Y); intPoints[1].changePos(otherRect.botRight.X, topLeft.Y); } else if(ContainsPoint(otherRect.topRight)) { intPoints[0].changePos(topLeft.X, otherRect.topRight.Y); intPoints[1].changePos(topLeft.X, otherRect.botRight.Y); } else { intPoints[0].changePos(otherRect.botRight.X, topLeft.Y); intPoints[1].changePos(topLeft.X, otherRect.botRight.Y); } } //At this point, there are only two unaccounted for cases: //Only the bottom left is inside, and only the top right is inside. //First, we check the bottom left. else if(ContainsPoint(otherRect.botLeft)) { intPoints[0].changePos(otherRect.botLeft.X, topRight.Y); intPoints[1].changePos(topRight.X, otherRect.botLeft.Y); } //If we have reached here, the intersecting point must be the top right. else { intPoints[0].changePos(otherRect.topRight.X, botLeft.Y); intPoints[1].changePos(botLeft.X, otherRect.topRight.Y); } }
void CheckForAdj(Rect otherRect) { //If the left is the same as or a subset of the other's right or left, color it yellow. //Likewise, do the same for right, top and bottom. //All sides are transparent until proven to be adjacent for(int i = 0; i < 4; i++) { for(int j = 0; j < 4; j++) { //The top and bottom are even indices, the right and left are odd indices. //Only check against the indices which have the same even or odd property. if( (i % 2) == (j % 2) ) { if(i % 2 == 0) { if (Math.Abs(lines[i].startPoint.Y - otherRect.lines[j].startPoint.Y) < float.Epsilon && lines[i].startPoint.X >= otherRect.lines[j].startPoint.X && lines[i].endPoint.X <= otherRect.lines[j].endPoint.X) { adjacent = true; lines[i].adjacent = true; } } else { //if(Math.Abs(topLeft.X - otherRect.topLeft.X) < float.Epsilon && topLeft.Y >= otherRect.topLeft.Y && botLeft.Y <= otherRect.botLeft.Y) if (Math.Abs(lines[i].startPoint.X - otherRect.lines[j].startPoint.X) < float.Epsilon && lines[i].startPoint.Y >= otherRect.lines[j].startPoint.Y && lines[i].endPoint.Y <= otherRect.lines[j].endPoint.Y) { adjacent = true; lines[i].adjacent = true; } } } } } }