Contents
title: Fundamentals of Digital Methods: Common Functions date: 2019-10-29 21:12:14 description: Fundamentals of Digital Methods: Common Functions. 1. Cross Product 2. Vector Normalization 3. Matrix Multiplication 4. Matrix Multiplication. categories:
- Programming
- OpenGL tags:
- Notes
- Digital Methods
1. Cross Product
void crossproject(float vec1[3],float vec2[3],float n[3])
{
n[0] = vec1[1]*vec2[2]-vec1[2]*vec2[1];
n[1] = vec1[2]*vec2[0]-vec1[0]*vec2[2];
n[2] = vec1[0]*vec2[1]-vec1[1]*vec2[0];
}
2. Vector Normalization
void Normalize(float n[3])
{
float length;
length = sqrt(n[0]*n[0]+n[1]*n[1]+n[2]*n[2]);
for(int i = 0;i < 3;i++)
n[i] /= length;
}
3. Matrix Multiplication 4×4 × 4×1
void ApplyMatrix(float *P0,float *translation,float *P1)
{
for(int i = 0;i < 3;i++)
P1[i] = P0[0]*translation[i]+P0[1]*translation[i+4]+P0[2]*translation[i+8]+translation[i+12];
}
4. Matrix Multiplication 4×4 × 4×4
void mul(float *rotation,float *translation,float *tran)
{
for(int i = 0;i < 4;i++)
for(int j = 0;j < 4;j++)
for(int k = 0;k < 4;k++)
tran[4*i+j] += rotation[4*k+j]*translation[4*i+k];
}
Comments