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

From Progzoo
Jump to: navigation, search
(Vector Length)
(Normal to a plane)
Line 184: Line 184:
 
#include <iostream>
 
#include <iostream>
 
using namespace std;
 
using namespace std;
 +
 +
float *subtract(float a[3], float b[3]){
 +
  float *r = new float[3];
 +
  for (int i=0;i<3;i++)
 +
    r[i] = a[i]-b[i];
 +
  return r;
 +
}
 +
 +
float *scalarproduct(float x,float a[3]){
 +
  float *r = new float[3];
 +
  for (int i=0;i<3;i++)
 +
    r[i] = x*a[i];
 +
  return r;
 +
}
  
 
float length(float a[3]){
 
float length(float a[3]){
 
   float r=sqrt(a[0]*a[0]+a[1]*a[1]+a[2]*a[2]);
 
   float r=sqrt(a[0]*a[0]+a[1]*a[1]+a[2]*a[2]);
 +
  return r;
 +
}
 +
 +
float *crossproduct(float a[3], float b[3]){
 +
  float *r = new float[3];
 +
  r[0] = a[1]*b[2]-a[2]*b[1];
 +
  r[1] = a[2]*b[0]-a[0]*b[2];
 +
  r[2] = a[0]*b[1]-a[1]*b[0];
 
   return r;
 
   return r;
 
}
 
}
Line 194: Line 216:
 
   float A[]={1,0,1};
 
   float A[]={1,0,1};
 
   float B[]={1,1,2};
 
   float B[]={1,1,2};
   cout << length(A)<< endl;
+
   float C[]={0,1,3};
   cout << length(B)<< endl;
+
  float *N = crossproduct(subtract(B,A),subtract(C,A));
 +
  N = scalarproduct(1.0/length(N),N);
 +
   cout << N[0]<<" "<<N[1]<<" "<<N[2]<< endl;
 
   return 0;
 
   return 0;
 
}
 
}
 
]]></answer>
 
]]></answer>
 
</question>
 
</question>

Revision as of 14:12, 23 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]

Normal to a plane

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



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