0% found this document useful (0 votes)
14 views47 pages

Note 4

Graphics

Uploaded by

deysarnabhahope
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views47 pages

Note 4

Graphics

Uploaded by

deysarnabhahope
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Computer Graphics:

Maths Preliminaries and Lines

Sourav Pramanik
Assistant Professor, New Alipore College

Course Website: http://www.comp.dit.ie/bmacnamee


2
of
30
Introduction
Computer graphics is all about maths!
None of the maths is hard, but we need to
understand it well in order to be able to
understand certain techniques
Today we’ll look at the following:
– Coordinate reference frames
– Points & lines
Images taken from Hearn & Baker, “Computer Graphics with OpenGL” (2004)
3
of
30
Big Idea
4
of
30
Coordinate Reference Frames – 2D
When setting up a scene in computer
graphics we define the scene using simple
geometry
y axis
For 2D scenes we use
simple two dimensional y
P

Cartesian coordinates
All objects are defined
using simple coordinate
pairs x x axis
5
of
Coordinate Reference Frames – 2D
30 (cont…)

(2, 7) (7, 7)
7

3
(2, 3) (7, 3)

x
2 7
6
of
30
Coordinate Reference Frames – 3D
For three dimensional scenes we simply add
an extra coordinate
y axis

z
x

z axis x axis
7
of
30
Left Handed Or Right Handed?
There are two different ways in which we
Images taken from Hearn & Baker, “Computer Graphics with OpenGL” (2004)

can do 3D coordinates – left handed or right


handed

We will mostly use


the right-handed
system

Right-Hand Left-Hand
Reference System Reference System
8
of
30
Points & Lines
Points:
– A point in two dimensional space is given as
an ordered pair (x, y)
– In three dimensions a point is given as an
ordered triple (x, y, z)
Lines:
– A line is defined using a start point and
an end-point
• In 2d: (xstart, ystart) to (xend, yend)
• In 3d: (xstart, ystart , zstart) to (xend, yend , zend)
9
of
30
Points & Lines (cont…)
y

(2, 7) (6, 7)

The line from


(2, 7) to (7, 3)
(2, 3) (7, 3)

(7, 1)

x
10
of
30
Line Drawing Algorithm
 Programmer specifies (x, y) values of
end pixels.
 Need algorithm to figure out which
intermediate pixels are on line path. y

 Pixel (x, y) values constrained to 5


integer values.
??
 Actual computed intermediate line
values may be floats.
2
 Rounding may be required. Ex.
Computed point (10.48, 20.51)
rounded to (10, 21). x
2 3 4 5 6 7
 Rounded pixel value is off actual line
path. Line: (2, 2) to (7, 7)
 Sloped lines end up having jaggies. Which intermediate
pixels to turn on?
 Vertical, horizontal lines, no jaggies.
11
of
30
The Equation of A Line
The slope-intercept y

equation of a line is:


y  m x b yend

where:
yend  y0 y0
m
xend  x0 x
x0 xend
b  y0  m  x0
The equation of the line gives us the
corresponding y point for every x point
12
of
30
The Equation of A Line
13
of
30
A Simple Example
Example 1 The endpoints of line are(0,0) & (6,18).
Compute each value of y as x steps from 0 to 6 and plot
the result.
Solution : Equation of line is y= mx +b
m = y2-y1/x2-x1= 18-0/6-0 = 3
Next the y intercept b is found by plugging y1& x1 into the
equation y = 3x + b,
0 = 3(0) + b. Therefore, b=0, so the equation for the line
is y= 3x.
14
of
30
A Simple Example
Let’s draw a portion of the line given by the
equation:
3 4
y  x
5 5
Just work out the y coordinate for each x
coordinate
15
of
30
A Simple Example (cont…)
For each x value just work out the y value:
3 4
y (2)   2   2
5 5
3 4 3 y
y(3)   3   2 5
5 5 5
3 4 1
y (4)   4   3
5 5 5
3 4 4 2
y (5)   5   3
5 5 5
3 4 2 x
y (6)   6   4 2 3 4 5 6 7
5 5 5
3 4
y (7 )   7   5
5 5
16
of
30

• The challenge is to find a way to calculate the next x,y


position by previous one as quickly as possible.

• While this approach is mathematically sound, it


