Professional Documents
Culture Documents
Chapter 3
Chapter 3
Chapter 3
5
Cont..
y = m*x +c where m is the slope of the line and c is the y-intercept
Or m is the slope or gradient of the line, and c is the coordinate at which the
line intercepts the y-axis.
Given that the two endpoints of a line (x1, y1) and (x2, y2), we can
determine m & b by:
∆𝑦 𝑦2−𝑦1
m = = , b = y1 – mx1
∆𝑥 𝑥1−𝑥1
8
DDA Cont’d …
9
DDA Cont’d…
Once we have computed values for δx and δy, the basic DDA algorithm is:
Start with (x0,y0)
Find successive pixel positions by adding on (δx, δy) and rounding to the nearest
integer, i.e.
xk+1 = xk + δx
yk+1 = yk + δy
For each position (xk,yk) computed, plot a line point at (round(xk),round(yk)), where
the round function will round to the nearest integer.
Note that the actual pixel value used will be calculated by rounding to the nearest
integer, but we keep the real-valued location for calculating the next pixel position.
DDA Cont’d…
example of applying the DDA algorithm for drawing a straight-line segment.
Given (10,10)(15,13) ( y end − y0 ) (13 − 10) 3
m = = = = 0.6
( xend − x0 ) (15 − 10) 5
Now, because |m| ≤ 1, we compute δx and δy as follows:
δx = 1
δy = 0.6
Using these values of δx and δy we can now start to plot line points:
Start with (x0,y0) = (10,10) – colour this pixel
Next, (x1,y1) = (10+1,10+0.6) = (11,10.6) – so we colour pixel (11,11)
DDA Cont’d…
If pk < 0:
Plot (xk+1,yk),
Otherwise:
Plot (xk+1,yk+1),
Exercise
Plotting the line Given (10,10)(15,13)using Bresenham’s
algorithm:
First,compute the following values:
Δx = 5
Δy = 3
2Δy = 6
2Δy - 2Δx = -4
Plot (x0,y0) = (10,10)
Iteration0:
p0 ≥ 0, so
Plot (x1,y1) = (x0+1,y0+1) = (11,11)
Iteration 1:
p1 < 0, so
Plot (x2,y2) = (x1+1,y1) = (12,11)
Iteration2:
p2 ≥ 0, so
Plot (x3,y3) = (x2+1,y2+1) = (13,12)
Iteration 3:
p3 < 0, so
Plot (x4,y4) = (x3+1,y3) = (14,12)
Iteration 4:
p4 ≥ 0, so
Plot (x5,y5) = (x4+1,y4+1) = (15,13)
We have reached the end-point, so the algorithm terminates
We can see that the algorithm plots exactly the same points as the DDA
algorithm but it computes those using only integer operations.
OpenGL Line Functions for Line Thickness and Line Style
We can draw straight-lines in OpenGL using the same glBegin … glEnd functions
that we saw for point-drawing.
This time we specify that vertices should be interpreted as line end-points by using
the symbolic constant GL_LINES. For example, the following code:
glLineWidth(3.0); // to adject the line thickness
glBegin(GL_LINES); // for change the type of lines
glVertex2f(100.0, 200.0);
glVertex2f(150.0, 200.0);
glVertex2f(150.0, 250.0);
glVertex2f(200.0, 250.0);
glEnd();
The line will be drawn in the current drawing colour and with a width defined by
the argument of the function glLineWidth.
Two other symbolic constants allow us to draw slightly different types of
straight-line primitive: 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.
void display(void){
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 340.0, 0.0, 340.0);//specifies the coordinate system OpenGL assumes
as it draws the
//final image and how the image gets mapped to the screen
glClearColor(1.0, 1.0, 1.0, 0.0); // white background
glClear(GL_COLOR_BUFFER_BIT); // Clear Screen
glColor3f(1,0,0); // red foreground
glBegin(GL_LINES);
glVertex2f(200,100);
glVertex2f(50,20);
glVertex2f(100,200);
glVertex2f(150,20);
glVertex2f(20,100);
glEnd();
glFlush(); // send all output to the display
}
void display(void){
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 340.0, 0.0, 340.0);//specifies the coordinate system OpenGL assumes
//as it draws the final image and how the image gets mapped to the screen
glClearColor(1.0, 1.0, 1.0, 0.0); // white background
glClear(GL_COLOR_BUFFER_BIT); // Clear Screen
glColor3f(0,0,0); // black foreground
glBegin(GL_LINE_STRIP);
glVertex2f(200,100);
glVertex2f(50,20);
glVertex2f(100,200);
glVertex2f(150,20);
glVertex2f(20,100);
glEnd();
glFlush(); // send all output to the display
}
void display(void){
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 340.0, 0.0, 340.0);//specifies the coordinate system OpenGL assumes
//as it draws thefinal image and how the image gets mapped to the screen
glClearColor(1.0, 1.0, 1.0, 0.0); // white background
glClear(GL_COLOR_BUFFER_BIT); // Clear Screen
glColor3f(0,0,1); // blue foreground
glBegin(GL_LINE_LOOP);
glVertex2f(200,100);
glVertex2f(50,20);
glVertex2f(100,200);
glVertex2f(150,20);
glVertex2f(20,100);
glEnd();
glFlush(); // send all output to the display
We can see that GL_LINES treat the vertices as pairs of end-points.
Lines are drawn separately and any extra vertices (i.e. a start-point with no end-
point) are ignored. GL_LINE_STRIP will 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.
Finally, 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.
27
Circle Drawing Algorithm
Some graphics packages allow us to draw circle primitives.
Before we examine algorithms for circle-drawing we will consider the
mathematical equations of a circle.
1. Cartesian coordinates we can write:
(x − x ) + ( y − y ) = r
c
2
c
2 2
2. Polar coordinates we can write:
in polar coordinates we express a position in the coordinate
system as an angle θ and a distance r.