Advertisement
_code_feedback_

Task 1: AARectangles main.cpp

Apr 2nd, 2019
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.71 KB | None | 0 0
  1. /*
  2.     File: main.cpp
  3.  
  4.     Task: Task 1: Axis Aligned Rectangles
  5.  
  6.     Author: *REDACTED*
  7.  
  8.     Created: *REDACTED*
  9.  
  10.     Desc: File containing the main function implementing a solution to task 1 of the assessment
  11.         dealing with Axis Aligned Rectangles. Also contains unit testing for the task
  12. */
  13.  
  14.  
  15. #include <iostream>
  16. #include <conio.h>
  17. #include <assert.h>
  18. #include "AA_Rectangle.h"
  19.  
  20. #define RUN_UNIT_TESTS 1
  21.  
  22. using namespace std;
  23.  
  24. void RunUnitTests()
  25. {
  26.     cout << "Running Unit tests...\n" << endl;
  27.  
  28.     /* Testing Points in Rectangle */
  29.  
  30.     // Create a AA_Rectangle at the origin with size 5, 6
  31.     AA_Rectangle * testRectangle = new AA_Rectangle(NULL, 5.0f, 6.0f);
  32.  
  33.     // Create a test point inside the rectangle
  34.     Point2D * insideTestPoint = new Point2D(1.0f, 1.0f);
  35.  
  36.     // Create a test point outside the rectangle
  37.     Point2D * outsideTestPoint = new Point2D(7.0f, -3.0f);
  38.  
  39.     assert(testRectangle->isPointInRectangle(insideTestPoint));
  40.  
  41.     assert(!(testRectangle->isPointInRectangle(outsideTestPoint)));
  42.  
  43.     /* Testing AA_Rectangle intersections */
  44.  
  45.     // Create an AA_Rectangle that does not intersect with testRectangle
  46.     AA_Rectangle * outsideTest = new AA_Rectangle(7.0f, 7.0f, 2.0f, 3.0f);
  47.  
  48.     // Create an AA_Rectangle that does intersect
  49.     AA_Rectangle * insideTest = new AA_Rectangle(1.0f, 1.0f, 5.0f, 6.0f);
  50.  
  51.     cout << "Result of outside test: " << testRectangle->isRectangleIntersecting(outsideTest) << endl;
  52.  
  53.     cout << "Result of inside test: " << testRectangle->isRectangleIntersecting(insideTest) << endl;
  54.  
  55.     assert(!(testRectangle->isRectangleIntersecting(outsideTest)));
  56.  
  57.     assert(testRectangle->isRectangleIntersecting(insideTest));
  58.  
  59.     // Cleanup
  60.     delete testRectangle;
  61.     delete outsideTest;
  62.     delete insideTest;
  63.     delete outsideTestPoint;
  64.     delete insideTestPoint;
  65.  
  66.     cout << "Unit Testing complete!" << endl;
  67. }
  68.  
  69. int main()
  70. {
  71.     cout << "Task 1: Axis Aligned Rectangles\n" << endl;
  72.  
  73.     // Basic point test
  74.     Point2D * origin1 = new Point2D();
  75.     Point2D * testPoint = new Point2D(1.0f, 1.0f);
  76.     AA_Rectangle * test1 = new AA_Rectangle(origin1, 5.0f, 5.0f);
  77.  
  78.     if (test1->isPointInRectangle(testPoint))
  79.     {
  80.         cout << "Test point is in rectangle\n\n";
  81.     }
  82.     else
  83.     {
  84.         cout << "Test point is not in rectangle\n\n";
  85.     }
  86.  
  87.     // Run some unit tests (guarded by a macro) to check the AA_Rectangle class provides expected behavior
  88. #if RUN_UNIT_TESTS
  89.     RunUnitTests();
  90. #endif
  91.  
  92.     // Cleanup
  93.     delete origin1;
  94.     delete testPoint;
  95.     delete test1;
  96.  
  97.     // Dirty getch to see terminal output before termination
  98.     _getch();
  99.  
  100.     return 0;
  101. }
  102.  
  103. /* NB:
  104. Things that can be done to improve this solution:
  105.  
  106. - Move floating point comparisons to use epsilons instead of straight comparisons, e.g. refPoint->x - mBottomLeft->x > epsilon
  107. allowing for better control over floating point accuracy.
  108.  
  109. - Rework AA_Rectangle class to better adhere to data encapsulation (right now its all public!).
  110. Decided to keep this simple since there is little context as to this classes overall usage. Saves having to write and test accessors.
  111.  
  112. - Could potentially extend the Point2D class to better deal with point vs point comparisons.
  113. Instead of explicitly having to test the x, y values when comparing points, could possibly offload that to the Point2D class in the form of a public method.
  114.  
  115. - Better asserts and logging. Again, kept things simple for this task, but I am usually weary of using simple assert().
  116. Would be ideal to use something that writes to the output log. Thought writing a logger class just for this task might be a bit overkill.
  117.  
  118. - Improve Rectangle intersection checks to return point of intersection if applicable.
  119. Would require a rework of the algorithm being used right now. Kept things simple so as to address only what was asked in the task.
  120. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement