Computer Graphics (CSE 4103)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

Department of Computer Science and Engineering,

Computer Graphics (CSE 4103)

University of Dhaka
Lecture 4-5
Basic Raster Graphics Algorithm for
Drawing 2D Primitives
Line Drawing
Line drawing is fundamental to computer graphics.
We must have fast and efficient line drawing functions.

Rasterization Problem: Given only the two end points, how to


compute the intermediate pixels, so that the set of pixels closely
approximate the ideal line.
Last Class
1. Incremental Line-drawing Algorithm
2. Mid-point Algorithm for Line-drawing
i) In only one Octant
ii) In all 8 octants
iii) 8-way Symmetry
iv) Tangent Independent mid-point line drawing algorithm
This Class
1. Mid-point Algorithm for Drawing Circle
2. Mid-point Algorithm for Drawing Ellipse
Drawing Circle with Mid-point Algorithm
N

Radius = r

r
W a r E

a
Cos 450 = r

r
a=
S
Drawing Circle with Mid-point Algorithm..
N

8-way
Symmetry
(-y, x) (y, x)

(-x, y) (x, y)
W E
(x, -y)
(-x, -y)
(-y, -x) (y, -x)

S
Drawing Circle with Mid-point Algorithm..

(xi, yi) E
N
M

W E
SE

Previous Choices Choices


Pixel for current for next
pixel pixel
Drawing Circle with Mid-point Algorithm..

Equation of a Circle:
(x h)2 + (y k)2=r2
(h, k)= Center of the Circle
r = radius of the circle

Equation of a Circle centered at the origin:


F(x,y) = x2 +y2 r2 = 0
Drawing Circle with Mid-point Algorithm..

E
F(M) = (x+1)2 +(y-1/2)2 r2 = 0
(xi, yi)
M
d (or dold)= (x+1)2 +(y-1/2)2 r2 = 0

SE M
M

Previous Choices Choices


Pixel for current for next
pixel pixel Case 1 Case 2

Condition Explanation Point to Select Resulting y value


If d<0 The point is within the Circle (Case 1) E yi+1 = yi
If d>=0 The point is outside the Circle (Case 2) SE yi+1 = yi -1
Drawing Circle with Mid-point Algorithm..

E
F(M) = (x+1)2 +(y-1/2)2 r2 = 0
(xi, yi)
M
d (or dold)= (x+1)2 +(y-1/2)2 r2 = 0
If(d<0), point (xi+1, yi) is selected
SE
Coordinate of next midpoint is (xi+2, yi-1/2)

dnew1= (x+2)2 +(y-1/2)2 r2 = 0


Previous Choices Choices
Pixel for current for next
pixel pixel
dnew1= dold+ 2xi +3

Condition Explanation Point to Select Resulting y value


If d<0 The point is within the Circle (Case 1) E yi+1 = yi
If d>=0 The point is outside the Circle (Case 2) SE yi+1 = yi -1
Drawing Circle with Mid-point Algorithm..

E
F(M) = (x+1)2 +(y-1/2)2 r2 = 0
(xi, yi)
M
d (or dold)= (x+1)2 +(y-1/2)2 r2 = 0
If(d>=0), point (xi+1, yi-1) is selected
SE
Coordinate of next midpoint is (xi+2, yi-3/2)

dnew2= (x+2)2 +(y-3/2)2 r2 = 0


Previous Choices Choices
Pixel for current for next
pixel pixel
dnew2= dold+ 2xi - 2yi +5

Condition Explanation Point to Select Resulting y value


If d<0 The point is within the Circle (Case 1) E yi+1 = yi
If d>=0 The point is outside the Circle (Case 2) SE yi+1 = yi -1
Drawing Circle with Mid-point Algorithm..

E
F(M) = (x+1)2 +(y-1/2)2 r2 = 0
(xi, yi)
M
F(1, r-1/2) = (1)2 +(r-1/2)2 r2
= 5/4- r
SE

