-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShapesContainer.cpp
175 lines (165 loc) · 4.03 KB
/
ShapesContainer.cpp
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "ShapesContainer.h"
void ShapesContainer::Free()
{
for (int i = 0; i < count; i++)
delete shapes[i];
delete[] shapes;
}
void ShapesContainer::CopyFrom(const ShapesContainer& other)
{
shapes = new Shapes * [other.count];
capacity = other.capacity;
count = other.count;
for (int i = 0; i < other.count; i++)
shapes[i] = other.shapes[i]->clone();
}
void ShapesContainer::Resize(int NewCappacity)
{
Shapes** NewArray = new Shapes * [NewCappacity];
for (int i = 0; i < count; i++)
NewArray[i] = shapes[i];
delete[] shapes;
shapes = NewArray;
capacity = NewCappacity;
}
int ShapesContainer::GetCount()const
{
return count;
}
ShapesContainer::ShapesContainer()
{
capacity = 8;
count = 0;
shapes = new Shapes * [capacity];
}
ShapesContainer::ShapesContainer(const ShapesContainer& other)
{
CopyFrom(other);
}
ShapesContainer& ShapesContainer::operator=(const ShapesContainer& other)
{
if (this != &other)
{
Free();
CopyFrom(other);
}
return *this;
}
ShapesContainer::~ShapesContainer()
{
Free();
}
Shapes* ShapesContainer::AtIndex(int index)
{
if (index - 1 > count)
cout << "There is no figure at index" << index;
else
return shapes[index - 1];
}
void ShapesContainer::AddShape(const char* shape, double startX, double startY, const char* color, double endX, double endY)
{
if (count == this->capacity)
Resize(capacity * 2);
Shapes* newObj;
if (strcmp(shape, "line") == 0)
{
newObj = new Line(startX, startY, endX, endY, color, count + 1);
}
else if (strcmp(shape, "circle") == 0)
{
newObj = new Circle(startX, startY, endX, color, count + 1); // endX here plays the role of radius
}
else if (strcmp(shape, "rectangle") == 0)
{
newObj = new Rectangle(startX, startY, endX, endY, color, count + 1); // endX and endY play the role of width and height exactly in that order
}
else
{
throw "Invalid shape";
}
shapes[this->count++] = newObj;
}
void ShapesContainer::PrintAllInStrm(ostream& strm)
{
for (int i = 0; i < count; i++)
shapes[i]->Print(strm);
}
void ShapesContainer::PrintAll()cosnt
{
for (int i = 0; i < count; i++)
shapes[i]->Print();
}
void ShapesContainer::Erase(int index)
{
//cout << count << index << endl;
int i = 0;
for (i; i < count; i++)
{
if (index == shapes[i]->GetID())
{
cout << "Erased figure number " << index << "!" << endl;
Shapes** NewArray = new Shapes * [capacity];
int i, j;
for (i = 0; i < index - 1; i++)
NewArray[i] = shapes[i];
for (j = index; j < count; j++)
{
NewArray[i] = shapes[j];
i++;
}
delete[] shapes;
shapes = NewArray;
count--;
break;
}
}
if (i == count)
{
cout << "There is no figure number " << index << "!" << endl;
}
}
void ShapesContainer::WithinCircle(double startX, double startY, double radius)
{
int counter = 0;
for (int i = 0; i < count + 1; i++)
{
shapes[i]->WithinCircle(startX, startY, radius);
if (shapes[i]->WithinCircle(startX, startY, radius))
{
counter++;
}
}
if (counter == 0)
{
cout << "No figures are located within circle " << startX << " " << startY << " " << radius;
}
}
void ShapesContainer::WithinRectangle(double startX, double startY, double width, double height)
{
int counter = 0;
for (int i = 0; i < count + 1; i++)
{
shapes[i]->WithinRectangle(startX, startY, width, height);
if (shapes[i]->WithinRectangle(startX, startY, width, height))
{
counter++;
}
}
if (counter == 0)
{
cout << "No figures are located within circle " << startX << " " << startY << " " << width << " " << height;
}
}
void ShapesContainer::TranslateShape(double vertical, double horizontal)
{
for (int i = 0; i < count; i++)
{
shapes[i]->Translate(vertical, horizontal);
}
cout << "All shape translated.";
}
void ShapesContainer::TranslateShape(double vertical, double horizontal, int n)
{
shapes[n]->Translate(vertical, horizontal);
}
///////////////////////////////////////////////////////////////////////////////