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

From Progzoo
Jump to: navigation, search
(Cross Product)
(Vector Length)
Line 133: Line 133:
 
   cout << length(A)<< endl;
 
   cout << length(A)<< endl;
 
   cout << length(B)<< endl;
 
   cout << length(B)<< endl;
 +
  return 0;
 +
}
 +
]]></prog>
 +
 +
<answer><![CDATA[
 +
#include <math.h>
 +
#include <iostream>
 +
using namespace std;
 +
 +
float length(float a[3]){
 +
  float r=sqrt(a[0]*a[0]+a[1]*a[1]+a[2]*a[2]);
 +
  return r;
 +
}
 +
 +
int main(int argc, char **argv)
 +
{
 +
  float A[]={1,0,1};
 +
  float B[]={1,1,2};
 +
  cout << length(A)<< endl;
 +
  cout << length(B)<< endl;
 +
  return 0;
 +
}
 +
]]></answer>
 +
</question>
 +
 +
==Normal to a plane==
 +
<question>
 +
Find the unit normal to the plane that contains the three points given.
 +
<prog><![CDATA[
 +
#include <math.h>
 +
#include <iostream>
 +
using namespace std;
 +
 +
float length(float a[3]){
 +
  float r=sqrt(a[0]*a[0]+a[1]*a[1]+a[2]*a[2]);
 +
  return r;
 +
}
 +
 +
int main(int argc, char **argv)
 +
{
 +
  float A[]={1,0,1};
 +
  float B[]={1,1,2};
 +
  float C[]={0,1,3};
 
   return 0;
 
   return 0;
 
}
 
}

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