Question: Is a Point Inside a Rectangle? Computers typically use a coordinate system when displaying graphics. The fundamental unit of a computer screen is typically the

Is a Point Inside a Rectangle?
Computers typically use a coordinate system when displaying graphics. The fundamental unit of a
computer screen is typically the pixel, a tiny square on the screen that cant be subdivided.
Generally, the pixel at coordinates (0,0) is at the top left corner of the screen. The x-coordinates of
pixels increase as we move toward the right side of the screen, while the y-coordinates increase as
we move toward the bottom of the screen.
In this kind of coordinate system, a point (i.e., a single pixel) on screen can be represented as two
integers, the x- and y-coordinates of the point. For example, we could create a C++ structure to
represent a point like this:
struct point
{
int x =0,// x-coordinate of point
int y =0; // y-coordinate of point
};
A rectangle on the screen can easily be represented using four integers: the x- and y-coordinates
of the upper left vertex of the rectangle, the width of the rectangle, and the height of the rectangle.
For example, a C++ structure to represent a rectangle on screen might look like this:
struct rectangle
{
point origin; // point representing upper left vertex of rectangle
int width =0; // width of rectangle
int height =0; // height of rectangle
};
height
width
(x, y)
(x, y)
Write a function to determine whether a given point lies inside a specified rectangle. The point is
inside the rectangle if it lies within the boundaries of the rectangle or on one of its four edges.
bool is_inside(const point& p, const rectangle& r);
The definitions for the point and rectangle structures are available in a header file named
shapes.h.
Files We Give You: A makefile , the header file shapes.h, and a sample main program
(inside.cpp) to test your solution. The executable file created by a successful build will be
named inside.
File You Must Submit: Place your solution code in a file named solution.cpp. You will need to
#include "shapes.h" at the top of the file in order to access the point and rectangle structures.
This will be the only file that you submit.
Examples
Input: p =[x: 25, y: 35],
r =[origin: [x: 20, y: 30], width: 20, height: 15]
Returns: true
Input: p =[ x: 12, y: 35],
r =[ origin: [x: 20, y: 30], width: 20, height: 15]
Returns: false
Input: p =[x: 40, y: 17],
r =[origin: [x: 40, y: 15], width: 5, height: 8]
Returns: true
Input: p =[x : 60, y: 23],
r =[origin: [x: 40, y: 15], width: 5, height: 8]
Returns: false

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 Programming Questions!