Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- #include <stdlib.h>
- using namespace std;
- struct Circle {
- int x;
- int y;
- int radius;
- };
- struct Rectangle {
- int x;
- int y;
- int width;
- int height;
- };
- bool CirclesCollision(Circle circle1, Circle circle2);
- bool RectangleCircleCollision(Circle circle1, Rectangle rectangle1);
- void createBoundingBox(Circle circle1);
- int main() {
- Circle circle1;
- // Circle circle2;
- Rectangle rectangle1;
- cout << "Enter circle1's center X:";
- cin >> circle1.x;
- cout << "Enter circle1's center Y:";
- cin >> circle1.y;
- cout << "Enter circle1's radius:";
- cin >> circle1.radius;
- /*
- cout << "Enter circle2's center X:";
- cin >> circle2.x;
- cout << "Enter circle2's center Y:";
- cin >> circle2.y;
- cout << "Enter circle2's radius:";
- cin >> circle2.radius;
- */
- cout << "Enter rectangle1's center X:";
- cin >> rectangle1.x;
- cout << "Enter rectangle1's center Y:";
- cin >> rectangle1.y;
- cout << "Enter rectangle1's width:";
- cin >> rectangle1.width;
- cout << "Enter rectangle1's height:";
- cin >> rectangle1.height;
- //cout << (CirclesCollision(circle1, circle2) ? "Circles Collision detected!" : "No collision detected!");
- cout << (RectangleCircleCollision(circle1, rectangle1) ? "\nRectangle <-> Circle Collision detected!" : "\nNo collision detected!");
- createBoundingBox(circle1);
- return 0;
- }
- bool CirclesCollision(Circle circle1, Circle circle2) {
- int x_axis = circle1.x - circle2.x;
- int y_axis = circle1.y - circle2.y;
- int dist = sqrt((x_axis * x_axis) + (y_axis * y_axis));
- return dist < circle1.radius + circle2.radius ? true : false;
- }
- /*
- bool RectangleCircleCollision(Circle circle1, Rectangle rectangle1) {
- int dist_x = abs(circle1.x - rectangle1.x);
- int dist_y = abs(circle1.y - rectangle1.y);
- if (dist_x > (rectangle1.width / 2 + circle1.radius))
- return false;
- if(dist_y > (rectangle1.height / 2 + circle1.radius))
- return false;
- if(dist_x <= (rectangle1.width / 2))
- return true;
- if(dist_y <= (rectangle1.height / 2))
- return true;
- int dest_x = dist_x - rectangle1.width / 2;
- int dest_y = dist_y - rectangle1.height / 2;
- return (dest_x * dest_x + dest_y * dest_y <= (circle1.radius * circle1.radius));
- }*/
- bool RectangleCircleCollision(Circle circle1, Rectangle rectangle1) {
- int half_x = (rectangle1.width / 2);
- int half_y = (rectangle1.height / 2);
- int center_x = (circle1.x - (rectangle1.x + half_x));
- int center_y = (circle1.y - (rectangle1.y + half_y));
- int side_x = (abs(center_x) - half_x);
- int side_y = (abs(center_y) - half_y);
- if(side_x > circle1.radius || side_y > circle1.radius) // outside
- return false;
- if(side_x < -circle1.radius && side_y < -circle1.radius) // inside
- return true;
- if(side_x < 0 || side_y < 0) // sidecorner
- return true;
- return side_x * side_x + side_y * side_y < circle1.radius * circle1.radius;
- }
- void createBoundingBox(Circle circle1) {
- int dy1 = circle1.y + circle1.radius;
- int dy2 = circle1.y - circle1.radius;
- int dx1 = circle1.x - circle1.radius;
- int dx2 = circle1.x + circle1.radius;
- int x1 = (dy1 + dx1) - circle1.radius;
- int x2 = (dy1 + dx2) - circle1.radius;
- int x3 = (dy2 + dx2) + circle1.radius;
- int x4 = (dy2 + dx1) + circle1.radius;
- cout << "\nCoordinates of the upper left corner of the box: (x: " << x1 << " y: " << dy1 << ")" << endl;
- cout << "\nCoordinates of the upper right corner of the box: (x: " << x2 << " y: " << dy1 << ")" << endl;
- cout << "\nCoordinates of the lower right corner of the box: (x: " << x3 << " y: " << dy2 << ")" << endl;
- cout << "\nCoordinates of the lower left corner of the box: (x: " << x4 << " y: " << dy2 << ")" << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement