// In this lab you are asked to // (1) Add the member function IsPointInside(..) to myRect // (2) Write the function IntersectingRectangle that takes two // myRect structs and returns a myRect struct containing // the intersection rectangle of the arguments // (3) Add color to myRect, so that the color a rectangle in // drawn in is the same and is specified when a myRect gets // created (thus no need for SetForColor(..) in the main() below). #include "SWindows.h" #include "Graphics.h" #include "Mouse.h" #include "Delay.h" #include "Text.h" #include "IOTools.h" #include "MathUtil.h" #include "Random.h" // for RandomLong(..) // On a PC, uncomment the includes below and comment out the includes above. //#include "IOTools.h" //#include "MathUtil.h" //#include "Random.h" // for RandomLong(..) //#include "ct_windows.h" //#include "ct_graphics.h" //#include "ct_mouse.h" //#include "ct_delay.h" //#include "ct_text.h" #include //On a PC, uncomment these macros if the compiler says //that min and max are undefined: //#define max(a,b) (((a) > (b)) ? (a) : (b)) //#define min(a,b) (((a) < (b)) ? (a) : (b)) // myDot is represented as a circle struct myDot { // x and y are the co-ordinates of the center of the dot int x_center; int y_center; int radius; myDot() { x_center = 0; y_center = 0; } myDot(int, int, int); void Draw(); }; // Constructor for myDot - takes in as input parameters // the x-coordinate and y-coordinate of the center of the dot // along with the radius myDot::myDot(int x, int y, int rad) { x_center = x; y_center = y; radius = rad; } // Draws the point on the screen void myDot::Draw() { PaintCircle(x_center, y_center, radius); } // Define a rectangle struct struct myRect { int left; // coordinates of the upper-left corner int top; int right; // coordinates of the lower-right corner int bottom; // Constructor which sets all values = 0 myRect() { left = 0; top = 0; right = 0; bottom = 0;} myRect(int,int,int,int); // ??? The prototype for a member function IsPointInside which takes in a myDot // ??? and checks if the point lies in the rectangle comes here void DrawSolid(); void DrawFrame(); }; // Constructor for myRect myRect::myRect(int x1, int y1, int x2, int y2) { left = min(x1,x2); top = min(y1,y2); right = max(x1,x2); bottom = max(y1,y2); } // Draws the rectangle on the screen as a solid rectangle of current color void myRect::DrawSolid() { PaintRect(left, top, right, bottom); } // Draws the rectangle on the screen as a frame rectangle of current color void myRect::DrawFrame() { FrameRect(left, top, right, bottom); } // ??? Define the function IsPointInside which takes in a myDot and checks // ??? if the point falls within the rectangle myRect BoundingRectangle( const myRect&, const myRect& ); // ??? Write the prototype for IntersectRect // definitions void main() { // open text and drawing windows BigSquarePair(); SetRandomSeed(); // ??? Create an object of the struct myDot with random coordinates // ??? The radius could be fixed at 5 units. // ??? You might want to look at the way rect1 and rect2 are constructed from // ??? random values // ??? Check if the point so created above lies in rect1 by calling the member function of // ??? rect1 - IsPointInside. Print out a message indicating if the point lies inside the // ??? rectangle or not. PressReturn( "10 Bounding and Intersection Rectangles tests" ); ClearDrawing(); // 10 rectangle tests int i = 0; while( i < 10 ){ // Two random rectangles created for the purpose of illustration myRect rect1 ( RandomLong(0,400), RandomLong(0,400), RandomLong(0,400), RandomLong(0,400) ); myRect rect2 ( RandomLong(0,400), RandomLong(0,400), RandomLong(0,400), RandomLong(0,400) ); // Set the color to red for one rectangle, set green for the other // and draw the rectangles SetForeColor(255, 0, 0); // red rect1.DrawFrame(); SetForeColor(0, 255, 0); // green rect2.DrawFrame(); PressReturn("Computing the bounding rectangle"); // BoundingRectangle returns the bounding rectangle. Then DrawFrame() // function of this rectangle is called to show it on the screen. (BoundingRectangle(rect2, rect1)).DrawFrame(); // myRect tmp = BoundingRectangle( rect2, rect1 ); // tmp.DrawFrame(); PressReturn("Computing the intersection rectangle"); // ??? Call IntersectingRectangle and display the result PressReturn(); i++; ClearDrawing(); } PressReturn("Done"); } //Function which draws the rectangle which bounds the two given rectangles myRect BoundingRectangle( const myRect& R1, const myRect& R2 ){ SetForeColor(0,0,255); myRect r; // Find the min and max x-coordinates of both rectangles r.left = min( R1.left, R2.left ); r.right = max( R1.right, R2.right ); // Find the min and max y-coordinates of both rectangles r.top = min( R1.top, R2.top ); r.bottom = max( R1.bottom, R2.bottom ); return r; } // ??? Define the function IntersectRect which determines the rectangle // ??? intersecting the two rectangles passed as parameters // ??? If no such rectangle exists, nothing is painted on the screen