Chapter Four Ai
Chapter Four Ai
Chapter Four Ai
1
outline
2
Point
3
Point
4
line
We can define a straight line with the help of the following equation.
y= mx + a Where, (x, y) = axis of the line.
m = Slope of the line.
a = Interception point
There are two algorithmic rules that are followed while drawing a line on the screen. They are DDA
(Digital Differential Analyser) algorithmic rule and Bresenham line algorithm.
Both the DDA and Bresenham's algorithm are examples of computational procedures that can be
utilised for the goal of approximating the length of a line segment.
5
DDA (Digital Differential Analyser)
6
What is Bresenham’s Algorithm?
7
DDA Line Algorithm Bresenham line Algorithm
DDA stands for Digital Differential Analyzer. While it has no full form.
DDA algorithm has less precision or accuracy. While it has more precision or accuracy.
10
Cont..
Plotting begins: For i=3
First pixel will be plotted at (2,3) x=x+∆x=4+1=5
For i=1 y=y+∆y=4.5+0.75=5.25
x=x+∆x=2+1=3 Next pixel will be plotted at (5,5.25)
y=y+∆y=3+0.75=3.75 =(5,6)
next pixel will be plotted at (3,3.75) For i=4
=(3,4) x=x+∆x=5+1=6
For i=2 y=y+∆y=5.25+0.75=6
x=x+∆x=3+1=4 Next pixel will be plotted at (6,6)
y=y+∆y=3.75+0.75=4.5
Next pixel will be plotted at (4,4.5) =(4,5)
11
Algorithm of Bresenham’s Line Drawing
Step 1: consider Starting point as (x1, y1) and Case 1: If pk < 0
ending point (x2, y2). Then
Step 2: Now, we have to calculate ▲x and ▲y. pk+1 =pk +2▲y
▲x = x2-x1 xk+1 = xk +1
▲y = y2-y1 yk+1 = yk
m = ▲y/▲x
Step 3: calculate the decision parameter pk with Case 2: If pk >= 0
the formula. Then
pk = 2▲y-▲x pk+1 =pk +2▲y-2▲x
Step 4: Now, we are going to calculate two xk+1 =xk +1
cases for decision parameter pk
yk+1 =yk +1
Step 5: We will repeat step 4 until we found the
ending point of the line and the total number of
iterations =▲x.
12
• Example: A line has a starting point (9,18) and ending point (14,22). Apply the Bresenham’s Line
Drawing algorithm to plot a line.
13
Cont..
If pk >= 0 Then Case 2 is satisfied.
Thus
pk+1 = pk +2▲y-2▲x =3+ (2 x 4) – (2 x 5) = 1
xk+1 =xk +1 = 9 + 1 = 10
yk+1 =yk +1 = 18 +1 = 19
Step 4: Now move to next step. We will calculate the coordinates until we
reach the end point of the line and the total number of iterations =▲x = 5
14
Case 1: If pk < 0 Case 2: If pk >= 0
Then Then
pk+1 =pk +2▲y pk+1 =pk +2▲y-2▲x
xk+1 = xk +1 xk+1 =xk +1
yk+1 = yk yk+1 =yk +1
15
Example: Bresenham Line Drawing
16
Example: Bresenham Line Drawing (cont.)
17
Example: Bresenham Line Drawing (cont.)
Pixel positions along the line path between endpoints (20, 10) and (30, 18), plotted with Bresenham’s line
algorithm. 18
OpenGL Point Drawing Primitives
• The most basic type of primitive is point. To draw point, we use the pair of functions
glBegin … glEnd, using the symbolic constant GL_POINTS to specify how the
19
Exercise
• Write an OPENGL program which displays four points which have the following
characteristics?
Point 1 (35.5, 56.5), Point 2 (40, 70), Point 3 (45.5, 88), Point 4 (50, 102)
20
OpenGL Line Functions
• To draw Line, we use the pair of functions glBegin … glEnd, using the symbolic constant GL_LINES
to specify how the vertices in between should be interpreted.
• glLineWidth(3.0);
• glBegin(GL_LINES);
glVertex2f(100.0, 200.0);
glVertex2f(150.0, 200.0);
glVertex2f(150.0, 250.0);
glVertex2f(200.0, 250.0);
• glEnd()
• will draw two separate line segments: one from (100,200) to (150,200) and one from (150,250) to
(200,250).
21
Cont.
• Two other symbolic constants to draw slightly different types of straight-line
primitive are: GL_LINE_STRIP and GL_LINE_LOOP.
• The following example illustrates the difference between the three types of line
primitive.
• First we define 5 points as arrays of 2 Glint values. Next, we define exactly the same
vertices for each of the three types of line primitive. The images to the right show
how the vertices will be interpreted by each primitive.
23
Cont.
• GL_LINE_STRIP create a connected polyline, in which each vertex is joined to the
one before it and after it. The first and last vertices are only joined to one other
vertex.
• glBegin(GL_LINE_STRIP);
glVertex2i(p1);
glVertex2i(p2);
glVertex2i(p3);
glVertex2i(p4);
glVertex2i(p5);
• glEnd();
24
circle
• The most important thing in drawing a circle is learning how the circle is drawn using
8-way symmetry. It is based on Mirror reflection. If we see Right hand in the mirror we
will see Left hand, Similarly if we see pixel (x,y) in mirror we will see (y,x). So point
P1(x,y) will become P2(y,x) after reflection.
25
Circle using Bresenham’s Algorithm
• We cannot display a continuous arc on the raster display. Instead, we have to choose the
nearest pixel position to complete the arc.
• From the following illustration, you can see that we have put the pixel at X,Y location
and now need to decide where to put the next pixel − at N (X+1,Y) or at S( X+1,Y−1).
This can be decided by the decision parameter d.
If d <= 0, then N(X+1,Y) is to be chosen as next pixel.
If d > 0, then S(X+1,Y−1) is to be chosen as the next pixel.
26
Cont..
27
Cont..
Bresenham's Circle Algorithm: putpixel (-x+p, y+q)
Step1: Start Algorithm putpixel (-x+p, -y+q)
Step2: Declare p, q, x, y, r, d variables putpixel (-y+p, -x+q)
p, q are coordinates of the center of the circle putpixel (y+p, -x+q)
r is the radius of the circle putpixel (x+p, -y-q)
Step3: Enter the value of r Step8: Find location of next pixels to be scanned
Step4: Calculate d = 3 - 2r If d < 0
Step5: Initialize x=0 and y= r then d = d + 4x + 6
Step6: Check if the whole circle is scan converted increment x = x + 1
If x > = y If d ≥ 0
Stop then d = d + 4 (x - y) + 10
Step7: Plot eight points by using concepts of eight-way increment x = x + 1
symmetry. The center is at (p, q). Current active pixel is (x, decrement y = y - 1
y). Step9: Go to step 6
putpixel (x+p, y+q) Step10: Stop Algorithm
putpixel (y+p, x+q)
31
Cont..
32
Cont..
33
Cont.
Advantages
• It is a simple algorithm.
• It can be implemented easily
• It is totally based on the equation of circle i.e. x2 +y2 =r2
Disadvantages
• There is a problem of accuracy while generating points.
• This algorithm is not suitable for complex and high graphic
images.
34
Plotting General Curves
• In computer graphics, we often need to draw different types of objects onto the
screen.
• Objects are not flat all the time and we need to draw curves many times to draw an
object.
• Types of Curves
• A curve is an infinitely large set of points. Each point has two neighbours except
endpoints.
• Curves can be broadly classified into three categories − explicit,
implicit, and parametric curves.
35
Cont.
• Implicit Curve
• This function would give multiple values of x for a single value of y. It is also
known as multivalued function.
• For example, the equation x2+y2=1.
• Line equation Ax + By +C= 0.
36
Cont.
• Explicit Curve
39
Cont.
• We can find the rectangular equation by eliminating the variable t i.e. solving for t.
• X =2t ⇨ t =X/2
Y =t2 ⇨ Y =(X/2)2
⇨ Y =X2/4, this makes drawing the curve easier compared using parametric equation.
Exercise: Eliminate the parameter for the following parametric equations and use the resulting
rectangular equation to graph the plane curve :
A. X=t-5 Y=t2
B. X=t+1 Y=t2+5-1
40
Polygon:
• Polygon is a representation of the surface. It is primitive which is closed in nature. It is
formed using a collection of lines. It is also called as many-sided figure. The lines combined
to form polygon are called sides or edges. The lines are obtained by combining two vertices.
• Example of Polygon:
• Triangle
• Rectangle
• Hexagon
• Pentagon
41
Cont..
42
Cont.
• GL_LINE_LOOP is the same as GL_LINE_STRIP except that the last point is joined to the first one to
create a loop.
• glBegin(GL_LINE_LOOP);
glVertex2i(p1);
glVertex2i(p2);
glVertex2i(p3);
glVertex2i(p4);
glVertex2i(p5);
• glEnd();
43