Advertisement
Garey

zad2

Oct 20th, 2017
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.05 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //     PROGRAM: Calculate Bounding Box Of A Circle
  3. // DESCRIPTION: It calculates the bounding box of circles
  4. //              and checks if they collided alongside the boxes.
  5. //      AUTHOR: Stf Kolew
  6. // AUTHOR/DATE: JP 2017-10-20
  7. ////////////////////////////////////////////////////////////////////////////////
  8. #include <iostream>
  9. #include <math.h>
  10. #include <stdlib.h>
  11.  
  12. using namespace std;
  13.  
  14. struct Circle {
  15.   int x;
  16.   int y;
  17.   int radius;
  18. };
  19.  
  20. struct BoundingBox {
  21.   int x;
  22.   int y;
  23.   int width;
  24.   int height;
  25. };
  26.  
  27. ////////////////////////////////////////////////////////////////////////////////
  28. //        NAME: createOverallBoundingBox
  29. // DESCRIPTION: Checks if the two bounding boxes collide. If they do
  30. //              creates bounding box and gets its top, bottom, left
  31. //              and right max points. Then uses them to computate the
  32. //              lower-left corner and the top-right corner points of
  33. //              the overall bounding box. Else, it gives a warning
  34. //              that the two bounding boxes did not collide and
  35. //              it cannot computate the points for the overall box.
  36. //   ARGUMENTS: BoundingBox bbox1, BoundingBox bbox2
  37. // USES GLOBAL: none
  38. // MODIFIES GL: none
  39. //     RETURNS: output
  40. //      AUTHOR: Stf Kolew
  41. // AUTHOR/DATE: JP 2017-10-20
  42. ////////////////////////////////////////////////////////////////////////////////
  43. void createOverallBoundingBox(BoundingBox bbox1, BoundingBox bbox2);
  44.  
  45. ////////////////////////////////////////////////////////////////////////////////
  46. //        NAME: calculateBoundingBox
  47. // DESCRIPTION: Calculates the bounding box of a circle given the circle
  48. //              variable to get the needed information and bbox variable
  49. //              to set it's values and pass it by reference.
  50. //   ARGUMENTS: Circle circle, BoundingBox &bbox
  51. // USES GLOBAL: none
  52. // MODIFIES GL: none
  53. //     RETURNS: output
  54. //      AUTHOR: Stf Kolew
  55. // AUTHOR/DATE: JP 2017-10-20
  56. ////////////////////////////////////////////////////////////////////////////////
  57. void calculateBoundingBox(Circle circle, BoundingBox &bbox);
  58.  
  59. ////////////////////////////////////////////////////////////////////////////////
  60. //        NAME: calculateBoundingBox
  61. // DESCRIPTION: Asks the user to input data about the circle like its'
  62. //              center points(x and y) and its' radius.
  63. //   ARGUMENTS: Circle &circle
  64. // USES GLOBAL: none
  65. // MODIFIES GL: none
  66. //     RETURNS: output
  67. //      AUTHOR: Stf Kolew
  68. // AUTHOR/DATE: JP 2017-10-20
  69. ////////////////////////////////////////////////////////////////////////////////
  70. void inputCircle(Circle &circle);
  71.  
  72. ////////////////////////////////////////////////////////////////////////////////
  73. //        NAME: main
  74. // DESCRIPTION: Declares the needed variables and inputs data for them.
  75. //              After declared each of the functions created are called.
  76. //              They are documented above.
  77. //   ARGUMENTS: none
  78. // USES GLOBAL: none
  79. // MODIFIES GL: none
  80. //     RETURNS: output
  81. //      AUTHOR: Stf Kolew
  82. // AUTHOR/DATE: JP 2017-10-20
  83. ////////////////////////////////////////////////////////////////////////////////
  84. int main() {
  85.  
  86.   Circle circle1, circle2;
  87.   BoundingBox bbox1, bbox2;
  88.  
  89.   inputCircle(circle1);
  90.   inputCircle(circle2);
  91.  
  92.   calculateBoundingBox(circle1, bbox1);
  93.   calculateBoundingBox(circle2, bbox2);
  94.  
  95.   createOverallBoundingBox(bbox1, bbox2);
  96.  
  97.   return 0;
  98. }
  99.  
  100. bool BoundingBoxesCollision(BoundingBox bbox1, BoundingBox bbox2) {
  101.   if (bbox1.x + bbox1.width < bbox2.x || bbox1.x > bbox2.x + bbox2.width) return false;
  102.   if (bbox1.y + bbox1.height < bbox2.y || bbox1.y > bbox2.y + bbox2.height) return false;
  103.  
  104.   return true;
  105. }
  106.  
  107. void calculateBoundingBox(Circle circle, BoundingBox &bbox) {
  108.  
  109.   bbox.x = circle.x;
  110.   bbox.y = circle.y;
  111.   bbox.width = bbox.height = circle.radius + circle.radius;
  112. }
  113.  
  114. void inputCircle(Circle &circle) {
  115.     cout << "Enter circle's center X:";
  116.     cin >> circle.x;
  117.     cout << "Enter circle's center Y:";
  118.     cin >> circle.y;
  119.     cout << "Enter circle1's radius:";
  120.     cin >> circle.radius;
  121. }
  122.  
  123. void createOverallBoundingBox(BoundingBox bbox1, BoundingBox bbox2) {
  124.   if(BoundingBoxesCollision(bbox1, bbox2)) {
  125.     int top_point = 0;
  126.     int left_point = 0;
  127.     int bot_point = 0;
  128.     int right_point = 0;
  129.  
  130.     if(bbox1.y > bbox2.y) {
  131.       top_point = bbox1.y + (bbox1.height / 2);
  132.       bot_point = bbox2.y - (bbox2.height / 2);
  133.     } else {
  134.       top_point = bbox2.y + (bbox2.height / 2);
  135.       bot_point = bbox1.y - (bbox1.height / 2);
  136.     }
  137.  
  138.     if(bbox1.x > bbox2.x) {
  139.       right_point = bbox1.x + (bbox1.width / 2);
  140.       left_point = bbox2.x - (bbox2.width / 2);
  141.     } else {
  142.       right_point = bbox2.x + (bbox2.width / 2);
  143.       left_point = bbox1.x - (bbox1.width / 2);
  144.     }
  145.  
  146.     cout << "\nLower Left corner: (" << left_point << ", " << bot_point << ")\n";
  147.     cout << "Top Right corner: (" << right_point << ", " << top_point << ")\n";
  148.   } else {
  149.     cout << "No collision between objects detected!";
  150.   }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement