forked from r-tron18/3d-Helix-Visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaxis.cpp
More file actions
163 lines (134 loc) · 3.49 KB
/
axis.cpp
File metadata and controls
163 lines (134 loc) · 3.49 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
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
#include<iostream>
#include<cmath>
#define M_ERROR (0.00001)
using namespace std;
class Point
{
double x;double y;double z;
public:
Point(): x(0),y(0),z(0) {}
Point(double a,double b) : x(a),y(b),z(0) {}
Point(double a,double b,double c) : x(a),y(b),z(c) {}
double _x() {return x;}
double _y() {return y;}
double _z() {return z;}
void display()
{
//cout.precision(5);
cout.setf(ios::fixed,ios::floatfield);
cout<<"The point is ("<<x<<", "<<y<<", "<<z<<")\n";
}
double distanceFrom(Point &other)
{
return sqrt((x - other.x) * (x - other.x) + (y - other.y) * (y - other.y) + (z - other.z) * (z - other.z)) ;
}
double magnitude()
{
return sqrt(x*x + y*y + z*z);
}
double mod()
{
return (x*x + y*y + z*z);
}
Point operator -() // unary minus
{
return Point(-x,-y,-z);
}
Point operator -(Point &other)
{
return Point(this->x - other.x,this->y - other.y, this->z - other.z);
}
Point operator +(Point &other)
{
return Point(this->x + other.x,this->y + other.y, this->z + other.z);
}
bool operator == (Point & other)
{
if ( abs(x-other.x) < M_ERROR && abs(y-other.y) < M_ERROR && abs(z-other.z) < M_ERROR )
{return true; }
else
{return false;}
}
double operator*(Point &other) // scalar product
{
return this->x * other.x + this->y * other.y + this->z * other.z;
}
friend Point operator*(Point &other,double l);
friend Point operator*(double l,Point &other);
};
Point operator *(Point &other,double l)
{
return Point(other.x * l, other.y * l, other.z * l);
}
Point operator*(double l,Point &other)
{
return Point(l*other.x, l * other.y, l * other.z);
}
// -------------- Axis -------------
class Axis
{
Point p;
Point v;
public:
Axis()
{
p = Point(0,0,0);
v = Point(0,0,0);
}
Axis(Point &P,Point &V)
{
p=P; v=V;
}
Point _getP() {return p;}
Point _getV() {return v;}
Point per_point_on_axis(Point b)
{
double lambda = ((b-p)*v)/(v.mod());
Point temp = lambda * v + p;
return temp;
}
Point leg_of(Point B,Point A) // here B is a point in space while A is the point in plane
{
double d = A * v;
double lambda = (d - (B*v)) / (v*v);
return lambda * v + B;
}
void display()
{
cout<<"\t The AXIS is \n";
p.display();
cout<<"Vector is ("<<v._x()<<", "<<v._y()<<", "<<v._z()<<")\n";
}
};
int main()
{
cout<<"\t\t Testing Axis \n\n";
cout<<"Creating Axis without values \n";
Axis ax;
ax.display();
cout<<endl;
cout<<"Creating Axis with Point(1,0,0) and vec(0,0,1) \n";
Point o1(1,0,0),vec1(0,0,1);
Axis ax1(o1,vec1);
ax1.display();
cout<<endl;
cout<<"Finding Point X on previous axis using Point(1,1,4) such that ...\n";
cout<<"\t a line passing through Point X and Point(1,1,4) is perpendicular to axis vector\n";
Point p(1,1,4);
Point perp_p = ax1.per_point_on_axis(p);
perp_p.display();
cout<<endl;
cout<<"Creating Axis with Point(1,1,2) and vec(1,3,1) \n";
Point o2(1,1,2),vec2(1,3,1);
Axis ax2(o2, vec2);
cout<<"Creating Point (2,2,2) \n";
Point st(2,2,2);
cout<<" The Plane in consideration is the one with Point (2,2,2) lying on plane and plane_vector (1,3,1)\n";
cout<<" We create anothe point (10,20,30) \n";
Point st2(10,20,30);
cout<<" The perpendicular point x of (10,20,30) is such that distance between x and plane is minimum \n";
Point perp_st2 = ax2.leg_of(st2,st);
perp_st2.display();
cout<<endl;
return 0;
}