dstart= 5/4-r
Previous
Pixel
Choices Choices
for current for next
dstart= 1-r
pixel pixel
Drawing Circle with Mid-point Algorithm..
void midpointCircle(int r)
{
int x=0, y= r;
int d= 1- r;

while(y>=x)
{
DrawPixels(x, y);
if(d<0)
d+ = 2*x + 3;
else
{
d+ = 2*(x-y) + 5;
y--;
}
x++;
}
}
Drawing Circle with Mid-point Algorithm..

Calculate the decision variables for other 7 octants


Drawing Ellipse with Mid-point Algorithm
N

Tangent
(xi, yi)
E Slope = -1
Gradient Vector
SE
Region - 1
S SE
Region - 2
W E

S
Two 4-way Symmetry
Drawing Ellipse with Mid-point Algorithm
N Equation of Ellipse:

Tangent
(xi, yi)
E Slope = -1
Gradient Vector At the switching point between Region 1 and
SE
Region 2, the slope of the curve is -1
Region - 1
Region - 2 S SE Determining this point is more complex than
E it was for circle.

The vector which is perpendicular tangent of the


curve at that point (P) called Gradient Vector and
defined as:

S
Drawing Ellipse with Mid-point Algorithm
N

Tangent
(xi, yi)
E Slope = -1
Gradient Vector
SE At that point, the gradient vector has a slope of 1,
Region - 1
Region - 2 S SE i.e., when the i and j components are of equal
E magnitude.

In Region 1: The j component of the Gradient is larger than the i component


In Region 2: The i component of the Gradient is larger than the j component

If , at the next midpoint (while calculating points for Region-1), (xp+1, yp-1/2), I
S component of the Gradient is larger than the j component, we switch from
Region-1 to Region-2
Drawing Ellipse with Mid-point Algorithm..
N Decision Variable Calculation for Region-1
F(M) = d (or dold1)= b2(x+1)2 + a2(y-1/2)2-a2b2 = 0
Tangent
(xi, yi)
E Slope = -1
Gradient Vector
SE
Region - 1
SE
Region - 2 S
E If E is selected
dnew11 = b2(x+2)2 + a2(y-1/2)2-a2b2 = 0
dnew11 = dold1+ b2(2x+3)

If SE is selected
dnew12 = b2(x+2)2 + a2(y-3/2)2-a2b2 = 0
S
dnew12 = dold1+ b2(2x+3) + a2(-2y+2)
Drawing Ellipse with Mid-point Algorithm..
N Decision Variable Calculation for Region-2
F(M) = d (or dold2)= b2(x+1/2)2 + a2(y-1)2-a2b2 = 0
Tangent
(xi, yi)
E Slope = -1
Gradient Vector
SE
Region - 1
SE
Region - 2 S
E If S is selected
dnew21 = b2(x+1/2)2 + a2(y-2)2-a2b2 = 0
dnew21 = dold2+ a2(-2y+3)

If SE is selected
dnew22 = b2(x+3/2)2 + a2(y-2)2-a2b2 = 0
S
dnew22 = dold2+ b2(2x+2) + a2(-2y+3)
Drawing Ellipse with Mid-point Algorithm..
N Decision Variable for Initial M (both the Regions)
Region-1
Tangent
(xi, yi)
E Slope = -1 dstart1= F(1, b-1/2 )
Gradient Vector
SE
E
= 1 + a2(b-1/2)2-a2b2
Region - 1
Region - 2 S SE
SE
= b2 - a2b + a2/4
E

Region-2
S SE dstart2= b2(x+1/2)2 + a2(y-1)2 -a2b2

S
Write down the mid-point ellipse drawing
algorithm
Task/ Assignment-1:
Implement Midpoint algorithm for
a) Line drawing ((rollnumber%8+1)th octant)
b) Circle drawing ((rollnumber%8+1)th octant)
c) Ellipse drawing ((rollnumber%4+1)th quadrant)

Hint: Implement the above three algorithms in three main modules (using
different other required modules), then rather than printing the pixel, just
leave the putpixel (x,y)/printpixel(x,y) function empty or simply use printf to
show the (x, y) values. In the first lab, I will introduce you with OpenGL and
then we will plug-in your code in the openGL.
Thank you

You might also like