Math portfolio

advertisement
2015
Math portfolio
Create a game
Sampo Kataja
1
Table of contents
Project 1 – Lines................................................................................................................................................. 4
1.1 .................................................................................................................................................................. 4
Basic theory ............................................................................................................................................... 4
MyCode...................................................................................................................................................... 4
Testing ....................................................................................................................................................... 5
1.2 .................................................................................................................................................................. 6
Basic Theory............................................................................................................................................... 6
MyCode...................................................................................................................................................... 6
Testing ....................................................................................................................................................... 7
Project 2 – Collision Detection .......................................................................................................................... 8
Basic theory ................................................................................................................................................... 8
MyCode ......................................................................................................................................................... 8
Testing ........................................................................................................................................................... 8
Project 3 – Trigonometry................................................................................................................................... 9
Basic theory ................................................................................................................................................... 9
MyCode ....................................................................................................................................................... 10
Testing ......................................................................................................................................................... 10
Project 4 – Vectors .......................................................................................................................................... 11
4.1 ................................................................................................................................................................ 11
Adding ...................................................................................................................................................... 11
Subtracting .............................................................................................................................................. 12
Scalar multiplication ................................................................................................................................ 12
Length of a vector .................................................................................................................................... 12
Normalization .......................................................................................................................................... 13
4.2 ................................................................................................................................................................ 13
Dot product ............................................................................................................................................. 13
Cross product........................................................................................................................................... 14
Testing ......................................................................................................................................................... 14
Project 5 – Matrices......................................................................................................................................... 15
Adding.......................................................................................................................................................... 16
Basic theory ............................................................................................................................................. 16
MyCode.................................................................................................................................................... 16
Testing ..................................................................................................................................................... 16
2
Subtracting .................................................................................................................................................. 17
Basic theory ............................................................................................................................................. 17
MyCode.................................................................................................................................................... 17
Testing ..................................................................................................................................................... 17
Scalar multiplication .................................................................................................................................... 17
Basic theory ............................................................................................................................................. 17
MyCode.................................................................................................................................................... 17
Testing ..................................................................................................................................................... 18
Matrix multiplication ................................................................................................................................... 18
Basic theory ............................................................................................................................................. 18
MyCode.................................................................................................................................................... 19
Testing ..................................................................................................................................................... 19
Transpose .................................................................................................................................................... 19
Basic theory ............................................................................................................................................. 19
MyCode.................................................................................................................................................... 19
Testing ..................................................................................................................................................... 20
Project 6 – Transformaties .............................................................................................................................. 20
6.1 – Translation .......................................................................................................................................... 20
Basic theory ............................................................................................................................................. 20
MyCode.................................................................................................................................................... 20
Testing ..................................................................................................................................................... 21
6.2 – Translation continued ......................................................................................................................... 22
Basic theory ............................................................................................................................................. 22
Testing ..................................................................................................................................................... 22
6.3a – Scaling ............................................................................................................................................... 23
Basic theory ............................................................................................................................................. 23
Testing ..................................................................................................................................................... 23
6.3b – Rotation ............................................................................................................................................ 24
Basic theory ............................................................................................................................................. 24
Testing ..................................................................................................................................................... 25
6.4a – Scaling ............................................................................................................................................... 26
Basic theory ............................................................................................................................................. 26
MyCode.................................................................................................................................................... 27
Testing ..................................................................................................................................................... 27
3
6.4b – Rotation ............................................................................................................................................ 28
Basic theory ............................................................................................................................................. 28
Testing ..................................................................................................................................................... 28
4
Project 1 – Lines
1.1
Basic theory
” Write a program that, based on an entered start point and end point, the equation of the line determines
between those two points.”
When determining the equation of a line, the first thing one must do is form the delta values. After that
one can calculate the slope with the delta values. When one has the slope value, it’s possible to determine
the equation of the line.
a
y y 2  y1

