-
Notifications
You must be signed in to change notification settings - Fork 2
/
vect3.cpp
executable file
·125 lines (107 loc) · 2.39 KB
/
vect3.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
119
120
121
122
123
124
125
/*
Copyright (C) 2010 Matthew Baranowski, Sander van Rossen & Raven software.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "vect3.h"
const Vect3 Vect3X(1.f,0.f,0.f);
const Vect3 Vect3Y(0.f,1.f,0.f);
const Vect3 Vect3Z(0.f,0.f,1.f);
const Vect3 Vect3negX(-1.f,0.f,0.f);
const Vect3 Vect3negY(0.f,-1.f,0.f);
const Vect3 Vect3negZ(0.f,0.f,-1.f);
const Vect3 Vect3Zero(0.f,0.f,0.f);
const Vect3 &Vect3::operator/= (const float d)
{
float inv=1.f/d;
return (*this)*=inv;
}
void Vect3::Cross(const Vect3& p)
{
Vect3 t=*this;
v[0]=t.v[1]*p.v[2]-t.v[2]*p.v[1];
v[1]=t.v[2]*p.v[0]-t.v[0]*p.v[2];
v[2]=t.v[0]*p.v[1]-t.v[1]*p.v[0];
}
void Vect3::NegCross(const Vect3& p)
{
Vect3 t=*this;
v[0]=p.v[1]*t.v[2]-p.v[2]*t.v[1];
v[1]=p.v[2]*t.v[0]-p.v[0]*t.v[2];
v[2]=p.v[0]*t.v[1]-p.v[1]*t.v[0];
}
float Vect3::Dist(const Vect3& p) const
{
Vect3 t=*this;
t-=p;
return t.Len();
}
float Vect3::Dist2(const Vect3& p) const
{
Vect3 t=*this;
t-=p;
return t^t;
}
void Vect3::Perp()
{
float rlen,tlen;
Vect3 r,t;
r=*this;
r.Cross(Vect3X);
rlen=r.Len();
t=*this;
t.Cross(Vect3Y);
tlen=t.Len();
if (tlen>rlen)
{
r=t;
rlen=tlen;
}
t=*this;
t.Cross(Vect3Z);
tlen=t.Len();
if (tlen>rlen)
{
r=t;
rlen=tlen;
}
*this=r;
}
void Vect3::Min(const Vect3& p)
{
if (p.v[0]<v[0])
v[0]=p.v[0];
if (p.v[1]<v[1])
v[1]=p.v[1];
if (p.v[2]<v[2])
v[2]=p.v[2];
}
void Vect3::Max(const Vect3& p)
{
if (p.v[0]>v[0])
v[0]=p.v[0];
if (p.v[1]>v[1])
v[1]=p.v[1];
if (p.v[2]>v[2])
v[2]=p.v[2];
}
float Vect3::MaxElement() const
{
return v[MaxElementIndex()];
}
int Vect3::MaxElementIndex() const
{
if (fabs(v[0])>fabs(v[1])&&fabs(v[0])>fabs(v[2]))
return 0;
if (fabs(v[1])>fabs(v[2]))
return 1;
return 2;
}