Module 1 CG
Module 1 CG
COMPUTER GRAPHICS
AND VISUALIZATION
18CS62
Module - 1
Dr. V. N Manju, GCEM, Bangalore.
2
Syllabus
• Overview: Computer Graphics and OpenGL: Computer
Graphics: Basics of computer graphics, Application of
Computer Graphics, Video Display Devices: Random
Scan and Raster Scan displays, graphics software.
• OpenGL: Introduction to OpenGL ,coordinate reference
frames, specifying two-dimensional world coordinate
reference frames in OpenGL, OpenGL point functions,
OpenGL line functions, point attributes, line attributes,
curve attributes, OpenGL point attribute functions,
OpenGL line attribute functions
• Line drawing algorithms(DDA, Bresenham’s), circle
generation algorithms (Bresenham’s).
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
3
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
5
Figure 2-2 Operation of an electron Figure 2-3 Electrostatic deflection of the electron
beam in a CRT.
gun with an accelerating anode.
6
CRT principles
• Kinetic energy is absorbed by the phosphor
• Part of energy is converted into heat
• The remainder causes electrons in the phosphor atom to move up
to higher quantum energy levels
• After a short time, “excited” phosphor electrons begin
dropping back to their stable ground state
• Electrons give up their extra energy as small quanta of light
(photons)
• Frequency (or color) of light emitted is in proportion to the energy
difference between the excited quantum state and the ground state
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
7
Phosphor spots
Figure 2-4 Intensity distribution of an illuminated Figure 2-5 Two illuminated phosphor spots are
phosphor spot on a CRT screen. distinguishable when their separation is greater than the
diameter at which a spot intensity has fallen to 60
percent of maximum.
8
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
9
Raster-scan display
• Electron beam is swept across the screen, one row at a
time, from top to bottom
• Each row is referred to as a scan line
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
10
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
11
Random-Scan Displays
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
12
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
13
Raster scan system have lower Random scan system have higher
Resolution than Random scan Resolution than Raster scan system.
resolution system.
In Raster scan system produced Random scan produce smooth line
Zacked lines that are plot. drawing because the CRT directly
follows line path.
Raster scan system designed for Random scan system are designed for
to display Realistic shaded line drawing application & can’t display
screen. Realistic shaded screen.
It is less expensive than Raster It is costlier than Random Scan system.
Scan system.
It is used for photos.That is Why It is used for text ,logs,letterheads.
photoshop is Raster editing
program.
Raster Scan Mainly in photos Random Scan system High Quality
like JPG,PNG,GIF File Format Images In SVG,the Size is Rescaleable
like Logo in sites
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
15
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
16
Refresh rate
• As each screen refresh takes place, we tend to see each
frame as a smooth continuation of patterns in the previous
frame as long as the refresh rate is not too low (≥ 24
frames/sec)
• < 24 frames/sec causes flickering
• Early raster-scan systems had a refresh rate of 30
frames/sec
• Currently, refresh rates are 60, 80, 120 fps (or Hertz)
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
17
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
18
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
21
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
22
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
24
Figure 2-17 Architecture of a raster system with a fixed portion of the system memory
reserved for the frame buffer.
26
X=0,Y=0
X++
X=0,Y++
X++
27
Coordinate Representation
1. First we define the shapes of individual objects, These reference frames
are called modeling coordinates or local coordinates or master
coordinates
2. Then we place the objects into appropriate locations within a scene
reference frame called world coordinates.
3. After all parts of a scene have been specified, it is processed through
various output-device reference frames for display. This process is called
the viewing pipeline.
4. Word coordinate is converted to viewing coordinate and the object
are transformed into 2D projection (what we want to see on the
output).
5. The scene is then stored in normalized coordinates. Which range
from −1 to 1 or from 0 to 1 Normalized coordinates are also referred to as
normalized device coordinates.
6. Picture is scan converted into the refresh buffer of the raster system for
display.
7. The coordinate systems for display devices are generally called device
coordinates, or screen coordinates.
29
30
Transformation Sequence
31
Graphics Functions
• It provides users with a variety of functions for creating and manipulating
pictures
•
• The basic building blocks for pictures are referred to as graphics output
primitives
•
• Attributes are properties of the output primitives
•
• We can change the size, position, or orientation of an object using
geometric transformations
•
• Modeling transformations, which are used to construct a scene.
•
• Viewing transformations are used to select a view of the scene, the type of
projection to be used and the location where the view is to be displayed.
•
• Input functions are used to control and process the data flow from these
interactive devices(mouse, tablet and joystick)
32
Software Standards
Introduction To OpenGL
✓ OpenGL basic(core) library :-A basic library of functions is provided in OpenGL
for specifying graphics primitives, attributes, geometric transformations,
viewing transformations, and many other operations.
35
Graphics System
The basic model of a graphics package is a
black box described by its input and
output.
Input Interface
• Function Calls from User Program
• Measurements from Input Devices
Output Interface
• Graphics to Output Device.
37
API Categories
• Primitive Functions
• Attribute Functions
• Viewing Functions
• Transformation Functions
• Input Functions
• Control Functions
• Query Functions
38
Library Organization
OpenGL (GL)
• Core Library
• OpenGL on Windows.
OpenGL Utility Library (GLU)
• It uses only GL functions to create common objects.
• It is available in all OpenGL implementations.
OpenGL Utility Toolkit (GLUT)
• It provides the minimum functionalities expected for
interacting with modern windowing systems.
39
Related Libraries
2) Open Inventor:- provides routines and predefined object shapes for interactive three-
dimensional applications which are written in C++.
40
Cont…
3) Window-system libraries:- To create graphics we need display window. We cannot
create the display window directly with the basic OpenGL functions since it contains
only device-independent graphics functions, and window-management operations are
device-dependent. However, there are several window-system libraries that supports
OpenGL functions for a variety of machines.
Eg:- Apple GL(AGL), Windows-to-OpenGL(WGL), Presentation Manager to
OpenGL(PGL), GLX.
4) OpenGL Utility Toolkit(GLUT):- provides a library of functions which acts as
interface for interacting with any device specific screen-windowing system, thus
making our program device-independent. The GLUT library functions are
prefixed with “glut”.
41
Header Files
In windows
#include<windows.h>
✓ #include <GLUT/glut.h>
42
Library Organization
glu – viewing , projection, complex objects
glut – windowing function
# include<windows.h>
#include<GL/glut.h>
43
Program Structure
Step 1: Initialize the interaction between windows
and OpenGL.
Step 2: Specify the window properties and further
create window.
Step 3: Set the callback functions
Step 4: Initialize the program attributes
Step 5: Start to run the program
OpenGL API
Program Framework
#include <GL/glut.h> includes gl.h
1 1 1 White
0 0 0 Black
1 0 0 Red
0 1 0 Green
0 0 1 Blue
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
46
void myDisplay()
{
/* clear the display */
glClear(GL_COLOR_BUFFER_BIT);
// code for objects
glFlush();
}
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
4
OpenGL API
Program Framework: Color Manipulation
glClearColor():establishes what color the
window will be cleared to.
glClear():actually clears the window.
glColor3f():establishes what color to use
for drawing objects.
glFlush():ensures that the drawing
command are actually executed.
Remark: OpenGL is a state machine. You put it into various
states or modes that remain in effect until you change
them
18CS62 Computer Graphics and Visualization 4 Dr. V N Manju, Dept. of CSE, GCEM,
Primitives and Attributes
Program Form of Primitives
glBegin(type);
glVertex*(…);
glVertex*(…);
.
.
glVertex*(…);
glEnd();
The basic ones are specified by a set of vertices.
The type specifies how OpenGL assembles the
vertices.
18CS62 Computer Graphics and Visualization 4 Dr. V N Manju, Dept. of CSE, GCEM,
Primitives and Attributes
Program Form of Primitives
Vertex Function: glVertex*()
•* : can be as the form [nt | ntv]
• n : the number of dimensions (2, 3, 4)
•t : data type (i: integer, f: float, and d:
double)
• v : the variables is a pointer.
glVertex2i (GLint x, GLint y);
glVertex3f(GLfloat x, GLfloat y, GLfloat
z); glVertex2fv(p); // int p[8] = {1.0,
1.0}
18CS62 Computer Graphics and Visualization 5 Dr. V N Manju, Dept. of CSE, GCEM,
Primitives and Attributes
Points and Line Segment
Point: GL_POINTS
Line Segments: GL_LINES
Polygons:
GL_POLYGON
GL_QUADS
GL_TRIANGLES
• GL_LINE_STRI GLP
• GL_LINE_LOOP
18CS62 Computer Graphics and Visualization 5 Dr. V N Manju, Dept. of CSE, GCEM,
52
1
X Y
0.2 0.2
0.6 0.2
.1 .2 .3 .4 .5 .6 .7 .8 .91
0.6 0.6
0.2 0.6
.1 .2 .3 .4 .5 .6 .7 .8 .91 1
-1
-1
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
53
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
54
X Y
4 3
7 3
7 6
4 6
(4,6) (7,6)
Triangle (L,T) (R,T)
1 8
3 8
2 10
(4,3) (7,3)
(L,B) (R,B)
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
55
X Y
10 20 30 40 50 60 70 80 90 100
30 30
60 30
60 60
30 60
Triangle
1 8
(100 X 100) 3 8
2 10
0
0 10 20 30 40 50 60 70 80 90 100
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
56
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
57
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
Primitives and Attributes
Polygon Primitives
Polygons: GL_POLYGON
Triangles: GL_TRIANGLES
Quadrilaterals: GL_QUADS
Stripes: GL_TRIANGLE_STRIP
Fans: GL_TRIANGLE_FAN
18CS62 Computer Graphics and Visualization 5 Dr. V N Manju, Dept. of CSE, GCEM,
Primitives and Attributes
Polygon Primitives
Polygons: GL_POLYGON
Triangles: GL_TRIANGLES
Quadrilaterals: GL_QUADS
Stripes: GL_TRIANGLE_STRIP
Fans: GL_TRIANGLE_FAN
18CS62 Computer Graphics and Visualization 5 Dr. V N Manju, Dept. of CSE, GCEM,
OpenGL
Viewing
Description
The viewing is to describe how we would like
these objects to appear.
The concept is just as what we record in a
photograph
• Camera Position
• Focal Lens
View Models
• Orthographic Viewing
• Two-Dimensional Viewing
18CS62 Computer Graphics and Visualization 6 Dr. V N Manju, Dept. of CSE, GCEM,
OpenGL
Viewing
Orthographic View
It is the simple and OpenGL’s default view
It is what we would get if the camera has an
infinitely long lens.
All projections become parallel
18CS62 Computer Graphics and Visualization 6 Dr. V N Manju, Dept. of CSE, GCEM,
OpenGL
Top Viewing
Two-Dimensional Viewing
It is a special case of three-dimensional graphics
Viewing rectangle is in the plane z=0.
void gluOrtho2D(GLdouble left, GLdouble right,
GLdouble bottom, GLdouble top);
LEFT –xmin,
Right – Xmax
Bottom – ymin
Top – ymax
18CS62 Computer Graphics and Visualization 6 Dr. V N Manju, Dept. of CSE, GCEM,
OpenGL
Viewing
Orthographic View
The parameters are distances measured from
the camera
It sees only the objects in the viewing volume.
OpenGL Default
• Cube Volume: 2x2x2
18CS62 Computer Graphics and Visualization 6 Dr. V N Manju, Dept. of CSE, GCEM,
OpenGL
Viewing
Two-Dimensional Viewing
It directly takes a viewing rectangle (clipping
rectangle) of our 2D world.
The contents of viewing rectangle is
transferred to the display.
18CS62 Computer Graphics and Visualization 6 Dr. V N Manju, Dept. of CSE, GCEM,
65
glBegin (GL_POINTS);
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
66
➢ we could specify the coordinate values for the preceding points in arrays
such as int point1 [ ] = {50, 100};
int point2 [ ] = {75, 150};
int point3 [ ] = {100, 200};
and call the OpenGL functions for plotting the three points as
glBegin (GL_POINTS);
glVertex2iv (point1);
glVertex2iv (point2);
glVertex2iv (point3);
glEnd ( );
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
67
Point Attributes
• we can set two attributes for points: color and size.
• We set the size for an OpenGL point with glPointSize
(size);
• The default value for point size is 1.0.
glColor3f (1.0, 0.0, 0.0);
glBegin (GL_POINTS);
glVertex2i (50, 100);
glPointSize (2.0);
glColor3f (0.0, 1.0, 0.0);
glVertex2i (75, 150);
glPointSize (3.0);
glColor3f (0.0, 0.0, 1.0);
glVertex2i (100, 200);
glEnd
18CS62 Computer Graphics and(Visualization
); Dr. V N Manju, Dept. of CSE, GCEM,
68
glBegin (GL_LINES);
glVertex2iv (p1);
glVertex2iv (p2);
glVertex2iv (p3);
glVertex2iv (p4);
glVertex2iv (p5);
glEnd ( );
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
69
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
70
glBegin (GL_LINES_LOOP);
glVertex2iv (p1);
glVertex2iv (p2);
glVertex2iv (p3);
glVertex2iv (p4);
glVertex2iv (p5);
glEnd ( );
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
71
glBegin (GL_LINES_STRIP);
glVertex2iv (p1);
glVertex2iv (p2);
glVertex2iv (p3);
glVertex2iv (p4);
glVertex2iv (p5);
glEnd ( );
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
72
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
73
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
74
• repeatFactor
• Integer parameter repeatFactor specifies how many times
each bit in the pattern is to be repeated before the next bit
in the pattern is applied.
• The default repeat value is 1.
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
75
• glDisable (GL_LINE_STIPPLE);
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
76
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
77
Assignment
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
78
X Y
10 20 30 40 50 60 70 80 90 100
0
0 10 20 30 40 50 60 70 80 90 100
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
3.1 COORDINATE REFERENCE FRAMES
To describe a picture, we first decide upon
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
3.1 COORDINATE REFERENCE FRAMES
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
3.1 COORDINATE REFERENCE FRAMES
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
3.1 COORDINATE REFERENCE FRAMES
• Once pixel positions have been identified the color values must be
stored in the frame buffer
• Assume we have available a low-level procedure of the form
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
3.1 Absolute and Relative Coordinate Specifications
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
84
Syllabus
• Overview: Computer Graphics and OpenGL: Computer
Graphics: Basics of computer graphics, Application of
Computer Graphics, Video Display Devices: Random
Scan and Raster Scan displays, graphics software.
• OpenGL: Introduction to OpenGL ,coordinate reference
frames, specifying two-dimensional world coordinate
reference frames in OpenGL, OpenGL point functions,
OpenGL line functions, point attributes, line attributes,
curve attributes, OpenGL point attribute functions,
OpenGL line attribute functions
• Line drawing algorithms(DDA, Bresenham’s),
circle generation algorithms (Bresenham’s).
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
Line Drawing Algorithms
Lines drawn as pixels
Graphics system
1. Projects the endpoints to their pixel locations in
the frame buffer (screen coordinates as integers)
2. Finds a path of pixels between the two
3. Loads the color
4. Plots the line on the monitor from frame buffer
(video controller)
Rounding causes all lines except horizontal or
vertical to be displayed as jigsaw appearance (low
resolution)
Improvement: high resolution, or adjust pixel
intensities on the line path.
85
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
8 9 10 11 12 13 14 15 16 17 18 19 20
(13,13)
5 6 7
2 3 4
(3,3)
1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
8 9 10 11 12 13 14 15 16 17 18 19 20
14,7
5 6 7
2 3 4
1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
8 9 10 11 12 13 14 15 16 17 18 19 20
5 6 7
2 3 4
1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
Line Drawing Algorithms (cont.)
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
Line Drawing Algorithms (cont.)
Line equation
Slope-intercept form
y=mx+b
slope m
y-intercept b
𝑦 𝑒𝑛𝑑 − 𝑦 0 𝛿 𝑦
Slope 𝑚= =
𝑥 𝑒𝑛𝑑 − 𝑥 0 𝛿 𝑥
y-intercept
𝑏= 𝑦 0 − 𝑚 𝑥 0
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
Calculating the values
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
Setting Deflection Values
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
Line Sampling
if m 1.0, y k +1 = y k - m ( δ x = -1)
1
if m > 1.0, x k +1 = x k - ( δ y = -1)
m
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
8 9 10 11 12 13 14 15 16 17 18 19 20
(13,13)
5 6 7
(xk+1,yk+1) = xk+1,yk+1
2 3 4
(xk,yk)
(X0,y0)(15,3)
1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
8 9 10 11 12 13 14 15 16 17 18 19 20
14,7
5 6 7
2 3 4
(xk+1,yk+1) = xk+1,yk
(xk,yk)
1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
DDA Algorithm
void lineDDA (int x0, int y0, int xEnd, int yEnd)
{
int dx = xEnd - x0, dy = yEnd - y0, steps, k;
float xIncrement, yIncrement, x = x0, y = y0;
if (fabs (dx) > fabs (dy))
steps = fabs (dx);
else
steps = fabs (dy);
xIncrement = float (dx) / float (steps);
yIncrement = float (dy) / float (steps);
setPixel (round (x), round (y));
for (k = 0; k < steps; k++) {
x += xIncrement;
y += yIncrement;
setPixel (round (x), round (y));
}
}
97
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
DDA Algorithm (cont.)
Advantage
Does not calculate coordinates based on the
complete equation (uses offset method)
Disadvantage
Round-off errors are accumulated, thus line
diverges more and more from straight line
Round-off operations take time
Perform integer arithmetic by storing float as integers
in numerator and denominator and performing
integer arithmetic.
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
Line Drawing Algorithms (cont.)
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
Vertical Distances
decision parameter:
pk= Δx (dlower – dupper )
pk = 2Δy xk- 2Δx yk + c
sign of pk is the same as sign of dlower – dupper
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
Bresenham’s Line Drawing
Algorithm (cont.)
1. Input the two line endpoints and store the left
endpoint in (x0, y0).
2. Set the color for the frame-buffer position (x0, y0)
– i.e. plot the first point.
3. Calculate the constant 2Δy –Δx, and set the
starting value for the decision parameter as p0 =
2Δy –Δx.
4. At each xk along the line, from k=0, perform the
following test:
if pk<0, next point to plot is (xk + 1, yk) and pk+1 = pk + 2Δy
otherwise, next point to plot is (xk + 1, yk + 1 )
and pk+1 = pk + 2Δy- 2Δx
5. Repeat step 4 Δx - 1 times.
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
Algorithm |M|<1.0
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
Example: Bresenham Line
Drawing
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
Example: Bresenham Line
Drawing (cont.)
|M|<1.0
Starting Point (x0, y0)
P0=2Δy –Δx (decision parameter)
IF P0<0
(xk + 1, yk)
pk+1 = pk + 2Δy
ELSE
(xk + 1, yk + 1 )
pk+1 = pk + 2Δy- 2Δx
REPEAT Δx - 1
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
Example: Bresenham’s Line
Drawing (cont.)
1112 13 14
23 24
Pixel positions along the line path between endpoints (20, 10) and (30, 18), plotted
with Bresenham’s line algorithm.
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
Different m (slope)
X1, y1 = (10,10)
X2, y2 = (20,380)
M=dy/dx = 370/10 > 1
X1,y1 = (10,20)
X2,y2= (360,370) = 1
X1,y1 = (10,30)
X2,y2 = (360,370)
M <Graphics
Computer
18CS62
1 and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
dx = x2-x1;
dy = y2-y1; |M|<1.0
Starting Point (x0, y0)
if (dx < 0) dx = -dx; P0=2Δy –Δx (decision parameter)
if (dy < 0) dy = -dy; IF P0<0
incx = 1;
if (x2 < x1) incx = -1;
(xk + 1, yk)
incy = 1; pk+1 = pk + 2Δy
if (y2 < y1) incy = -1; ELSE
x = x1; y = y1; (xk + 1, yk + 1 )
if (dx > dy) //m<1.0
{ pk+1 = pk + 2Δy- 2Δx
draw_pixel(x, y); // user-defined function REPEAT Δx - 1
pk = 2 * dy-dx; //decision parameter
inc1 = 2*(dy-dx);
inc2 = 2*dy;
for (i=0; i<dx; i++)
{
if (pk >= 0) {
y += incy;
pk=pk + inc1;
}
else
pk=pk + inc2;
x += incx;
draw_pixel(x, y);
}
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
else { |M|>1.0
Starting Point (x0, y0)
draw_pixel(x, y); P0=2Δx –Δy (decision parameter)
e = 2*dx-dy; IF P0<0
inc1 = 2*(dx-dy); (xk , yk+1)
inc2 = 2*dx; pk+1 = pk + 2Δx
for (i=0; i<dy; i++) { ELSE
if (e >= 0) { (xk + 1, yk + 1 )
pk+1 = pk + 2Δx- 2Δy
x +=
REPEAT Δy - 1
incx;
e +=
inc1;
}
else
e +=
inc2;
y += incy;
draw_pixel(x,
y);
}
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
}
Midpoint Circle Algorithm
•Midpoint circle algorithm generates all points on a circle centered at the origin by incrementing all
the way around circle.
•The strategy is to select which of 2 pixels is closer to the circle by evaluating a function at the
midpoint between the 2 pixels
•To apply the midpoint method, we define a circle function as
•To summarize, the relative position of any point (x, y) can be determined by checking the sign of
the circle function as follows:
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
(Y)
(-X)
(Cx,cy) (radius) (X)
(-y)
(ry)
(Cx,cy) (rX)
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
Midpoint Circle Algorithm
1. Input radius r and circle center (xc , yc ), then set the coordinates for the first point on the circumference of
a circle centered on the origin as
(x0, y0) = (0, r )
If pk <0, the next point along the circle centered on (0, 0) is (xk+1, yk ) and pk+1 = pk +
2xk+1 + 1
Otherwise, the next point along the circle is (xk + 1, yk − 1) and
pk+1 = pk + 2xk+1 + 1 – 2yk+1
where 2xk+1 = 2xk + 2 and 2yk+1= 2yk − 2.
5. Move each calculated pixel position (x, y) onto the circular path centered at (xc , yc ) and plot the
coordinate values as follows:
x = x + xc , y = y + yc
6. Repeat steps 3 through 5 until x ≥ y.
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,
18CS62 Computer Graphics and Visualization Dr. V N Manju, Dept. of CSE, GCEM,