-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4. Distance Comparison.cpp
73 lines (55 loc) · 1.22 KB
/
4. Distance Comparison.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
#include <iostream>
#include <cmath>
//without the use of the squareroot function to find the length
class Vector {
public:
float length() const;
float lengthsqr() const;
float x, y, z;
};
class Point {
public:
Point AddVector(Vector v);
float x, y, z;
};
float Vector::length() const {
return sqrt(x * x + y * y + z * z);
}
float Vector::lengthsqr() const {
return (x * x + y * y + z * z);
}
Vector operator-(Point a, Point b) {
Vector v;
v.x = a.x - b.x;
v.y = a.y - b.y;
return v;
}
Point Point::AddVector(Vector v) {
Point p2;
p2.x = x + v.x;
p2.y = y + v.y;
p2.z = z + v.z;
return p2;
}
int main (int argc, char** args) {
Point p; // main point form we are calculating the distance
p.x = 0;
p.y = -1;
p.z = 0;
Point i; //position of i
i.x = 1;
i.y = 1;
i.z = 0;
Point c; //position of c
c.x = 2;
c.y = -1;
c.z = 0;
Vector cp;
Vector ip;
cp =p - c;
ip = p - i;
float length_sqr_cp = cp.lengthsqr();
float length_sqr_ip = ip.lengthsqr();
std :: cout <<"length squared of cp= "<< length_sqr_cp << std :: endl;
std :: cout <<"length squared of ip= "<< length_sqr_ip << std :: endl;
}