x x 2  x1
MyCode
function float CalculateDelta(float second, float first)
{
return second-first;
}
function float CalculateSlope(float deltaY, float deltaX)
{
return deltaY/deltaX;
}
The formula for the equation of the line:
y = ax + b
function string CalculateAnswer(float slope,float x,float y)
{
local float calc1,calc2;
local string answer,calc2AsString,withPlus;
x = -1*(x);
calc1 = slope*x;
calc2 = calc1 + (y);
withPlus = "+"$calc2;
calc2AsString = (calc2 > -1) ? withPlus : string(calc2);
answer = "y ="$slope$"x"$calc2AsString;
return answer;
}
5
Testing
Part 1:
Part 2:
6
1.2
Basic Theory
“Write a program that determines of 2 given lines (linear equations) will intersect. If they do intersect,
return the point of intersection.”
One can determine this multiple ways, but when calculating this with pen and paper the easiest
way is to calculate the X value out of the two equations of line:
y1 = a1x1 + b1
y2 = a2x2 + b2

a1x1 + b1 = a2x2 + b2

They’ll intersect if a1 != a2
...and place the X value into one of the first two equations of lines to get the Y value.
When one has to do this by programming, there are a bit more steps to take. I followed the steps found
here:
linkToAlgorithm
MyCode
x12
x34
y12
y34
=
=
=
=
x1
x3
y1
y3
-
x2;
x4;
y2;
y4;
c = x12 * y34 - y12 * x34;
if (Abs(c) < 0.01)
{
// No intersection
`Log("no intersection");
}
else
{
// Intersection
a = x1 * y2 - y1 * x2;
b = x3 * y4 - y3 * x4;
x = (a * x34 - b * x12) / c;
y = (a * y34 - b * y12) / c;
`Log("x: "$x$", y: "$y);
}
7
Testing
Part 1:
Part 1:
8
Project 2 – Collision Detection
Basic theory
“Write a program that determines if 2 given spheres (the center and radius are given) collide or not.”
Theory behind this is pretty straight forward. When the center and radius are given one just has to place
the values into this formula:
(a2  a1)2  (b2  b1)2  (r1  r2 )2
MyCode
function string CheckIfCollide(float x1,float x2,float y1,float y2,float z1, float z2, float
r1,float r2)
{
local float comp1, comp2;
local string answer;
comp1 = Square(x2 - x1) + Square(y2 - y1) + Square(z2 - z1);
comp2 = Square(r1 + r2);
answer = (comp1 <= comp2) ? "intersects" : "nope";
return answer;
}
Testing
Part 1:
9
Part 2:
Project 3 – Trigonometry
Basic theory
“Write a program that on the basis of 2 data (sides/corners) of a rectangular triangle the other data
(sides/corners) of this rectangular triangle calculates. Write the program in such manner that all possible
combinations of input will be elaborated”
When one has been given one corner of a rectangular it’s very easy to calculate the third one, because the
one is always 90 degrees.
180 – 90 – givenCorner = lastCorner
When one has a corner and a side one can calculate the rest sides using these formulas:
sin  
cos  
tan  
y
h
x
h
y sin 

x cos 
h
y

x
10
MyCode
Because all possible combinations of inputs must be elaborated, the main function has to take everything in
as parameters and has to return also everything as out parameters.
function calcStuff(float angle1, float angle2, float hypo, float X, float Y, out float
sideX, out float sideY, out float sideHYPO, out float alpha, out float gamma)
{
Couple of examples using different inputs:
//Values that are set: angle1 and hypo
if(angle2 == 0 && X == 0 && Y == 0)
{
sinCosTanAngle = Sin(angle1);
sideY = hypo * sinCosTanAngle;
sideX = Square(hypo) - Square(sideY);
sideX = Sqrt(sideX);
alpha = 180 - 90 - angle1;
gamma = 180 - 90 - alpha;
sideHYPO = hypo;
}
//Values that are set: X and Y sides
else if(angle1 == 0 && angle2 == 0 && hypo == 0)
{
hypo = Square(X) + Square(Y);
hypo = Sqrt(hypo);
alpha = Tan(X/Y);
alpha = Atan(alpha);
alpha = alpha / (PI/180);
gamma = 180 - 90 - alpha;
sideHYPO = hypo;
sideX = X;
sideY = Y;
}
}
Testing
Part 1: // calcStuff(40, 0, 15, 0, 0, sideX, sideY, sideHYPO, alpha, gamma);
11
Part 2: calcStuff(0, 0, 0, 12, 15, sideX, sideY, sideHYPO, alpha, gamma);
Project 4 – Vectors
4.1
” Create your own library of vector math functions including: addition/subtraction, scalar multiplication,
length of a vector and normalization of a vector.”
Adding
Basic theory
Adding vectors are simple as adding numbers. If one has two vectors and wants to add them:

