Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////////////////////////////////////////////////////////
- // PROGRAM: Calculate Bounding Box Of A Circle
- // DESCRIPTION: It calculates the bounding box of circles
- // and checks if they collided alongside the boxes.
- // AUTHOR: Stf Kolew
- // AUTHOR/DATE: JP 2017-10-20
- ////////////////////////////////////////////////////////////////////////////////
- #include <iostream>
- #include <math.h>
- #include <stdlib.h>
- using namespace std;
- struct Circle {
- int x;
- int y;
- int radius;
- };
- struct BoundingBox {
- int x;
- int y;
- int width;
- int height;
- };
- ////////////////////////////////////////////////////////////////////////////////
- // NAME: createOverallBoundingBox
- // DESCRIPTION: Checks if the two bounding boxes collide. If they do
- // creates bounding box and gets its top, bottom, left
- // and right max points. Then uses them to computate the
- // lower-left corner and the top-right corner points of
- // the overall bounding box. Else, it gives a warning
- // that the two bounding boxes did not collide and
- // it cannot computate the points for the overall box.
- // ARGUMENTS: BoundingBox bbox1, BoundingBox bbox2
- // USES GLOBAL: none
- // MODIFIES GL: none
- // RETURNS: output
- // AUTHOR: Stf Kolew
- // AUTHOR/DATE: JP 2017-10-20
- ////////////////////////////////////////////////////////////////////////////////
- void createOverallBoundingBox(BoundingBox bbox1, BoundingBox bbox2);
- ////////////////////////////////////////////////////////////////////////////////
- // NAME: calculateBoundingBox
- // DESCRIPTION: Calculates the bounding box of a circle given the circle
- // variable to get the needed information and bbox variable
- // to set it's values and pass it by reference.
- // ARGUMENTS: Circle circle, BoundingBox &bbox
- // USES GLOBAL: none
- // MODIFIES GL: none
- // RETURNS: output
- // AUTHOR: Stf Kolew
- // AUTHOR/DATE: JP 2017-10-20
- ////////////////////////////////////////////////////////////////////////////////
- void calculateBoundingBox(Circle circle, BoundingBox &bbox);
- ////////////////////////////////////////////////////////////////////////////////
- // NAME: calculateBoundingBox
- // DESCRIPTION: Asks the user to input data about the circle like its'
- // center points(x and y) and its' radius.
- // ARGUMENTS: Circle &circle
- // USES GLOBAL: none
- // MODIFIES GL: none
- // RETURNS: output
- // AUTHOR: Stf Kolew
- // AUTHOR/DATE: JP 2017-10-20
- ////////////////////////////////////////////////////////////////////////////////
- void inputCircle(Circle &circle);
- ////////////////////////////////////////////////////////////////////////////////
- // NAME: main
- // DESCRIPTION: Declares the needed variables and inputs data for them.
- // After declared each of the functions created are called.
- // They are documented above.
- // ARGUMENTS: none
- // USES GLOBAL: none
- // MODIFIES GL: none
- // RETURNS: output
- // AUTHOR: Stf Kolew
- // AUTHOR/DATE: JP 2017-10-20
- ////////////////////////////////////////////////////////////////////////////////
- int main() {
- Circle circle1, circle2;
- BoundingBox bbox1, bbox2;
- inputCircle(circle1);
- inputCircle(circle2);
- calculateBoundingBox(circle1, bbox1);
- calculateBoundingBox(circle2, bbox2);
- createOverallBoundingBox(bbox1, bbox2);
- return 0;
- }
- bool BoundingBoxesCollision(BoundingBox bbox1, BoundingBox bbox2) {
- if (bbox1.x + bbox1.width < bbox2.x || bbox1.x > bbox2.x + bbox2.width) return false;
- if (bbox1.y + bbox1.height < bbox2.y || bbox1.y > bbox2.y + bbox2.height) return false;
- return true;
- }
- void calculateBoundingBox(Circle circle, BoundingBox &bbox) {
- bbox.x = circle.x;
- bbox.y = circle.y;
- bbox.width = bbox.height = circle.radius + circle.radius;
- }
- void inputCircle(Circle &circle) {
- cout << "Enter circle's center X:";
- cin >> circle.x;
- cout << "Enter circle's center Y:";
- cin >> circle.y;
- cout << "Enter circle1's radius:";
- cin >> circle.radius;
- }
- void createOverallBoundingBox(BoundingBox bbox1, BoundingBox bbox2) {
- if(BoundingBoxesCollision(bbox1, bbox2)) {
- int top_point = 0;
- int left_point = 0;
- int bot_point = 0;
- int right_point = 0;
- if(bbox1.y > bbox2.y) {
- top_point = bbox1.y + (bbox1.height / 2);
- bot_point = bbox2.y - (bbox2.height / 2);
- } else {
- top_point = bbox2.y + (bbox2.height / 2);
- bot_point = bbox1.y - (bbox1.height / 2);
- }
- if(bbox1.x > bbox2.x) {
- right_point = bbox1.x + (bbox1.width / 2);
- left_point = bbox2.x - (bbox2.width / 2);
- } else {
- right_point = bbox2.x + (bbox2.width / 2);
- left_point = bbox1.x - (bbox1.width / 2);
- }
- cout << "\nLower Left corner: (" << left_point << ", " << bot_point << ")\n";
- cout << "Top Right corner: (" << right_point << ", " << top_point << ")\n";
- } else {
- cout << "No collision between objects detected!";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement