0% found this document useful (0 votes)
18 views

CG-UNIT-II

The document covers Unit II of a Computer Graphics course, focusing on output primitives such as points, lines, circles, and ellipses. It details line drawing algorithms including DDA and Bresenham's, explaining their calculations and implementations, along with examples in Python. Additionally, it discusses circle and ellipse generation algorithms, emphasizing symmetry and midpoint methods for efficient rendering.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

CG-UNIT-II

The document covers Unit II of a Computer Graphics course, focusing on output primitives such as points, lines, circles, and ellipses. It details line drawing algorithms including DDA and Bresenham's, explaining their calculations and implementations, along with examples in Python. Additionally, it discusses circle and ellipse generation algorithms, emphasizing symmetry and midpoint methods for efficient rendering.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Subject: Computer Graphics III-II CSE –A1

Computer Graphics
UNIT-II

UNIT- 2 Contents
Output Primitives
1. Points and Lines

2. Line Drawing Algorithms – DDA and Brasenhams

3. Circle Generation Algorithm

4. Ellipse Generation Algorithm

Introduction:

A picture can be described in several ways. a picture is completely specified by the set of
intensities for the pixel positions in the display. At the other extreme, we can describe a picture
as a set of complex objects, such as trees and terrain or furniture and walls, positioned at
specified coordinate locations within the scene.

Shapes and colors of the objects can be described internally with pixel arrays or with sets of
basic geometric structures, such as straight line segments and polygon color areas.

Typically, graphics programming packages provide functions to describe a scene in terms of


these basic geometric structures, referred to as output primitives, and to group sets of output
primitives into more complex structures.

Points and straight line segments are the simplest geometric components of pictures. Additional
output primitives that can be used to construct a picture include circles and other conic sections.

Points:

 Point plotting is accomplished by converting a single coordinate position furnished by


an application program into appropriate operations for the output device in use.

 With a CRT monitor, for example, the electron beam is turned on to illuminate the
screen phosphor at the selected location. How the electron beam is positioned depends
on the display technology.

 A random-scan (vector) system stores point-plotting instructions in the display list, and
coordinate values in these instructions are converted to deflection voltages that position
the electron beam at the screen locations to be plotted during each refresh cycle.

 For a black and-white raster system, on the other hand, a point is plotted by setting the
bit value corresponding to a specified screen position within the frame buffer to 1.

Department of CSE
Subject: Computer Graphics III-II CSE –A1

 With an RGB system, the frame buffer is loaded with the color Points and lines codes
for the intensities that are to be displayed at the screen pixel positions.

Lines:

In mathematics, a line is a one-dimensional figure that is made up of an infinite number of


points that extend in opposite directions. It is determined by two points in a two-dimensional
plane.

In computer graphics, a line is a mark that connects two points, or a mark that extends from a
single point. Lines are a fundamental element of visual art and are used to create structure and
movement in designs.

Line drawing is accomplished by calculating intermediate positions along the line path between
two specified endpoint positions. An output device is then directed to fill in these positions
between the endpoints.

Digital devices display a straight line segment by plotting discrete points between the two
endpoints. Discrete coordinate positions along the line path are calculated from the equation of
the line.

For a raster video display, the line color (intensity) is then loaded into the frame buffer at the
corresponding pixel coordinates.
A computed line position of (10.48, 20.51), for example, would be converted to pixel position
(10,21). Tlus rounding of coordinate values to integers causes lines to be displayed with a
stairstep appearance ("the jaggies"), as represented in Fig 3-1.

To load a specified color into the frame buffer at a position corresponding to column x along
scan line y, we will assume we have available a low-level procedure of the form.

For the raster-graphics device-level algorithms discussed in this chapter, object positions are
specified directly in integer device coordinates. For the time being, we will assume that pixel
positions are referenced according to scan-line number and column number (pixel position
across a scan line). This addressing scheme is illustrated in Fig. 3-2.

Scan lines are numbered consecutively from 0, starting at the bottom of the screen; and pixel
columns are numbered from 0, left to right across each scan line.

Department of CSE
Subject: Computer Graphics III-II CSE –A1

We sometimes will also want to be able to retrieve the current frame-buffer intensity setting
for a specified location. We accomplish this with the low-level function.

Line Drawing Algorithms:


The Cartesian slope-intercept equation for a straight line is

With m representing the slope of the line and b as they intercept. Given that the two endpoints
of a h e segment are specified at positions (x1, y1,) and (x2, y2), as shown in Fig. 3-3, we can
determine values for the slope m and y intercept b with the following calculations

Algorithms for displaying straight lines are based on the line equation 3-1 and the calculations
given in Eqs. 3-2 and 3-3.
For any given x interval ∆x along a line, we can compute the corresponding ∆y interval from
equation 3-2 as

Similarly, we can obtain the x interval ∆x corresponding to a specified ∆y as

These equations form the basis for determining deflection voltages in analog devices.
Raster Systems: On raster systems, lines are plotted with pixels, and step sizes in the
horizontal and vertical directions are constrained by pixel separations. That is, we must

Department of CSE
Subject: Computer Graphics III-II CSE –A1

"sample" a line at discrete positions and determine the nearest pixel to the / line at each sampled
position.

This scan conversion process for straight lines is illustrated, lustrated in Fig. 3-4, for a near
horizontal line with discrete sample positions along the x axis.

DDA Algorithm:
The digital differential analyser (DDA) is a scan-conversion line algorithm based on
calculating either ∆y or ∆x, using Eq. 3-4 or Eq. 3-5. We sample the line at unit in Straight line
segment with intervals in one coordinate and determine corresponding integer values nearest
the five sampling positions along line path for the other coordinate.

Steps of the Algorithm:


1. Calculate the differences in x and y:
Δx=x2−x1, Δy =y2−y1

2. Determine the number of steps needed, based on the greater absolute difference
between Δx and Δy Steps=max(∣Δx∣,∣Δy∣)

3. Calculate the increments for each step: Xincrement=Δx / Steps, Yincrement=Δy/Steps

4. Starting at (x1,y1) iterate through each step and compute the next point by adding the
increments. X= X+ Xincrement ; Y = Y + Yincrement

Department of CSE
Subject: Computer Graphics III-II CSE –A1

This algorithm is summarized in the following procedure, which accepts as input the two end
point. pixel positions. Horizontal and vertical differences between the endpoint positions are
assigned to parameters dx and dy. The difference with the greater magnitude determines the
value of parameter steps. Starting with pixel position (xa, ya), we determine the offset needed
at each step to generate the next pixel position along the line path. We loop through this process
steps times.

Advantages of DDA algorithm


1. The DDA algorithm is a faster method for calculating pixel positions than the direct use
of Eq. 3-1.
2. It eliminates the multiplication in Eq. 3-1 by making use of raster characteristics, so
that appropriate increments are applied in the x or y direction
Disadvantage of DDA
1. The rounding operations and floating-point arithmetic in procedure line DDA are still
time-consuming.
Example: Draw a line from point P1(2,3) to P2(10,8) using the DDA algorithm.
Python Code Example:
def dda_line(x1, y1, x2, y2):
# Calculate differences
dx = x2 - x1
dy = y2 - y1

# Determine the number of steps


steps = max(abs(dx), abs(dy))

# Calculate increments
x_increment = dx / steps
y_increment = dy / steps

# Initialize starting point


x = x1
y = y1

# Draw the line


print("Points on the line:")
for i_ in range(steps + 1): # +1 to include the endpoint
print(f"({round(x)}, {round(y)})") # Rounding for integer pixel values
x += x_increment
y += y_increment
Department of CSE
Subject: Computer Graphics III-II CSE –A1

# Example usage
dda_line(2, 3, 10, 8)
Output:
The output will list the points that form the line:Points on the line:
(2, 3)
(3, 3)
(4, 4)
(5, 4)
(6, 5)
(7, 5)
(8, 6)
(9, 7)
(10, 8)

1. Δx=x2−x1, Δy =y2−y1
=8 =5
2. Steps=max(∣Δx∣,∣Δy∣)
Steps = max(8,5) = 8
3. Xincrement=Δx / Steps, Yincrement=Δy/Steps
= 8/8 =1 = 5/8 = 0.62
4. for first iteration X = 2+1 Y= 3+0.62
For second iteration X= 3+1 Y= 4.02 + 0.62
…..
After 8 iterations X=10 , Y=8.62

Students can also refer the following link for additional information:
https://www.youtube.com/watch?v=EWOlUxvVtNg

Brasenham’s Line Algorithm:


An accurate and efficient raster line-generating algorithm, developed by Bresenham, scan
converts lines using only incrementa1 integer calculations that can be adapted to display circles
and other curves.
Figures 3-5 and 3-6 illustrate sections of a display screen where straight line segments are to
be drawn. The vertical axes show-scan-line positions, and the horizontal axes identify pixel
columns. Sampling at unit x intervals in these examples, we need to decide which of two
possible pixel positions is closer to the line path at each sample step.

Department of CSE
Subject: Computer Graphics III-II CSE –A1

The decision parameters required to calculate the discrete points on the line to draw are
computed as follows

Department of CSE
Subject: Computer Graphics III-II CSE –A1

Brenham’s algorithm is outlined in the following steps

Department of CSE
Subject: Computer Graphics III-II CSE –A1

2.3

Students can also refer the following link

Reference: https://www.youtube.com/watch?v=h3gDB89h0os

Department of CSE
Subject: Computer Graphics III-II CSE –A1

CIRCLE-GENERATING ALGORITHM
Properties of Circles A circle is defined as the set of points that are all at a given distance r
from a center position (x,, y,) (Fig. 3-12). This distance relationship is expressed by the
Pythagorean theorem in Cartesian coordinates as

Computation can be reduced by considering the symmetry of circles. The shape of the circle is
similar in each quadrant. Circle sections in adjacent octants within one quadrant are symmetric
with respect to the 45o line dividing the two octants. These symmetry conditions are illustrated
in Fig.3-14, where a point at position (x, y) on a one-eighth circle sector is mapped into the
seven circle points in the other octants of the xy plane.

Department of CSE
Subject: Computer Graphics III-II CSE –A1

To apply the midpoint method, we define a circle function:

Department of CSE
Subject: Computer Graphics III-II CSE –A1

Department of CSE
Subject: Computer Graphics III-II CSE –A1

Department of CSE
Subject: Computer Graphics III-II CSE –A1

Students can also refer for additional problem the following link:
https://www.youtube.com/watch?v=LduamxjhD6E

Department of CSE
Subject: Computer Graphics III-II CSE –A1

2.4 ELLIPSE-GENERATING ALGORITHMS

An ellipse is defined as the set of points such that the sum of the distances from two fixed
positions (foci) is the same for all points (Fig. 3-17). If the distances to the two foci from any
point P = (x, y) on the ellipse are labeled d1 and d2, then the general equation of an ellipse can
be stated as
d1 + d2 = constant

Midpoint Ellipse Algorithm

Department of CSE
Subject: Computer Graphics III-II CSE –A1

we select the next pixel along the ellipse according to the sign of the ellipse function evaluated
at the midpoint between the two candidate pixels.
Starting at (0, r,), we take unit steps in the x direction until we reach the boundary between
region 1 and region 2-(Fig. 3-20).

Department of CSE
Subject: Computer Graphics III-II CSE –A1

Department of CSE
Subject: Computer Graphics III-II CSE –A1

Department of CSE
Subject: Computer Graphics III-II CSE –A1

Department of CSE
Subject: Computer Graphics III-II CSE –A1

Department of CSE
Subject: Computer Graphics III-II CSE –A1

Students can also refer the following link for additional example
https://www.youtube.com/watch?v=KbDN4WOZ3f8

Department of CSE

You might also like