Difference between revisions of "C++:3D Exercises"

From Progzoo
Jump to: navigation, search
(Cross Product)
(Normalise a vector)
Line 165: Line 165:
 
#include <iostream>
 
#include <iostream>
 
using namespace std;
 
using namespace std;
 +
 +
void normalise(float r[3],float a[3]){
 +
  //Your code here. set r to normalise a
 +
}
  
 
int main(int argc, char **argv)
 
int main(int argc, char **argv)
Line 170: Line 174:
 
   float A[]={1,0,1};
 
   float A[]={1,0,1};
 
   float B[]={1,1,2};
 
   float B[]={1,1,2};
   float *C;
+
   float C[3];
 +
  normalise(C,A);
 
   cout << C[0]<<" "<<C[1]<<" "<<C[2]<< endl;
 
   cout << C[0]<<" "<<C[1]<<" "<<C[2]<< endl;
 +
  normalise(C,B);
 
   cout << C[0]<<" "<<C[1]<<" "<<C[2]<< endl;
 
   cout << C[0]<<" "<<C[1]<<" "<<C[2]<< endl;
 
   return 0;
 
   return 0;
Line 187: Line 193:
 
}
 
}
  
float *scalarproduct(float x,float a[3]){
+
void scalarproduct(float r[3],float x,float a[3]){
  float *r = new float[3];
 
 
   for (int i=0;i<3;i++)
 
   for (int i=0;i<3;i++)
 
     r[i] = x*a[i];
 
     r[i] = x*a[i];
  return r;
 
 
}
 
}
  
Line 198: Line 202:
 
   float A[]={1,0,1};
 
   float A[]={1,0,1};
 
   float B[]={1,1,2};
 
   float B[]={1,1,2};
   float *C = scalarproduct(1.0/length(A),A);
+
   float C[3];
 +
  scalarproduct(C,1.0/length(A),A);
 
   cout << C[0]<<" "<<C[1]<<" "<<C[2]<< endl;
 
   cout << C[0]<<" "<<C[1]<<" "<<C[2]<< endl;
   C = scalarproduct(1.0/length(B),B);
+
   scalarproduct(C,1.0/length(B),B);
 
   cout << C[0]<<" "<<C[1]<<" "<<C[2]<< endl;
 
   cout << C[0]<<" "<<C[1]<<" "<<C[2]<< endl;
 
   return 0;
 
   return 0;

Revision as of 12:10, 25 November 2011

Dot Product

Calculate the dot product of two vectors. Fill in the details in the function dotproduct

Wikipedia includes a good article on dot products. http://en.wikipedia.org/wiki/Dot_product#Definition



[Font] [Default] [Show] [Resize] [History] [Profile]

Cross Product

Calculate the cross product of two vectors. Fill in the details in the function crossproduct

Wikipedia includes a good article on cross products. http://en.wikipedia.org/wiki/Cross_product#Coordinate_notation



[Font] [Default] [Show] [Resize] [History] [Profile]

Vector Length

Calculate the magnitude of a vector. Fill in the details in the function length



[Font] [Default] [Show] [Resize] [History] [Profile]

Normalise a vector

Normalise the given vector. The direction should be the same, the length should be 1.



[Font] [Default] [Show] [Resize] [History] [Profile]

Normal to a plane

Find the unit normal to the plane that contains the three points given.



[Font] [Default] [Show] [Resize] [History] [Profile]