Professional Documents
Culture Documents
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.
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
Equation of a Circle:
(x h)2 + (y k)2=r2
(h, k)= Center of the Circle
r = radius of the circle
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
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)
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)
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..
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.
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.
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