Chapter 1 (Part 1), OCW - 3
Chapter 1 (Part 1), OCW - 3
http://ocw.ump.edu.my
Computer Graphics
Introduction to Computer
Graphics
Edited by
Dr. Md. Manjur Ahmed
Dr. Suryanti Awang
Faculty of Computer Systems and Software Engineering
[email protected]
Chapter Description
• Aims
– Basic of Computer Graphics.
• Expected Outcomes
– Understand the basic concept of computer graphics. (CO1: Knowledge)
– Ability to use the computer graphics technology. (CO1: Knowledge)
• References
– Computer Graphics by Zhigang Xiang, Schaum’s Outlines.
– Donald Hearn & M. Pauline Baker, Computer Graphics with OpenGL, 4th Edition, Boston :
Addison Wesley, 2011.
COORDINATES SYSTEM
Most windowing systems:
(0,0) x
OpenGL framebuffer:
y
(0,0) x
COORDINATES SYSTEM
Reference: Donald Hearn & M. Pauline Baker, Computer Graphics with OpenGL, 4th Edition, Boston : Addison Wesley, 2011.
• Sample code:
SetPixel ( x, y ) a function in windows.h
(x, y) (x, y)
Simple Line
Raster-scan devices address
discrete pixels
y2 y1 y
m
x2 x1 x
Therefore,
y m x
and y
x
m
Example
Digital Differential Analyzer (DDA)
Sample at unit x:
xk 1 xk x
xk 1
Corresponding y pos.:
y k 1 y k y
y k m x
y k m (1)
DDA Example
Sample at unit x:
xk 1 xk x
xk 1
Corresponding y pos.:
y k 1 y k y
y k m x
y k m (1)
DDA Example
Sample at unit x:
xk 1 xk x
xk 1
Corresponding y pos.:
y k 1 y k y
y k m x
y k m (1)
DDA Example
Sample at unit x:
xk 1 xk x
xk 1
Corresponding y pos.:
y k 1 y k y
y k m x
y k m (1)
DDA Example
Sample at unit x:
xk 1 xk x
xk 1
Corresponding y pos.:
y k 1 y k y
y k m x
y k m (1)
DDA Example
Sample at unit x:
xk 1 xk x
xk 1
Corresponding y pos.:
y k 1 y k y
y k m x
y k m (1)
DDA Example
Sample at unit x:
xk 1 xk x
xk 1
Corresponding y pos.:
y k 1 y k y
y k m x
y k m (1)
Consider endpoints:
P1(0,0), P2(7,4)
DDA Example
Let P1(2,2), P2(7, 5)
Calculate the points that made up the line P1P2
First work out m and c:
52 3 3 4
m c 2 *2
72 5 5 5
Now work for each x value work out the y value:
3 4 3
y (3) *3 2 3
5 5 5
3 4 1
y (4) * 4 3 3
5 5 5
3 4 4
y (5) * 5 3 4
5 5 5
3 4 2
y ( 6) * 6 4 4
5 5 5
DDA in C
DDA Exercise
1. Consider endpoints:
P1(0,0), P2(6, 4)
Calculate the points that made up the line P1P2
y mx c
t pt (xt+1, yt+1)
1
5
Exercise
Calculate pixel positions that made up the line
connecting endpoints: (12, 10) and (17, 14).
1. (x1, y1) = ?
2. x = ?, y =?, 2y = ?, 2y – 2x =?
3. p1 = 2y – x =?
t pt (xt+1, yt+1)
Exercise
Calculate pixel positions that made up the line
connecting endpoints: (12, 10) and (17, 14).
1. (x1, y1) = (12, 10)
2. x = 5, y = 4, 2y = 8, 2y – 2x = -2
3. p1 = 2y – x = 3
k pk (xk+1, yk+1)
1 3
3
Circle Drawing
Simple way to start
Equation of a circle:
If we solve for y:
Simple way to start
“uncertainty”
In certain cases, the slope
of the line tangent is
greater than 1. Therefore,
tangent > 1 shows the
uncertainty of this
algorithm.
Looping (or stepping) using
x won’t work here.
Reference: Computer Graphics: Principles
and Practice by James D. Foley and et.al.
Circles symmetrical nature
To solve the issue “slope
of the tangent”
Let’s take the advantage
of the circle’s symmetric
nature.
Consider both positive
and negative values for y
(or x).
Midpoint Circle Algorithm
• Consider current point is at (xk,
yk) •Use the discriminator
• Next point: (xk+1, yk), or (xk+1, function to decide:
yk-1)? f x, y x 2 y 2 r 2
• Take the midpoint: (xk+1, yk-0.5)
yk
yk-1
yk
Circle path
yk-1
xk xk+1 xk+2
xk
Midpoint
Using the circle discriminator
Based on the value return:
<0 inside the circle
f(x, y) = 0; on the circle path
>0; outside the circle
By using the midpoint between 2 pixel candidates, we can introduce a
decision parameter, pk , to decide which to plot next:
1) p k f x k 1, y k 0.5
x k 1 y k 0.5 r 2
2 2
2 and 3)
-ve: midpoint is inside the circle; plot (xk+1, yk)
pk
+ve: midpoint is outside the circle; plot (xk+1, yk-1)
If the current point is inside the
circle …
If pk < 0
We want to know f(x+1, y) so we can update p:
f(x+1, y) = (x + 1)2 + y2 – r2
f(x+1, y) = (x2 + 2x + 1) + y2 – r2
f(x+1, y) = f(x, y) + 2x + 1
Pk+1 Pk
So we increment:
p += 2x + 1
If the current point is outside the
circle …
If pk > 0
Let’s drop the subscript for a while…
p0 = 1.25 – r
**We want to use integer calculation; you can round p0
Midpoint circle algorithm
Homework
Ellipse Drawing
Equation
F2
x x1 2 y y1 2 x x2 2 y y2 2 constant d1 (x, y)
d1 + d2 = constant
Or,
2
y yc
2
x xc
1
r
ry rx y
rx
Symmetry
2-way symmetry.
• Region 1:
– Sampling is at x direction
– Choose between (xk+1, yk),
or (xk+1, yk-1)
Region 1
– Midpoint: (xk+1, yk-0.5)
• Region 2: Region 2
– Sampling is at y direction
– Choose between (xk, yk-1), or
(xk+1, yk-1)
– Midpoint: (xk+0.5, yk-1)
Conclusion of The Chapter