(x1+x2, y1+y2)
In UnrealScript one can do it by adding already declared and initialized vectors together:

vect a + vect b
MyCode
function vector Addition(vector one, vector two)
{
return one + two;
}
12
Subtracting
Basic theory
Substracting vectors comes as easy as adding did. Same logic, just substract!

(x1-x2, y1-y2)

vect a – vect b
MyCode
function vector Subtraction(vector one, vector two)
{
return one - two;
}
Scalar multiplication
Basic theory
When multiplying a vector with scalar one just has to multiply every X,Y,Z value with the scalar. One can’t
multiply scalar with vector.

scalar*(X,Y,Z)

(sclalarX,scalarY,scalarZ)

in Unreal: scalar * vect a
MyCode
function vector ScalarMultiplication(float scal, vector V)
{
return scal * V;
}
Length of a vector
Basic theory
When one wants to get the length of a vector, one should use this formula:
V  Vx2  Vy2  Vz2
This formula is itself a variation of the trusty Pythagorean
MyCode
function float LengthOfV(vector V)
{
return Sqrt(Square(V.X)+Square(V.Y)+Square(V.Z));
}
13
Normalization
Basic theory
The normalized vector of V is a vector in the same direction but with norm (length) 1.
ˆ  V
V
V
MyCode
function vector Normalization(vector V)
{
local float vectorLength;
vectorLength = LengthOfV(V);
return V / vectorLength;
}
4.2
Dot product
Basic theory
Good place to learn it: dotProductExplained
MyCode
function float DotProduct(vector one, vector two)
{
return (one.X*two.X)+(one.Y*two.Y)+(one.Z*two.Z);
}
14
Cross product
Basic theory
Good place to learn it: crossProductExplained
 Vx 
 
 Vy 
V 
 z
x
 Wx   Vy Wz  Vz Wy 


 
 Wy    Vz Wx  Vx Wz 
 W   V W  V W 
y
x
 z  x y
MyCode
function vector CrossProduct(vector
{
three.X = (one.Y*two.Z)
three.Y = (one.Z*two.X)
three.Z = (one.X*two.Y)
return three;
}
Testing
Part 1:
one, vector two, vector three)
- (one.Z*two.Y);
- (one.X*two.Z);
- (one.Y*two.X);
15
Part 2:
Project 5 – Matrices
“Create your own library of matrix math functions including: addition/subtraction, scalar multiplication,
matrix multiplication and transpose.”
In Unreal one can either use planes to fill a matrix or as I did; 2d arrays.
struct Row
{
var Array<float> cells;
};
var Array<Row> rows;
When Array rows has values in it, I used this function to create the actual matrix:
static function MyMatrix createMatrix(Array<Row> rows)
{
local MyMatrix result;
result = new class'MyMatrix';
result.rows = rows;
return result;
}
And as one can see, I’m doing all the calculation stuff in another class called MyMatrix.
There can be out of bound warnings, so I used this to handle them:
if(result.rows.Length <= i){
result.rows.Length = i + 1;
}
It is important to know these lines of code in the sections later.
16
Adding
Basic theory
Adding matrices is basically the same thing than adding vectors, one just has to have equal size matrices.
MyCode
for(i=0;i<rows.Length; i++){
if(result.rows.Length <= i){
result.rows.Length = i + 1;
}
for(j=0;j<rows[i].cells.Length;j++){
result.rows[i].cells[j] = other.rows[i].cells[j] + rows[i].cells[j];
}
}
Testing
17
Subtracting
Basic theory
Same logic as adding matrices, just subtracting!
MyCode
result.rows[i].cells[j] = other.rows[i].cells[j] - rows[i].cells[j];
Testing
Scalar multiplication
Basic theory
The logic is the same with vectors, one needs a scalar that will be multiplied with every cell of the matrix.
MyCode
result.rows[i].cells[j] = scalar * rows[i].cells[j];
18
Testing
Matrix multiplication
Basic theory
Two matrices can be multiplied if and only if number of cells First = number of rows Second.
I found this topic helpful to understand the algorithm when programming matrix multiplications:
matrixMultiplicationCodeHelper
19
MyCode
for(k=0; k<other.rows[i].cells.Length; k++){
result.rows[i].cells[j] =
result.rows[i].cells[j] + other.rows[i].cells[k] * rows[k].cells[j];
}
Testing
Link to verify it’s correct: checkIfCorrect
Transpose
Basic theory
The transpose matrix MT of matrix M is given by interchanging the rows and columns of M
MyCode
Programming vice the out of bound checking must be done after the second loop because result rows must
be the same amount than the cells length before transpose.
if(result.rows[i].cells.Length <= j){
result.rows.Length = j + 1;
}
result.rows[j].cells[i] = other.rows[i].cells[j];
20
Testing
Project 6 – Transformaties
6.1 – Translation
“Write a program that translates a given vector. Log the starting vector, the translation used and the
result.”
Basic theory
Translation of an object means moving the object around (up, down, left, right). One can do this by adding
or multiplying matrices. When using matrix multiplication one has to multiply the object with the desired
coordinates.
 x' 1 0 dx  x 
  
  
 y'  0 1 dy   y 
 1  0 0 1  1
