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 is at the top left corner of the screen. The xcoordinates of
pixels increase as we move toward the right side of the screen, while the ycoordinates increase as
we move toward the bottom of the screen.
In this kind of coordinate system, a point ie a single pixel on screen can be represented as two
integers, the x and ycoordinates of the point. For example, we could create a C structure to
represent a point like this:
struct point
int x xcoordinate of point
int y ; ycoordinate of point
;
A rectangle on the screen can easily be represented using four integers: the x and ycoordinates
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 ; width of rectangle
int height ; 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 isinsideconst 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
insidecpp 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: y:
r origin: x: y: width: height:
Returns: true
Input: p x: y:
r origin: x: y: width: height:
Returns: false
Input: p x: y:
r origin: x: y: width: height:
Returns: true
Input: p x : y:
r origin: x: y: width: height:
Returns: false
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
