Question: Please separate the question's code to the answer! Given the following C++ object definition for a 2-dimensional point (Point class provided), write an object definition

Please separate the question's code to the answer! Given the following C++ object definition for a 2-dimensional point (Point class provided), write an object definition for a rectangle (a class). Your code should include both the definition (i.e. rectangle.h) and the implementation (i.e. rectangle.cpp) of the rectangle class. You only need to write both parts in the single box below. It should be designed so that it stores only rectangles with sides parallel to the x and y axes (i.e. not arbitrary quadrilaterals or rotated rectangles). Your rectangle object should contain:

a class name

two member variables of the Point class (like pt1 and pt2. see the example image.)

two constructors

default constructor

constructor that accepts diagonally opposite corners as 2-dimensional points using the Point class provided (i.e., store the x,y coordinates into the two points, pt1 and pt2).

methods (member functions)

set the position of the rectangle and size of the rectangle (use parameters to modify the two points (pt1, pt2) by repositioning and sizing the rectangle)

draw the rectangle as a blue wireframe (emphasizing its corners as red dots. Remember, you can use x,y values of the two points (pt1, pt2) to make the other two points)

report whether or not a given point is inside the rectangle (did you click inside the rectangle?)

hint: you won't be using all of the Point class, but it is a good example to model after on how to code the rectangle class (i.e., a good reminder of the syntax for classes).

note: the pt1 and pt2 in the image are just placeholders showing where the two diagonal points might lie on a rectangle. You are not drawing the words pt1 and pt2.

#define PI 3.1415926536

#define CIRC_INC (2 * PI / 36)

#define HIT_RADIUS 5

// Draw filled dot with center at (x,y) and radius r

void drawDot(float x, float y, float r) {

float theta;

glBegin(GL_POLYGON);

for(theta=0; theta

glVertex2f(x + r * cos(theta), y + r * sin(theta)); glEnd();

}

// 2-Dimensional Point class "the dot"

class Point {

public:

float x, y; // dot coordinates

Point(void) { x = 0; y = 0; } // default constructor

Point(float x0, float y0) { setpoint(x0, y0); } // constructor (x0, y0)

void setpoint(float newx, float newy); // set to (newx, newy)

void translate(float dx, float dy); // move by (dx, dy)

bool hit(Point cursor); // returns TRUE if cursor near dot

void draw(void); // draw the dot

};

// Position the dot at (newx, newy)

void Point::setpoint(float newx, float newy) {

x = newx; y = newy;

}

// Move the dot dx in the x direction, dy in the y direction

void Point::translate(float dx, float dy) {

x += dx;

y += dy;

}

// Return TRUE if cursor is within HIT_RADIUS of dot, FALSE otherwise

bool Point::hit(Point cursor) {

return(sqr(x cursor.x) + sqr(y cursor.y)

}

// Draw the dot in red using OpenGL

void Point::draw(void) {

glColor3f(1, 0, 0);

drawDot(x, y, HIT_RADIUS);

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!