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 164 165 166 167 168 169 170 171 172
|
// Definition of structure "v3d" AKA a vector
struct v3d
{
double X, Y, Z, mag;
};
// Function to calculate dot product
double dotproduct(v3d a, v3d b)
{
double val=0;
val = a.X*b.X + a.Y*b.Y + a.Z*b.Z;
return val;
}
// Function for directional vector to create cross product
void d(v3d A, v3d B, v3d C, v3d &D, v3d &E)
{
D.X = B.X - A.X;
D.Y = B.Y - A.Y;
D.Z = B.Z - A.Z;
E.X = C.X - A.X;
E.Y = C.Y - A.Y;
E.Z = C.Z - A.Z;
}
// Function to calcuate cross product
void crossproduct(v3d A, v3d B, v3d &C)
{
C.X = A.Y * B.Z - A.Z * B.Y;
C.Y = A.Z * B.X - A.X * B.Z;
C.Z = A.X * B.Y - A.Y * B.X;
}
// To calculate the magnitude of a vector
void magnitude(v3d &a)
{
a.mag = sqrt(dotproduct(a,a));
}
// To create a normalised vector given two other vectors
void normalise (v3d &v, v3d a, v3d b)
{
v3d temp;
temp.X = b.X - a.X;
temp.Y = b.Y - a.Y;
temp.Z = b.Z - a.Z;
magnitude(temp);
v.X = temp.X/temp.mag;
v.Y = temp.Y/temp.mag;
v.Z = temp.Z/temp.mag;
magnitude(v);
}
void pprint(v3d A)
{
cout<<A.X<<"\t"<<A.Y<<"\t"<<A.Z<<"\t"<<A.mag<<"\n";
}
int main()
{
double P1x, P1y, P1z, P2x, P2y, P2z, P3x, P3y, P3z, P4x, P4y, P4z;
P1x = 2;
P1y = 2;
P1z = 2;
P2x = 7;
P2y = 2;
P2z = 2;
P3x = 3;
P3y = 5;
P3z = 6;
P4x = 8;
P4y = 5;
P4z = 6;
v3d P1, P2, P3, P4;
P1.X = P1x;
P1.Y = P1y;
P1.Z = P1z;
magnitude(P1);
cout<<"P1\n";
pprint(P1);
P2.X = P2x;
P2.Y = P2y;
P2.Z = P2z;
magnitude(P2);
cout<<"P2\n";
pprint(P2);
P3.X = P3x;
P3.Y = P3y;
P3.Z = P3z;
magnitude(P3);
cout<<"P3\n";
pprint(P3);
P4.X = P4x;
P4.Y = P4y;
P4.Z = P4z;
magnitude(P4);
cout<<"P4\n";
pprint(P4);
// assign values to u and v array
double u[3] = {0.0, 0.5, 1.0 }, v[3] = {0.0, 0.5, 1.0 };
v3d temp1, temp2, temp3, uhat;
v3d temp4, temp5, temp6, vhat;
v3d temp7, temp8, temp9, what;
// 3d vectors to output 9 vectors
v3d S[9];
for(int i=0; i<3; ++i)
for(int j=0; j<3; ++j)
{
S[3*i+j].X = ( P1.X*(1-u[i] ) + P2.X*u[i] )*( 1 - v[j] ) + ( P3.X*(1-u[i] ) + P4.X*u[i] )* v[j];
S[3*i+j].Y = ( P1.Y*(1-u[i] ) + P2.Y*u[i] )*( 1 - v[j] ) + ( P3.Y*(1-u[i] ) + P4.Y*u[i] )* v[j];
S[3*i+j].Z = ( P1.Z*(1-u[i] ) + P2.Z*u[i] )*( 1 - v[j] ) + ( P3.Z*(1-u[i] ) + P4.Z*u[i] )* v[j];
magnitude(S[3*i+j]);
pprint(S[3*i+j]);
}
d(S[0], S[3], S[1], temp1,temp2);
crossproduct (temp1,temp2, temp3);
magnitude(temp3);
uhat.X = temp3.X / temp3.mag;
uhat.Y = temp3.Y / temp3.mag;
uhat.Z = temp3.Z / temp3.mag;
magnitude(uhat);
cout<<"uhat\n";
pprint(uhat);
d(S[3], S[4], S[1], temp4,temp5);
crossproduct (temp4,temp5, temp6);
magnitude(temp6);
vhat.X = temp6.X / temp6.mag;
vhat.Y = temp6.Y / temp6.mag;
vhat.Z = temp6.Z / temp6.mag;
magnitude(vhat);
cout<<"vhat\n";
pprint(vhat);
d(S[3], S[6], S[4], temp7,temp8);
crossproduct (temp7,temp8, temp9);
magnitude(temp9);
what.X = temp9.X / temp6.mag;
what.Y = temp9.Y / temp6.mag;
what.Z = temp9.Z / temp6.mag;
magnitude(what);
cout<<"what\n";
pprint(what);
|