-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTriFan.cpp
107 lines (88 loc) · 2.44 KB
/
TriFan.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
/**
* Instructor's solution for A5 in ECE 3400, Fall 2020.
* @author Andrew Vardy
*/
#include "TriFan.h"
namespace A6 {
TriFan::TriFan(const PolarVec vertexArray[], int nVertices)
: m_pVertices{ new PolarVec[nVertices] }
, m_nVertices{ nVertices }
{
if (m_nVertices < 2)
throw TriFanConstructionException{};
// Copying from input array
for (int i=0; i<nVertices; i++)
m_pVertices[i] = vertexArray[i];
}
TriFan::TriFan(const TriFan& other)
: m_pVertices{ new PolarVec[other.m_nVertices] }
, m_nVertices{ other.m_nVertices }
{
for (int i=0; i<m_nVertices; i++)
m_pVertices[i] = other.m_pVertices[i];
}
TriFan& TriFan::operator=(const TriFan& other)
{
if (this != &other) {
delete[] m_pVertices;
m_pVertices = new PolarVec[other.m_nVertices];
m_nVertices = other.m_nVertices;
for (int i=0; i<m_nVertices; i++)
m_pVertices[i] = other.m_pVertices[i];
}
return *this;
}
TriFan::~TriFan()
{
delete[] m_pVertices;
}
void TriFan::split1(TriFan*& pieceOne, TriFan*& pieceTwo) const
{
split2(this, pieceOne, pieceTwo);
}
void split2(const TriFan *input, TriFan*& pieceOne, TriFan*& pieceTwo)
{
if (input->m_nVertices <= 2) {
pieceOne = nullptr;
pieceTwo = nullptr;
throw TriFanSplitException{};
}
int n = input->m_nVertices;
int n_2 = n / 2; // Note that this is integer division.
int nFirst = n_2 + 1;
int nSecond = n - n_2;
PolarVec* firstHalf = new PolarVec[nFirst];
PolarVec* secondHalf = new PolarVec[nSecond];
for (int i=0; i <= n_2; i++)
firstHalf[i] = input->m_pVertices[i];
for (int i=n_2; i < n; i++)
secondHalf[i - n_2] = input->m_pVertices[i];
pieceOne = new TriFan(firstHalf, nFirst);
pieceTwo = new TriFan(secondHalf, nSecond);
delete[] firstHalf;
delete[] secondHalf;
}
ostream& operator<<(ostream& os, TriFan t) {
os << "nVertices: " << t.m_nVertices;
os << ", [";
for (int i=0; i<t.m_nVertices; i++) {
os << t.m_pVertices[i];
if (i <t.m_nVertices - 1) os << ", ";
}
os << "]";
return os;
}
bool operator==(const TriFan &a, const TriFan &b)
{
if (a.m_nVertices != b.m_nVertices)
return false;
for (int i=0; i<a.m_nVertices; i++)
if (a.m_pVertices[i] != b.m_pVertices[i])
return false;
return true;
}
bool operator!=(const TriFan &a, const TriFan &b)
{
return !(a == b);
}
};