-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathline.cpp
More file actions
56 lines (48 loc) · 1.95 KB
/
line.cpp
File metadata and controls
56 lines (48 loc) · 1.95 KB
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
#include "line.hpp"
namespace cpp_utils {
Line::Line(Vector _point, Vector _direction) : point(_point) {
Vector normDir = _direction.normalise();
if ((normDir.x < 0) || (normDir.x == 0 && normDir.y < 0) || (normDir.x == 0 && normDir.y == 0 && normDir.z < 0)) {
direction = -normDir;
} else {
direction = normDir;
}
};
bool Line::operator==(const Line &that) const {
bool on = this->isOnLine(that.point);
if (direction == that.direction && on) {
return true;
} else {
return false;
}
};
bool Line::isOnLine(const Vector &that) const {
bool isOn = true;
try {
this->whereOnLine(that);
} catch (value_error) {
isOn = false;
}
return isOn;
}
double Line::whereOnLine(const Vector &that) const {
Vector vecDiff = that - point;
double scalarX, scalarY, scalarZ;
if (direction.x == 0) {scalarX = vecDiff.x;} else {scalarX = vecDiff.x / direction.x;}
if (direction.y == 0) {scalarY = vecDiff.z;} else {scalarY = vecDiff.y / direction.y;}
if (direction.z == 0) {scalarZ = vecDiff.z;} else {scalarZ = vecDiff.z / direction.z;}
if ((scalarX == scalarY && scalarX == scalarZ) ||
(scalarY == 0 && scalarX == scalarZ && direction.y == 0) ||
(scalarZ == 0 && scalarX == scalarY && direction.z == 0) ||
(scalarY == 0 && scalarZ == 0 && direction.y == 0 && direction.z == 0)) {
return scalarX;
} else if ((scalarX == 0 && scalarY == scalarZ && direction.x == 0) ||
(scalarX == 0 && scalarZ == 0 && direction.x == 0 && direction.z == 0)) {
return scalarY;
} else if ((scalarX == 0 && scalarY == 0 && direction.x == 0 && direction.y == 0)) {
return scalarZ;
} else {
throw value_error("Point not on line.");
}
};
}