-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResultInRoom.cpp
More file actions
151 lines (130 loc) · 4.88 KB
/
ResultInRoom.cpp
File metadata and controls
151 lines (130 loc) · 4.88 KB
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
150
151
#include"ResultInRoom.h"
#include<math.h>
#include <algorithm>
double IlluminatedSpaceInRoom::calculateFloorCoordinates(double &area)
{
double toCantimeter = area * 100;
return (sqrt(toCantimeter)) / 2;
}
void IlluminatedSpaceInRoom::initializeTheFloor(double &area, double &height)
{
double positiveCoordinate = calculateFloorCoordinates(area);
double negativeCoordinate = -positiveCoordinate;
//the floor - MNPQ
M.x = negativeCoordinate;
M.y = negativeCoordinate;
N.x = positiveCoordinate;
N.y = negativeCoordinate;
P.x = positiveCoordinate;
P.y = positiveCoordinate;
Q.x = negativeCoordinate;
Q.y = positiveCoordinate;
M.z = N.z = P.z = Q.z = height;
}
void IlluminatedSpaceInRoom::swapPoints(point &A, point&B)
{
point swapPoint = A;
A = B;
B = swapPoint;
}
bool IlluminatedSpaceInRoom::IsPointInInterval(double leftCoordinate, double rightCoordinate, double point)
{
if (point >= leftCoordinate && point <= rightCoordinate) return true;
return false;
}
point IlluminatedSpaceInRoom::findPointBetweenTwoLines(point &A, point &C, point &B, point &D)
{
point Res;
Res.z = A.z;
// Line AC represented as a1x + c1y = d1
double a1 = C.y - A.y;
double c1 = A.x - C.x;
double d1 = a1*(A.x) + c1*(A.y);
// Line BD represented as a2x + c2y = d2
double a2 = D.y - B.y;
double c2 = B.x - D.x;
double d2 = a2*(B.x)+ c2*(B.y);
double determinant = a1*c2 - a2*c1;
Res.x = (c2*d1 - c1*d2)/determinant;
Res.y = (a1*d2 - a2*d1)/determinant;
return Res;
}
void IlluminatedSpaceInRoom::FindWhereTheFloorIsIlluminated(double floorArea, IlluminatedSpace &space)
{
double height = space.A.z;
initializeTheFloor(floorArea, height);
if(fabs(space.A.x) < fabs(space.C.x))
{
swapPoints(space.A, space.C);
swapPoints(space.B, space.D);
}
bool isSwapedSquare = false;
if(space.A.y > space.C.y)
{
swapPoints(M, Q);
swapPoints(N, P);
isSwapedSquare = true;
}
if((!isSwapedSquare && (space.C.y < M.y || space.A.y > Q.y)) ||
(isSwapedSquare && (space.C.y > M.y || space.A.y < Q.y)) ) //0 points - space is not in the square
{
std::cout<<"The floor is not illuminated!\n";
return;
}
else if(IsPointInInterval(0, fabs(M.x), fabs(space.C.x)) &&
IsPointInInterval(0, fabs(M.x), fabs(space.A.x)) ) // 4 points
{
if(!(IsPointInInterval(0, fabs(M.y), fabs(space.C.y))) ) // VIII & VII graphics
{
space.C = findPointBetweenTwoLines(space.A, space.C, Q, P);
space.D = findPointBetweenTwoLines(space.B, space.D, Q, P);
}
if(!(IsPointInInterval(0, fabs(M.y), fabs(space.A.y))) ) // VI & VII graphics
{
space.A = findPointBetweenTwoLines(space.A, space.C, M, N);
space.B = findPointBetweenTwoLines(space.B, space.D, M, N);
}
space.printSpace();
return;
}
else if(!(IsPointInInterval(0, fabs(M.x), fabs(space.C.x))) &&
!(IsPointInInterval(0, fabs(M.x), fabs(space.A.x))) ) //II & IV & V graphics
{
space.A.x = std::max(space.A.x, M.x);
if(space.A.y < 0) space.A.y = std::max(space.A.y, M.y);
else space.A.y = std::min(space.A.y, M.y);
space.C.x = std::max(space.C.x, Q.x);
if(space.C.y > 0) space.C.y = std::min(space.C.y, Q.y);
else space.C.y = std::max(space.C.y, Q.y);
//because the figure is isosceles trapezoid
space.B.x = -space.A.x;
space.B.y = space.A.y;
space.D.x = -space.C.x;
space.D.y = space.C.y;
space.printSpace();
return;
}
else if(IsPointInInterval(0, fabs(M.x), fabs(space.C.x)) &&
!(IsPointInInterval(0, fabs(M.x), fabs(space.A.x))) ) // 6 points
{
if(! IsPointInInterval(0, fabs(M.y), fabs(space.C.y)) ) //X & XI graphics
{
space.C = findPointBetweenTwoLines(space.A, space.C, Q, P);
space.D = findPointBetweenTwoLines(space.B, space.D, Q, P);
}
point leftIntersectionSquareAndTrapezoid = findPointBetweenTwoLines(space.A, space.C, M, Q);
point rightIntersectionSquareAndTrapezoid = findPointBetweenTwoLines(space.B, space.D, N, P);
if(!(IsPointInInterval(0, fabs(M.y), fabs(space.A.y))) ) //IX & XII graphics
{
space.A = findPointBetweenTwoLines(space.A, space.C, M, N);
space.B = findPointBetweenTwoLines(space.B, space.D, M, N);
}
space.A.printPoint();
space.B.printPoint();
leftIntersectionSquareAndTrapezoid.printPoint();
rightIntersectionSquareAndTrapezoid.printPoint();
space.C.printPoint();
space.D.printPoint();
return;
}
}