# Practice 8.

## Points and rectangles

Write a program which represents *rectangles* using *points*.

Create functions to compute the area and the periphery of the rectangle,
and functions to decide whether a point is inside or on the rectangle,
and one more to decide whether the rectangle is a square.

### Solution

Video:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149

#include <iostream>
#include <cmath>
#include <ios>
#include <vector>
#include <cassert>
struct Point // representing a point
{
double x;
double y;
};
struct Rectangle // representing s rectangle
{
Point leftLower; // with two points: the left lower
Point rightUpper; // and the right upper
Rectangle(Point ll, Point ru) // constructor
{
assert(ll.x < ru.x); // check, that the parameters
assert(ll.y < ru.y); // are valid, otherwise abort
leftLower = ll; // set the attributes
rightUpper = ru;
}
};
double pointDistance(const Point& p, const Point& r);
Point rightLower(const Rectangle& r);
double area(const Rectangle& r);
double periphery(const Rectangle& r);
bool isSquare(const Rectangle& r);
bool isInnerPoint(const Rectangle& r, const Point& p);
bool isPointOnSide(const Rectangle& r, const Point& p);
int main()
{
Point p = { 3, 2 }; // aggregate initialization
Point q; // no initialization
q.x = 9;
q.y = 7;
Rectangle rect = { { 3, 2 }, { 9 , 7 } };
// this is also a proper constructor call
Rectangle rect2( { 3, 4 }, { 6 , 7 } );
std::cout << pointDistance(p, q) << std::endl;
std::cout << "Area of the rectangle is: "
<< area(rect) << std::endl;
std::cout << "Periphery of the rectangle is: "
<< periphery(rect) << std::endl;
std::cout << std::boolalpha << isSquare(rect)
<< std::endl;
// std::noboolalpha to disable bool to string conversion
std::cout << isSquare(rect2) << std::endl;
std::cout << isInnerPoint(rect, Point{6, 4})
<< std::endl;
std::cout << isInnerPoint(rect, Point{-5, -2})
<< std::endl;
srand(time(0)); // seed: "randomize" the random generator
std::vector<Point> points;
for (int i = 0; i < 10; ++i)
{
// generate random points
points.push_back(Point{(double)(rand() % 10),
(double)(rand() % 10)});
std::cout << "(" << points[i].x << ", "
<< points[i].y << ") ";
if (isInnerPoint(rect, points[i]))
{
std::cout << "is inside the rectangle.\n";
}
else if (isPointOnSide(rect, points[i]))
{
std::cout << "is on the rectangle.\n";
}
else
{
std::cout << "is outside the rectangle.\n";
}
}
}
double area(const Rectangle& r)
{
Point rightLowerPoint = rightLower(r);
double a = pointDistance(rightLowerPoint, r.leftLower);
double b = pointDistance(r.rightUpper, rightLowerPoint);
return a * b;
}
double periphery(const Rectangle& r)
{
Point rightLowerPoint = rightLower(r);
double a = pointDistance(rightLowerPoint, r.leftLower);
double b = pointDistance(r.rightUpper, rightLowerPoint);
return 2 * a + 2 * b;
}
bool isSquare(const Rectangle& r)
{
if (std::abs(r.leftLower.x - r.rightUpper.x) ==
std::abs(r.leftLower.y - r.rightUpper.y))
{
return true;
}
return false;
}
bool isInnerPoint(const Rectangle& r, const Point& p)
{
if (p.x < r.rightUpper.x && p.x > r.leftLower.x &&
p.y < r.rightUpper.y && p.y > r.leftLower.y)
{
return true;
}
return false;
}
bool isPointOnSide(const Rectangle& r, const Point& p)
{
if ((p.x == r.leftLower.x || p.x == r.rightUpper.x) &&
p.y > r.leftLower.y && p.y < r.rightUpper.y ||
(p.y == r.leftLower.y || p.y == r.rightUpper.y) &&
p.x > r.leftLower.x && p.x < r.rightUpper.x)
{
return true;
}
return false;
}
double pointDistance(const Point& p, const Point& r)
{
return std::sqrt(std::pow(p.x - r.x, 2.0)
+ std::pow(p.y - r.y, 2.0));
}
Point rightLower(const Rectangle& r)
{
double x = r.rightUpper.x;
double y = r.leftLower.y;
return Point{ x, y }; // temporary object
}

The expected output:

*Financed from the financial support ELTE won from the Higher Education Restructuring Fund of the Hungarian Government.*