-
Notifications
You must be signed in to change notification settings - Fork 0
/
Triangle.h
164 lines (150 loc) · 4.65 KB
/
Triangle.h
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
//---------------------------------------------------------------------------
#ifndef TriangleH
#define TriangleH
#include "Object3D.h"
#include "Vertice.h"
#define _X 0
#define _Y 1
#define _Z 2
//---------------------------------------------------------------------------
class Matrix;
struct vector3D {
double x;
double y;
double z;
};
class Triangle : virtual public Object3D
{
private :
inline void normalized(TPoint, double &, double &);
inline void makevector3D(const vector3D, const vector3D, vector3D &);
inline void crossProduct(const vector3D, const vector3D, vector3D &);
inline void swap(double *, double *);
void sort(double [3][3], unsigned);
inline double dotProduct(const vector3D, const vector3D);
inline double vectorLength(const vector3D);
inline double angle(const vector3D, const vector3D);
inline void normalizeVec(vector3D &);
inline TRGBTriple RGBTripleFromColor(TColor);
void drawWithColor(Graphics::TBitmap *, float **, double[3][3], TRGBTriple);
public :
Triangle() { }
Triangle(const Triangle&);
~Triangle();
inline void applyTransform(Matrix *);
inline void applyRotation(const double, const double);
void draw(Graphics::TBitmap *) {}
void draw(Graphics::TBitmap *, float **, TColor, TColor);
Vertice *av;
Vertice *bv;
Vertice *cv;
inline double minZ();
};
//---------------------------------------------------------------------------
// Lifecycle
//---------------------------------------------------------------------------
Triangle::Triangle(const Triangle &src)
{
av = new Vertice(*src.av);
bv = new Vertice(*src.bv);
cv = new Vertice(*src.cv);
}
//---------------------------------------------------------------------------
Triangle::~Triangle()
{
delete av;
delete bv;
delete cv;
}
//---------------------------------------------------------------------------
// Custom methods
//---------------------------------------------------------------------------
inline double Triangle::minZ()
{
double z = av->getZ();
z = (bv->getZ() < z) ? bv->getZ() : z;
z = (cv->getZ() < z) ? cv->getZ() : z;
return z;
}
//---------------------------------------------------------------------------
inline void Triangle::normalized(TPoint center, double &x, double &y)
{
x = center.x + x * scale;
y = center.y - y * scale;
}
//---------------------------------------------------------------------------
inline void Triangle::makevector3D(const vector3D a, const vector3D b, vector3D &result)
{
result.x = b.x - a.x;
result.y = b.y - a.y;
result.z = b.z - a.z;
}
//---------------------------------------------------------------------------
inline void Triangle::crossProduct(const vector3D a, const vector3D b, vector3D &result)
{
result.x = a.y * b.z - a.z * b.y;
result.y = -a.x * b.z + a.z * b.x;
result.z = a.x * b.y - a.y * b.x;
}
//---------------------------------------------------------------------------
inline void Triangle::swap(double *a, double *b)
{
double t[3];
t[_X] = a[_X];
t[_Y] = a[_Y];
t[_Z] = a[_Z];
a[_X] = b[_X];
a[_Y] = b[_Y];
a[_Z] = b[_Z];
b[_X] = t[_X];
b[_Y] = t[_Y];
b[_Z] = t[_Z];
}
//---------------------------------------------------------------------------
inline double Triangle::dotProduct(const vector3D a, const vector3D b)
{
return a.x * b.x + a.y * b.y + a.z * b.z;
}
//---------------------------------------------------------------------------
inline double Triangle::vectorLength(const vector3D v)
{
return sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
}
//---------------------------------------------------------------------------
inline double Triangle::angle(const vector3D a, const vector3D b)
{
return acos(dotProduct(a, b) / (vectorLength(a) * vectorLength(b)));
}
//---------------------------------------------------------------------------
inline void Triangle::normalizeVec(vector3D &v)
{
double length = vectorLength(v);
length = (length) ? length : 0.00001;
v.x /= length;
v.y /= length;
v.z /= length;
}
//---------------------------------------------------------------------------
inline void Triangle::applyTransform(Matrix *transform)
{
av->applyTransform(transform);
bv->applyTransform(transform);
cv->applyTransform(transform);
}
//---------------------------------------------------------------------------
inline void Triangle::applyRotation(const double ax, const double ay)
{
av->applyRotation(ax, ay);
bv->applyRotation(ax, ay);
cv->applyRotation(ax, ay);
}
//---------------------------------------------------------------------------
inline TRGBTriple Triangle::RGBTripleFromColor(TColor color)
{
TRGBTriple rgbTriple;
rgbTriple.rgbtRed = GetRValue(color);
rgbTriple.rgbtGreen = GetGValue(color);
rgbTriple.rgbtBlue = GetBValue(color);
return rgbTriple;
}
#endif