MyCode
After filling the matrices:

resultFirst = vectorMatrix.Multiplication(translator);
Check the matrix multiplication for more specific code examples of multiplying matrices.
21
Testing
Part 1:
Part 2:
22
6.2 – Translation continued
” Write a program that translates a given 2D triangle. You’ve to translate each of the 3 vertices of the
triangle. Log the 3 vertices of the triangle you start with, the translation used and the result.”
Basic theory
The same logic applies here with this one as did with the earlier one. Now one just has to multiply three
vectors (matrices) instead of one. The same translator applies for each vertices.
Testing
23
6.3a – Scaling
" Write a program that scales a given 2D triangle with respect to the origin. It has to be possible to use
different scaling values for both directions. Log the 3 vertices of the triangle you start with, the scaling
values used and the result.”
Basic theory
It’s very possible to scale an object. The scaling also happens by using matrix multiplication. When scaling
with respect to the origin, it basically means that the scaling point isn’t in the object itself (origin = (0, 0)).
So when scaling by more than 1 moves the object away from the origin and scaling by less than 1 moves the
object towards the origin (goodExample).
One has to multiply every value in the object with the scaling factor. To do that, one must create two
matrices:
 x' S x
  
 y'   0
 1   0
Testing
Part 1:
0
Sy
0
0  x 
  
0   y 
1  1
24
Part2:
6.3b – Rotation
“Write a program that rotates a given 2D triangle with respect to the origin. Log the 3 vertices of the
triangle you start with, the rotation used and the result.”
Basic theory
When rotating with respect to the origin, one doesn’t have to move the object anywhere. So basically the
object moves around a point, not itself (goodExample). When rotating in the XY-plane one rotates around
the z-axis.
 cos 

Mz   sin 
 0

 sin  0 

cos  0 
0
1 
In UnrealScript one should remember to change the values to degrees, so:
25
Cos(90*PI/180)
or Cos(90*RadianToDegree)
Testing
Part1:
Using 90 degrees as rotation value
26
Part2:
Using 25 degrees as rotation value
6.4a – Scaling
Write a program that scales a given 2D triangle with respect to a given vertex. It has to be possible to use
different scaling values for both directions. Log the 3 vertices of the triangle you start with, the scaling
values used and the vertex used and at last the result.
Basic theory
When scaling with respect to a given vertex, it basically means that the scaling point is somewhere in the
object itself. That means the fixed point is at the origin, so one has to translate the object first. One must be
careful, the order matters!
1 0 75   0.5

 
0
1
93

  0.866
0 0 1   0
 0.866 0 1 0  75 
 

0.5
0  0 1  93 
0
1 0 0
1 
27
MyCode
Code example of scaling one vertice:
//create the vertice to be translated
vectorToTrans1 = class'MyMatrix'.static.createMatrix(vertice1);
//first transtlation
translatedV1 = vectorToTrans1.Multiplication(translator);
//scale the translated vector matrix
comb1 = translatedV1.Multiplication(scalorMatrix);
//create the translator to go back to origin
stepBack = class'MyMatrix'.static.createMatrix(goback);
//go back translation
returnVertex1 = comb1.Multiplication(stepBack);
Testing
28
6.4b – Rotation
Write a program that rotates a given 2D triangle with respect of a given vertex. Log the 3 vertices of the
triangle, the rotation and the vertex and at last the result.
Basic theory
When rotating with respect of a given vertex, the basic logic is the same as it was with scaling. One just has
to rotate, not scale.
Testing
Using 90 degrees as rotation value
29
Download