dotboxEngine/points.cxx
2024-11-20 02:44:18 +00:00

104 lines
2.0 KiB
C++

#include "points.h"
#include <iostream>
Points::Points() {
size = 0;
capacity = 1;
array = new point[capacity];
}
Points::Points(int rows, int cols): size(0), capacity(4) {
array = new point[capacity];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if((i%2==0 && j%2!=0) || (i%2!=0 && j%2==0)) {
point temp;
temp.x = i;
temp.y = j;
push(temp);
}
}
}
}
Points::~Points() {
delete[] array;
}
int Points::getSize() {
return size;
}
int Points::getCapacity() {
return capacity;
}
void Points::push(point val) {
if (size == capacity) {
growArray();
}
array[size] = val;
size++;
}
void Points::removeVal(point val) {
for (int i = 0; i < size; i++) {
if (array[i].x==val.x && array[i].y==val.y) {
if (i==size-1) {
array[size-1].x = -1;
array[size-1].y = -1;
size--;
} else {
array[i] = array[i+1];
}
}
}
if (size == capacity/2) {
shrinkArray();
}
}
void Points::removeIndex(int index) {
for (int i = index; i < size; i++) {
if (i==size-1) {
array[size-1].x = -1;
array[size-1].x = -1;
} else {
array[i] = array[i+1];
}
}
size--;
if (size == capacity/2) {
shrinkArray();
}
}
void Points::growArray() {
capacity = capacity * 2;
point* temp = new point[capacity];
for (int i = 0; i < size; i++) {
temp[i] = array[i];
}
delete[] array;
array = temp;
}
void Points::shrinkArray() {
capacity = capacity / 2;
point* temp = new point[capacity];
for (int i = 0; i < size; i++) {
temp[i] = array[i];
}
delete[] array;
array = temp;
}
void Points::printArray() {
for (int i=0;i<size;i++) {
std::cout << array[i].x << " " << array[i].y << std::endl;
}
}