Page 140 - DCAP313_LAB_ON_COMPUTER_GRAPHICS
P. 140
Lab on Computer Graphics
Notes In homogeneous coordinates:
s È x 0 0˘
Í
S(s x , s y ) = 0 s y 0 ˙ ˙
Í
Í Î 0 0 1˙ ˚
Homogeneous coordinates (HC) add an extra virtual dimension. Thus 2D HC are actually 3D
and 3D HC are 4D. Consider a 2D point p = (x,y). In HC, we represent p as p = (x, y, 1). An
extra coordinate is added whose value is always 1. This may seem odd but it allows us to now
represent translations as matrix multiplication instead of as vector addition. A translation (dx,
dy) which would normally be performed as q = (x,y) + (dx, dy) now is written as
È 10 dx˘ È x + dx˘
Í
˙
q = Tp = 01 dy = Í y + dy ˙
Í ˙ Í ˙
Í Î 00 1 ˙ ˚ Í Î 1 ˙ ˚
Now, we can write the scaling about a fixed point as simply a matrix multiplication:
q = (–T) S T p = A p,
Where A = (–T) S T
The matrix A can be calculated once and then applied to all the points in the object. This is much
more efficient than our previous representation. It is also easier to identify the transformations
and their order when everything is in the form of matrix multiplication.
The matrix for scaling in HC is
È sx 0 0˘
Í
S = 0 sy 0 ˙
Í ˙
Í Î 0 0 1 ˙ ˚
And the matrix for rotation is
a
a
È cos( ) sin() 0˘
Í
R = - sin( )cos() 0 ˙ ˙
a
a
Í
Í Î 0 0 1˙ ˚
So scaling with homogeneous:
È x¢ ˘ S È x 0 0˘ x È ˘ Ïx ¢= S x Ï Ô Ôw ¢ x = S x x
x
Ô
Í ˙ Í ˙ Í ˙ Æ Ì y ¢= S y Æ Ì ¢ w
y
Í y¢ ˙ = Í 0 S y 0 ˙ Í ˙ Ô y Ô ¢ y y
Í Î w¢ ˙ ˚ Í Î 0 0 1˙ Í w ˙ Ó w ¢= w Ó Ô w ¢ = S y w
˚ Î ˚
8.2.7 Transformation 2D (Scaling, Translation, Rotation)
#include "Stdio.h"
#include "conio.h"
#include "math.h"
#include "graphics.h"
void intgraph();
void display_cordinate(char bg_color,char line_color);
Create_poly(int poly[][2]);
void fill_poly(int poly[][2],int points,char line_color,char fill_color);
void Identity(float Matrix[3][3]);
void Translate_to_Relative(int poly[][2],int points,int Xdis,int Ydis);
void main()
134 LOVELY PROFESSIONAL UNIVERSITY