involves floating-point computation (multiplication &
addition) in every step that uses the line equation since
m & b are generally real numbers. The challenge is to
find a way to achieve the same goal as quickly as
possible.
17
of DDA (Digital Differential Analyzer) Algorithm
30

12

10

0 2 4 6 8 10 12 14 16 17 18 x
18
of
DDA (Digital Differential Analyzer)
30
Algorithm
x y

2 2

3 2

4 2

5 2

6 2

7 2

8 2

9 2
19
of
DDA (Digital Differential Analyzer)
30
Algorithm
x y

2 5

2 6

2 7

2 8

2 9

2 10

2 11

2 12
20
of
DDA (Digital Differential Analyzer)
30
Algorithm
x y

5 4 4

6 4.4 4

7 4.8 5

8 5.2 5

9 5.6 6

10 6 6

11 6.4 6

12 6.8 7
21
of
DDA (Digital Differential Analyzer)
30
Algorithm
X y

5 5 7

6 5.6 8

6 6.2 9

7 6.8 10

7 7.4 11

8 8 12

9 8.6 13

9 9.2 14
10 9.8 15
22
of
DDA (Digital Differential Analyzer)
30
Algorithm
x y
23
of
DDA (Digital Differential Analyzer)
30
Algorithm
24
of
30
Vectors
Vectors:
– A vector is defined as the difference between
two points
– The important thing is that a vector has a
direction and a length
What are vectors for?
– A vector shows how to move from one point
to another
– Vectors are very important in graphics -
especially for transformations
25
of
30
Vectors (2D)
To determine the vector between two points
simply subtract them
P2 (7, 10)
y axis
V  P2  P1
V P (6, 7) P2 (10, 7)
 ( x2  x1 , y2  y1 )
2

P1 (2, 6)

 (6  1, 7  3)
V V

P1 (1, 3) P1 (5, 3)
 (5, 4)
x axis

WATCH OUT: Lots of pairs of points share the same


vector between them
26
of
30
Vectors (3D)
In three dimensions a vector is calculated in
much the same way
y axis V  P2  P1
 ( x2  x1 , y2  y1 , z2  z1 )
 (Vx , V y , Vz )
P2
So for (2, 1, 3) to (7, 10, 5)
we get

P1
 (7  2, 10  1, 5  3)
 (5,9,2)
z axis x axis
27
of
30
Vector Operations
There are a number of important operations
we need to know how to perform with
vectors:
– Calculation of vector length
– Vector addition
– Scalar multiplication of vectors
– Scalar product
– Vector product
28
of
30
Vector Operations: Vector Length
Vector lengths are easily calculated in two
dimensions:
| V | V  V
x
2
y
2

and in three dimensions:

| V | Vx2  V y2  Vz2
29
of
30
Vector Operations: Vector Addition
The sum of two vectors is calculated by
simply adding corresponding components
V1  V 2  (V1x  V2 x , V1 y  V2 y )
y axis y axis

V1 + V2
V2 V2

V1
V1
x axis x axis

Performed similarly in three dimensions


30
of
30
Vector Operations: Scalar Multiplication

Multiplication of a vector by a scalar proceeds


by multiplying each of the components of the
vector by the scalar
sV  ( sVx , sVy )
y axis y axis (sVx, sVy)

(Vx, Vy) sV

x axis x axis
31
of
30
Other Vector Operations
There are other important vector operations
that we will cover as we come to them
These include:
– Scalar product (dot product)
– Vector product (cross product)
32
of
30
Matrices
A matrix is simply a grid of numbers
 1 11 13   4 .3 
10 4  3  6 .7   4 8 15 
  1 2 3 4   16 23 42
 2 0 6  1.2   

However, by using matrix operations we can


perform a lot of the maths operations
required in graphics extremely quickly
33
of
30
Matrix Operations
The important matrix operations for this
course are:
– Scalar multiplication
– Matrix addition
– Matrix multiplication
– Matrix transpose
– Determinant of a matrix
– Matrix inverse
34
of
30
Matrix Operations: Scalar Multiplication

To multiply the elements of a matrix by a


scalar simply multiply each one by the scalar
a b c  s *a s *b s *c 
s * d e f    s * d s * e s * f 
 g h i   s * g s * h s * i 

Example:
 2 4 6   6 12 18 
