Class3_MathReview2
Class3_MathReview2
Mankyu Sung
Computer Graphics I
Dot product
a b = a x bx + a y b y + a z bz = a i bi
a b = a b cos
Mankyu Sung
Computer Graphics I
Example
What is the value of dot product of v ·u?
v = (7,3,-2)
u = (10,4,2)
v ·u ?
Mankyu Sung
Computer Graphics I
glm
glm::vec3 v(7,3,-2);
glm::vec3 u(10,4,2);
float d = glm::dot(v,u)
Mankyu Sung 4
Computer Graphics I
b θ
a
Mankyu Sung
Computer Graphics I
a b = a b cos
a b
cos =
a b
b θ
a b
= cos −1 a
a b
Mankyu Sung
Computer Graphics I
Quiz
a = (2,-1,1) b=(1,1,2)
Angle between vector a and vector b?
Mankyu Sung
Computer Graphics I
glm
#include <glm/gtc/constants.hpp>
glm::vec3 v(2,-1,1);
glm::vec3 u(1,1,2);
https://padlet.com/mksung89/dotProductEpitech
Mankyu Sung 8
Computer Graphics I
Quiz
a = (1,0,0) b=(0,1,0)
Angle between vector a and vector b?
Mankyu Sung
Computer Graphics I
Mankyu Sung
Computer Graphics I
glm
#include <glm/gtc/constants.hpp>
v = glm::normalize(v);
u = glm::normalize(u);
Mankyu Sung 11
Computer Graphics I
Even though vector a and b are not unit vectors, following are true.
Mankyu Sung
Computer Graphics I
glm
#include <glm/gtc/constants.hpp>
Mankyu Sung 13
Computer Graphics I
Another use of dot product
: Vector Projection (only one vector is Unit Vector)
u
a·u
Mankyu Sung
Computer Graphics I
Vector decomposition
We can decompose a vector into two vectors
I would like to decompose a vector v into two vectors a and b, but b must be
parallel to an unit vector c
a 𝑏 = 𝑐 ∗ (𝑣 ∙ 𝑐)
v v 𝑎 =𝑣−𝑏
b
c
Mankyu Sung 15
Computer Graphics I
•x
n
p•
Mankyu Sung http://mathinsight.org/distance_point_plane
Computer Graphics I
•x
dist = (x − p) n n x-p
p•
Mankyu Sung
Computer Graphics I
glm
glm::vec3 v = x – p;
n = glm::normalize(n);
float d = glm::dot(v, n);
Mankyu Sung 18
Computer Graphics I
b
a
Cross product returns a vector orthogonal to two initial vectors
(perpendicular to both vectors)
Direction determined by right-hand rule
Useful in constructing coordinate systems (later)
Mankyu Sung
Computer Graphics I
u u
uxv
Which one is the cross product of two vector u and v?
Right hand rule -> https://youtu.be/zGyfiOqiR4s
Mankyu Sung 20
21
Computer Graphics I
x y = +z
y x = −z
a ba=−bb=−ab a
y z = +x
a aa= 0a = 0
z y = −x
a (ab + (cb) += ca)=ba+ab+ca c
z x = +y
a (akb)(=kbk)(=
a k b(a) b)
x z = −y
Cross Product
a b = a y b z − a z b y a z bx − a x bz a x b y − a y bx
Cross product only works in 3D space(dot product works in 2D
and 3D space as well)
Mankyu Sung
Computer Graphics I
Mankyu Sung
Computer Graphics I
Example
U = (1,-2,-1) V = (-2,4,1)
UxV=?
Mankyu Sung
Computer Graphics I
glm
Mankyu Sung 26
Computer Graphics I
Length of
vector a b = a b sin
The area of parallelogram that is
a b = consisted of a와 b
a b = 0 If, a and b are parallel
a b is perpendicular to both a and b, in the
direction defined by the right hand rule
Mankyu Sung
Computer Graphics I
float l = glm::length(c);
float ang = glm::acos(glm::dot(u, v) / (glm::length(u) * glm::length(v)));
Mankyu Sung 28
Computer Graphics I
b
a
Mankyu Sung
Computer Graphics I
1
area = (b − a ) (c − a )
2
c
c-a
b
a b-a
Mankyu Sung
Computer Graphics I
glm
glm::vec3 v1 = p2-p1;
glm::vec3 v2 = p3-p1;
Mankyu Sung 31
Computer Graphics I
Mankyu Sung 32
Computer Graphics I
p1
ccw(counterclock-wise)
p2 p3
p3,p1,p2
Specifying the order of three p2,p3,p1
vertices is important p1,p2,p3…
Mankyu Sung 33
Computer Graphics I
p1
v1 v2
p2 p3
p1->p2->p3 v1=p2-p1
v2=p3-p1 N = v1 X v2
CCW
Mankyu Sung 34
Computer Graphics I
p1(3, 0, 0)
p2->p3->p1
Or p3->p1->p2
p2
Mankyu Sung https://padlet.com/mksung89/EpitehNormal 35
Computer Graphics I
Matrix
Mankyu Sung 36
Computer Graphics I
Matrices
Mankyu Sung
Computer Graphics I
Matrices
Mankyu Sung
Computer Graphics I
Matrix Transposes
Mankyu Sung
Computer Graphics I
glm
#include <glm/mat4x4.hpp>
glm::vec4 v1 = { 1, 0, 1, -1 };
glm::vec4 v2 = { 1, 0, 1, -1 };
glm::vec4 v3 = { 1, 0, 1, -1 };
glm::vec4 v4 = { 1, 0, 1, -1 };
glm::mat4 m;
m[0] = v1; 1 1 1 1
m[1] = v2; 0 0 0 0
m[2] = v3; 𝐴=
1 1 1 1
m[3] = v4;
−1 −1 −1 −1
We can initialize the matrix from vectors
(Note that the vectors set as columns)
Mankyu Sung 40
Computer Graphics I
glm
#include <glm/mat4x4.hpp>
glm::mat4 m = {{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}};
1st column 2nd column 3rd column 4th column
1 0 0 0
0 1 0 0
m=
1 0 1 0
0 0 0 1
Mankyu Sung 41
Computer Graphics I
glm
#include <glm/mat4x4.hpp>
#include <glm/mat2x2.hpp>
#include <glm/mat3x2.hpp>
glm::mat2 m2 = { 1, 0, 1, -1 };
std::cout << to_string(m2) << std::endl;
glm::mat3x2 m3 = { { 1, 0 }, { 1, -1 }, { 0, 1 } };
std::cout << to_string(m3) << std::endl;
Mankyu Sung 42
Computer Graphics I
Matrix Addition
Mankyu Sung
Computer Graphics I
glm::mat2 m1 = { 1, 0, 1, -1 };
glm::mat2 m2 = { 0, 1, -1, 0 };
glm::mat2 m3 = m1 + m2;
Mankyu Sung 44
Computer Graphics I
Matrix Scaling
Mankyu Sung
Computer Graphics I
Properties of Matrix Addition and Scaling
Addition is Commutative
Addition is Associative
Scaling is Associative
Mankyu Sung
Computer Graphics I
glm
#include <glm/mat4x4.hpp>
#include <glm/mat2x2.hpp>
#include <glm/mat3x2.hpp>
glm::mat2 m1 = { 1, 0, 1, -1 };
m1 = m1 * 2.0f;
std::cout << to_string(m3) << std::endl;
Mankyu Sung 47
Computer Graphics I
Matrix Multiplication
Only well defined if the number of columns of the first matrix and the number
of rows of the second matrix are the same
Matrix * Matrix = Matrix
i.e. if F is m x n, and
G is n x p, then FG
if m x p
Let’s do an example
Mankyu Sung
Example
Computer Graphics I
glm
#include <glm/mat4x4.hpp>
#include <glm/mat2x2.hpp>
#include <glm/mat3x2.hpp>
glm::mat2 m1 = { 1, 0, 1, -1 };
glm::mat2 m2 = { 0, 1, 1, -1 };
glm::mat2 m3 = m1 * m2;
std::cout << to_string(m3) << std::endl;
Mankyu Sung 50
Computer Graphics I
𝑀𝑉 ≠ 𝑉𝑀
Mankyu Sung 51
Computer Graphics I
An example
1 0 2 1 0 0
𝐴= 1 2 3 𝐵 = 0 −1 1
−1 0 1 0 1 2
𝐴×𝐵 =?
𝐵×𝐴 =?
https://padlet.com/mksung89/matrixMul
Mankyu Sung 52
Computer Graphics I
Defined such that the product of any matrix M and the identity matrix
I is M
IM = MI = M
Let’s derive it
The identity matrix is a square matrix with ones on the diagonal and
zeros elsewhere
Mankyu Sung
Computer Graphics I
Mankyu Sung
Computer Graphics I
Mankyu Sung 55
Computer Graphics I
T
1 2
1 3 5
3 4 = 2 4 6
5 6
( AB)T = BT AT
Mankyu Sung
Computer Graphics I
glm
#include <glm/mat4x4.hpp>
#include <glm/mat2x2.hpp>
#include <glm/mat3x2.hpp>
glm::mat3x2 m3 = { { 1, 0 }, { 1, -1 }, { 0, 1 } };
glm::mat2x3 m4 = glm::transpose(m3);
Mankyu Sung 57
Computer Graphics I
1 0 0
I 33 = 0 1 0
0 0 1
−1 −1
AA = A A = I
( AB) −1 = B −1 A−1
Mankyu Sung
Computer Graphics I
1 d − b
ad − cb − c a
Mankyu Sung 59
Computer Graphics I
Example
4 8
A = 𝐴 = 𝐴−1 =? 𝜋𝑟 2
1 3
Mankyu Sung 60
Computer Graphics I
glm
#include <glm/mat4x4.hpp>
#include <glm/mat2x2.hpp>
#include <glm/mat3x2.hpp>
glm::mat2 m2 = { 1, 0, 1, -1 };
glm::mat2 im = glm::inverse(m2);
std::cout << to_string(im) << std::endl;
glm::mat2 ii = m2 * im;
std::cout << to_string(ii) << std::endl;
Mankyu Sung 61
Computer Graphics I
𝑣′ = M 𝑣
𝑣 : 4D vector
:M : 4x4 matrix
Mankyu Sung 62
Computer Graphics I
An example
1 0 2 1
𝐴= 1 2 3 𝑣= 1
−1 0 1 −1
𝐴×𝑣 =?
𝑣×𝐴 =?
Mankyu Sung 63
Computer Graphics I
glm pointer
#include <glm/gtc/type_ptr.hpp>
If you want to get an address of glm::vec or glm::mat, you get use like this
glm::vec4 v(0.0f);
glm::mat4 m(1.0f);
...
glVertex3fv(glm::value_ptr(v))
glLoadMatrixfv(glm::value_ptr(m));
Mankyu Sung 64
Computer Graphics I
important functions
Linear Interpolation
glm::mix(U, V, t) : Linearly interpolate U and V
Lineal interpolate U and V using weight value t ( t goes between (0,1) )
If t == 0, it return U
If t == 1, it returns V
If t == 0.5, it blend U and V in half and half.
U, V can be a vector or scalar.
Clamping
glm::clamp(value, min_value, max_value) : Keep the value in (min_value, max_value)
If value < min_value, then set it back to min_value
If value > max_value, then set it back to max_value
Mankyu Sung 65