/
IntPlane.cpp
118 lines (105 loc) · 3.17 KB
/
IntPlane.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
/**
* The IntPlane class represents an XY plane of pixels, and the integer value each of these pixels holds.
*
* @author Sasha Ouellet - spaouellet@me.com - www.sashaouellet.com
* @version 1.0 - 02/25/17
*/
#include <algorithm>
#include <stdexcept>
#include <iostream>
#include "IntPlane.h"
using namespace std;
/**
* Construct with the given dimensions of the plane. Will initialize the array of the pixel data values according to
* this size and will keep the pointer to this array. The IntPlane is responsible for deleting the memory at this pointer
*
* @param width The width of the plane
* @param height The height of the plane
*/
IntPlane::IntPlane(int width, int height)
{
m_width = width;
m_height = height;
m_pixelData = new int[width * height];
}
/**
* Copy constructor for the IntPlane
*
* @param plane The reference to the IntPlane this new IntPlane will be copied from
*/
IntPlane::IntPlane(const IntPlane& plane)
{
m_width = plane.m_width;
m_height = plane.m_height;
m_pixelData = new int[m_width * m_height];
copy(plane.m_pixelData, plane.m_pixelData + (plane.m_width * plane.m_height), m_pixelData);
}
IntPlane::~IntPlane()
{
delete [] m_pixelData;
}
/**
* Gets the index in the int array that the associated xy-coords lie at
*
* @param x The x value of the point
* @param y The y value of the point
* @return The index of the specified point in the array
*/
int IntPlane::getIndexFromPoint(int x, int y)
{
return (y * m_width + x);
}
/**
* Gets the integer value stored at the given pixel position in the XY plane
*
* @param x The x value of the pixel in the plane
* @param y The y value of the pixel in the plane
* @return The integer stored at this position
* @throws invalid_argument if the given x or y values exceeds the width or height of the plane (or less than 0)
*/
int IntPlane::getPixelValueAt(int x, int y)
{
if (x >= m_width || y >= m_height || x < 0 || y < 0)
{
throw invalid_argument("Received x or y value that exceeds width or height of plane (or they are less than 0)");
}
return m_pixelData[getIndexFromPoint(x, y)];
}
/**
* Sets the integer value of the pixel at the specified xy-coords
*
* @param x The x value of the pixel in the plane
* @param y The y value of the pixel in the plane
* @param value The value to set the pixel information to
* @throws invalid_argument if the given x or y values exceeds the width or height of the plane (or less than 0)
*/
void IntPlane::setPixelValueAt(int x, int y, int value)
{
if (x >= m_width || y >= m_height || x < 0 || y < 0)
{
throw invalid_argument("Received x or y value that exceeds width or height of plane (or they are less than 0)");
}
m_pixelData[getIndexFromPoint(x, y)] = value;
}
/**
* Fills the entire plane with the specified value
* @param value The integer value to assign to every spot in the plane
*/
void IntPlane::fill(int value)
{
std::fill(m_pixelData, m_pixelData + (m_width * m_height), value);
}
/**
* For debug purposes
*/
void IntPlane::print()
{
for (int i = 0 ; i < m_height ; i++)
{
for (int j = 0 ; j < m_width ; j++)
{
cout << getPixelValueAt(j, i) << " ";
}
cout << endl;
}
}