-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrid.cpp
85 lines (61 loc) · 1.5 KB
/
Grid.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
#define _CRT_SECURE_NO_WARNINGS
#include "Grid.h"
#include <iostream>
using namespace std;
//C'tor
Grid::Grid(double tileW, double tileH, int width, int height, int color)
{
//Calculating the amount of rectangles in the array
_numOfRecs = (width * height);
_RectArr = new Rectangle * [_numOfRecs];
//Loops that form the grid of rectangles
int k = 0;
for (int i = 0 ; i < width ; i++ )
{
for (int j = 0; j < height ; j++)
{
_RectArr[k] = new Rectangle(tileW*i, tileH*j ,tileW,tileH,color);
k++;
}
}
}
//D'tor - Release dynamic allocations
Grid::~Grid() {
for (int i = 0; i < _numOfRecs ; i++)
{
delete this->_RectArr[i];
}
delete[] _RectArr;
}
//Checks which rectangle the point is contained in
Rectangle* Grid::getRectAtPoint(const Point& p)
{
for (int i = 0; i < _numOfRecs ; i++)
{
if (_RectArr[i]->contains(p) == true)
{
return _RectArr[i];
}
}
}
//Returns the rectangle in the position of the index
Rectangle* Grid::getRectAtIndex(int i)
{
return _RectArr[i];
}
//Moves all the rectangles in space
void Grid::moveGrid(double deltaLeft, double deltaTop)
{
for (int i = 0; i < _numOfRecs ; i++)
{
_RectArr[i]->moveRect(deltaLeft, deltaTop);
}
}
//Shrinks and expands the entire grid of rectangles
void Grid::scaleGrid(double rectWidth, double rectHeight)
{
for (int i = 0; i < _numOfRecs ; i++)
{
_RectArr[i]->scaleRect(rectWidth, rectHeight);
}
}