-
Notifications
You must be signed in to change notification settings - Fork 2
/
Distance.cpp
60 lines (49 loc) · 1.57 KB
/
Distance.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
//
// Created by Ryan.Zurrin001 on 12/16/2021.
//
#include "Distance.h"
float rez::distance(Point3d& A, Point3d& B, Point3d& C)
{
Vector3f AB = B - A; //direction vector of line
Vector3f CA = A - C;
Vector3f cross = crossProduct3d(CA, AB);
float mag_cross = cross.magnitude();
float mag_dir = AB.magnitude();
float distance = mag_cross / mag_dir;
return distance;
}
float rez::distance(Line& line, Point3d& C)
{
// TODO we can simple call distance frunction which takes three points here as well.
// But then the methodology of finding the distance is not visible to students. So keep this.
// Ignor the fact that we duplicate the similar implementation, which is not the best practise.
Vector3f AC = C - line.point();
auto t = dotProduct(line.direction(), AC);
auto xt = line.point() + line.direction() * t;
auto dist_vec = xt - C;
return dist_vec.magnitude();
}
float rez::distance(Line2d& line, Point2d& C)
{
return 0.0;
}
float rez::distance(Point3d& p1, Point3d& p2)
{
float dx = p1[X_] - p2[X_];
float dy = p1[Y_] - p2[Y_];
float dz = p1[Z_] - p2[Z_];
float distance = sqrt(pow(dx, 2) + pow(dy, 2) + pow(dz, 2));
return distance;
}
float rez::distance(Point2d& p1, Point2d& p2)
{
float dx = p1[X_] - p2[X_];
float dy = p1[Y_] - p2[Y_];
float distance = sqrt(pow(dx, 2) + pow(dy, 2));
return distance;
}
float rez::distance(Planef& p, Point3d& Q)
{
auto result = dotProduct(p.getNormal(), Q) - p.getD();
return result;
}