3 *  8 10 12  24 30 36
14 16 18 42 48 54
35
of
30
Matrix Operations: Addition
To add two matrices simply add together all
corresponding elements
a b c  r s t  a  r b  s ct 
d e f   u v w  d  u e  v f  w

 g h i   x y z   g  x h  y i  z 

Example:
 2 4 6   3 5 7   5 9 13 
 8 10 12   9 11 13  17 21 25
     
14 16 18 15 17 19 29 33 37

Both matrices have to be the same size


36
of
30
Matrix Operations: Matrix Multiplication

We can multiply two matrices A and B


together as long as the number of columns
in A is equal to the number of rows in B
So, if we have an m by n matrix A and a p
by q matrix B we get the multiplication:
C=AB
where C is a m by q matrix whose elements
are calculated as follows:
n
cij   aik bki
k 1
37
of
Matrix Operations: Matrix Multiplication
30 (cont…)
Examples:
 0  1 0 *1  (1) * 3 0 * 2  (1) * 4  3  4
5  1 2     26 38 
 7  3 4  5 *1  7 * 3 5 * 2  7 * 4   
 2 8      2 *1  8 * 3  2 * 2  8 * 4   22 28 
 4
1 2 3 5  1* 4  2 * 5  3 * 6  32
6
 4 4 *1 4 * 2 4 * 3 4 8 12
5 1 2 3  5 *1 5 * 2 5 * 3  5 10 15
     
6 6 *1 6 * 2 6 * 3 6 12 18
38
of
Matrix Operations: Matrix Multiplication
30 (cont…)
Watch Out! Matrix multiplication is not
commutative, so:
AB  BA
39
of
30
Matrix Operations: Transpose
The transpose of a matrix M, written as MT
is obtained by simply interchanging the rows
and columns of the matrix
For example:

T 1 4 
1 2 3  2 5
 4 5 6   
  3 6
40
of
30
Other Matrix Operations
There are some other important matrix
operations that we will explain as we need
them
These include:
– Determinant of a matrix
– Matrix inverse
41
of
30
Pixel
• Pixels are the basic building block of
graphics display.
• We can call it Picture Element.
• The pixel is a smallest size object or color
spot that can be displayed and addressed
on a screen.
• Any image that is displayed on the
monitor is made up of thousands of
pixels.
42
of
30
Dot
• The internal surface of the monitor screen
is coated with Red, Green, Blue phosphor
material. That glows when struck by the
stream of electrons.
• The coated material is arranged into an
array of millions of tiny cells.
• Red, Green, Blue.- these colored cells are
usually called dots.
43
of
30
Summary
In this lecture we have taken a brief tour
through the following:
– Basic idea
– The mathematics of points, lines and vectors
– The mathematics of matrices
These tools will equip us to deal with the
computer graphics techniques that we will
begin to look at, starting next time
44
of
30
Exercises 1
Plot the line y = ½x + 2 from x = 1 to x = 9
y

0 1 2 3 4 5 6 7 8 9 10 x
45
of
30
Exercises 2
Perform the following matrix additions:

11  19  15 5   1  14  5 1  __ __ __ __ 

 3 20  11 0  16 10  12  11  __ __ __ __ 
 14 5  3 6  10  15 15 5   __ __ __ __ 
  
 15 2 9  18  1 14 9 0   __ __ __ __ 
     
  19 4  15 10   3  3  16  5   __ __ __ __ 

13  3  4   1  15  7  __ __ __ 
19 9    
 8   14 5 17   __ __ __ 
46
of
30
Exercises 3
Perform the following matrix multiplications:
  15 19 
 
8 15 19 4  12  19  __________ __________  __ __ 
7  4 12 3 *  0   
   13  __________ __________  __ __ 
 
 10 7 
16 10  12  11  4   _________________   __ 
10  15 15 5  11  _________________   __ 
 *     
 1 14 9 0   6   _________________   __ 
       
 3  3  16  5    
3 _________________   __ 
47
of
30
Exercises 4
Perform the following multiplication of a
matrix by a scalar
15 19   __ __ 
 2 5   __ __ 
6*  
 0  1  __ __ 
   
 1 7   __ __ 

Calculate the transpose of the following


matrix
T
 3 11
 6 4    __ __ __ 
   __ __ 
23 7   __

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy