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

From Progzoo
Jump to: navigation, search
 
(15 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
{{3D Exercise}}
 +
==Dot Product==
 +
<question>
 +
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
 +
<prog><![CDATA[
 +
#include <math.h>
 +
#include <iostream>
 +
using namespace std;
 +
 +
float dotproduct(float a[3], float b[3]){
 +
  float r = 0; // Your code goes here
 +
  return r;
 +
}
 +
 +
int main(int argc, char **argv)
 +
{
 +
  float A[]={1,0,1};
 +
  float B[]={1,1,2};
 +
  float d = dotproduct(A,B);
 +
  cout << d<< endl;
 +
  return 0;
 +
}
 +
]]></prog>
 +
 +
<answer><![CDATA[
 +
#include <math.h>
 +
#include <iostream>
 +
using namespace std;
 +
 +
float dotproduct(float a[3], float b[3]){
 +
  float r = a[0]*b[0]+a[1]*b[1]+a[2]*b[2];
 +
  return r;
 +
}
 +
 +
int main(int argc, char **argv)
 +
{
 +
  float A[]={1,0,1};
 +
  float B[]={1,1,2};
 +
  float d = dotproduct(A,B);
 +
  cout << d<< endl;
 +
  return 0;
 +
}
 +
]]></answer>
 +
</question>
 +
==Cross Product==
 
<question>
 
<question>
 
Calculate the cross product of two vectors.
 
Calculate the cross product of two vectors.
 
Fill in the details in the function '''crossproduct'''
 
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
 
<prog><![CDATA[
 
<prog><![CDATA[
 +
#include <math.h>
 +
#include <iostream>
 +
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;
 +
}
 +
 +
void crossproduct(float r[3],float a[3],float b[3]){
 +
  // Your code goes here, change the result - r
 +
}
 +
 +
int main(int argc, char **argv)
 +
{
 +
  float A[]={1,0,1};
 +
  float B[]={1,1,2};
 +
  float C[3];
 +
  crossproduct(C,A,B);
 +
  cout << C[0]<<" "<<C[1]<<" "<<C[2]<< endl;
 +
  return 0;
 +
}
 +
]]></prog>
 +
 +
<answer><![CDATA[
 
#include <math.h>
 
#include <math.h>
 
#include <iostream>
 
#include <iostream>
Line 16: Line 95:
 
float *crossproduct(float a[3], float b[3]){
 
float *crossproduct(float a[3], float b[3]){
 
   float *r = new float[3];
 
   float *r = new float[3];
   // Your code goes here
+
   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 26: Line 107:
 
   float *C = crossproduct(A,B);
 
   float *C = crossproduct(A,B);
 
   cout << C[0]<<" "<<C[1]<<" "<<C[2]<< endl;
 
   cout << C[0]<<" "<<C[1]<<" "<<C[2]<< endl;
 +
  return 0;
 +
}
 +
]]></answer>
 +
</question>
 +
 +
==Vector Length==
 +
<question>
 +
Calculate the magnitude of a vector.
 +
Fill in the details in the function '''length'''
 +
 +
<prog><![CDATA[
 +
#include <math.h>
 +
#include <iostream>
 +
using namespace std;
 +
 +
float length(float a[3]){
 +
  float r=0; // Your code goes here
 +
  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;
 +
}
 +
]]></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>
 +
 +
==Normalise a vector==
 +
<question>
 +
Normalise the given vector. The direction should be the same, the length should be 1.
 +
 +
<prog><![CDATA[
 +
#include <math.h>
 +
#include <iostream>
 +
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)
 +
{
 +
  float A[]={1,0,1};
 +
  float B[]={1,1,2};
 +
  float C[3];
 +
  normalise(C,A);
 +
  cout << C[0]<<" "<<C[1]<<" "<<C[2]<< endl;
 +
  normalise(C,B);
 +
  cout << C[0]<<" "<<C[1]<<" "<<C[2]<< 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;
 +
}
 +
 +
void scalarproduct(float r[3],float x,float a[3]){
 +
  for (int i=0;i<3;i++)
 +
    r[i] = x*a[i];
 +
}
 +
 +
int main(int argc, char **argv)
 +
{
 +
  float A[]={1,0,1};
 +
  float B[]={1,1,2};
 +
  float C[3];
 +
  scalarproduct(C,1.0/length(A),A);
 +
  cout << C[0]<<" "<<C[1]<<" "<<C[2]<< endl;
 +
  scalarproduct(C,1.0/length(B),B);
 +
  cout << C[0]<<" "<<C[1]<<" "<<C[2]<< endl;
 +
  return 0;
 +
}
 +
]]></answer>
 +
</question>
 +
 +
==Normal to a plane==
 +
<question>
 +
Find the unit normal to the plane that contains the three points given.
 +
<hint>
 +
You will probably want the functions:
 +
float *subtract(float a[3], float b[3]){
 +
  float *r = new float[3];
 +
  for (int i=0;i&lt;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&lt;3;i++)
 +
    r[i] = x*a[i];
 +
  return r;
 +
}
 +
 +
float length(float a[3]){
 +
  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;
 +
}
 +
 +
</hint>
 +
<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;
 
}
 
}
 
]]></prog>
 
]]></prog>
 +
 +
<answer><![CDATA[
 +
#include <math.h>
 +
#include <iostream>
 +
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 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;
 +
}
 +
 +
int main(int argc, char **argv)
 +
{
 +
  float A[]={1,0,1};
 +
  float B[]={1,1,2};
 +
  float C[]={0,1,3};
 +
  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;
 +
}
 +
]]></answer>
 
</question>
 
</question>
 +
{{3D Exercise}}

Latest revision as of 12:12, 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]