0% found this document useful (0 votes)
50 views61 pages

CG Chapter No 2

This document discusses output primitives and line drawing algorithms in computer graphics. It introduces points and lines as the simplest geometric components used to construct pictures. The digital differential analyzer (DDA) algorithm is described for scan converting lines based on calculating incremental changes in x or y coordinates at each step. An example demonstrates applying the DDA algorithm to draw a line between two points by calculating the slope and incremental x or y values over 7 steps.

Uploaded by

Purva Jage
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views61 pages

CG Chapter No 2

This document discusses output primitives and line drawing algorithms in computer graphics. It introduces points and lines as the simplest geometric components used to construct pictures. The digital differential analyzer (DDA) algorithm is described for scan converting lines based on calculating incremental changes in x or y coordinates at each step. An example demonstrates applying the DDA algorithm to draw a line between two points by calculating the slope and incremental x or y values over 7 steps.

Uploaded by

Purva Jage
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 61

ChapterNo2

Output Primitives

Computer Graphics(CSE305) 1
Introduction
⮚ Shapes and colors of the objects can be described internally with pixel arraysorwithsetsof basic
geometric structures, such as straight line segments and polygoncolor areas

⮚ The picture is then displayed either by loading the pixel arrays into the framebufferorbyscan
converting the basic geometric-structure specifications into pixel patterns.

⮚ Graphics programming packages provide functions to describe a scene intermsofthesebasic


geometric structures, referred to as output primitives

⮚ Each output primitive is specified with input coordinate data and other informationaboutthe
way that object is to be displayed.

⮚ Points and straight line segments are the simplest geometric components of pictures.

⮚ Additional output primitives that can be used to construct a picture includecirclesandother


conic sections, quadric surfaces, spline curves and surfaces,
polygoncolorareas,andcharacter strings

Computer Graphics(CSE305) 2

Mario Picture inpixel form


Computer

Graphics(CSE305) 3

POINTS ANDLINES
⮚ With a CRT monitor, the electron beam is turned on to illuminatethescreenphosphor
at the selected location.
⮚ For A random-scan (vector) system ⮚ stores point-plotting instructions in the display
list file andcoordinatevalues in these instructions

⮚ Then converted to deflection voltages that position the electronbeamatthe screen


locations to be plotted during each refresh cycle.

⮚ A random-scan display, a straight line can be drawn smoothlyfromoneendpoint


to the other.

⮚ With an RGB system

⮚ The frame buffer is loaded with the color codes for the intensitiesthatareto be
displayed at the screen pixel positions

Computer Graphics(CSE305) 4

POINTS ANDLINES
⮚ For a black and-white raster system ⮚ A point is
plotted by setting the bit value
corresponding to a specified screen position
within the frame buffer to 1.

⮚ the electron beam sweeps across each


horizontal scan line,

⮚ it emits a burst of electrons (plots a point) whenever avalueof1is


encounter in the

⮚ Thus rounding of coordinate values to integers causes lines


frame buffer.

tobedisplayed
with a stairstep appearance ("the jaggies"), ⮚ noticeable on systems

⮚ for smoothing raster lines are based on adjusting pixel


with low resolution
intensitiesalongthe
line paths. Computer Graphics(CSE305) 5

Setting up pixel position

⮚ We can
set and get the pixel position with the help of followingcommands:⮚ setpixel (x,y)

⮚ getPixel (x,y)
Computer Graphics(CSE305) 6

LINE-DRAWINGALGORITHMS
⮚ The Cartesian slope-intercept equation for a
straight line is y= �. � + �
--------- Eq 3.1
⮚ Where �= slope of the line
� = y intercept

⮚ we can determine values for the slope m and y


intercept b with the
following
calculations:

-----------Eq 3.2

------Eq 3.3
⮚ For any given x interval Δx along a line, we can compute the correspondingyinterval
Δy from eq 3.2 and Vise versa ,
Δy= m * Δx ----Eq 3.4
and Δx = Δy / m -----Eq 3.5
Computer Graphics(CSE305) 7

For different values of slopmCase I: For |


m| < 1
Case II : For | m| > 1
Case III: For |m|==1

▪Δxcanbeset

▪Δycanbeset

▪ For lines withm=1,Δx=


proportional to a small
proportional to a small
Δy andthehorizontal
horizontal deflection
v e rti c a l d e f l e c ti o n
and vertical deflections
voltageandthe
voltagewiththe
voltages areequal.
corresponding vertical
corresponding horizontal
deflection is then set
deflection voltage set
proportional to Δy
proportional to Δx


Δy
Δy
Δy
Δx Δx Δx

Computer Graphics(CSE305) 8

DDAAlgorithm
⮚ The digital deferential analyzer (DDA) is a scan-
conversionlinealgorithmbased on Calculating either Δy or Δx

⮚ We sample the line at unit intervals in one coordinate anddeterminecorresponding


integer values nearest the line path for the other coordinate.
⮚ If the slope is less than or equal to 1, we sample at unit xintervals(Δx=1)and
compute each successive Δ y value as
��+1 = �� + �----------eq3.6

⮚ Where ��+1= next point and ��= Old /Prev. point

⮚ For lines with a positive slope greater than 1, we reverse therolesofxandy.That


is, we sample at unit Δy intervals (Δy = 1) andcalculateeachsucceeding x
value as
��+1 = �� +1�--------eq3.7
Computer Graphics(CSE305) 9

DDAAlgorithm
⮚ Last equations are based on the assumption that lines are
tobeprocessedfromthe left endpoint to the right endpoint
⮚ If this processing is reversed, so that the starting endpoint is at
theright,theneither we have Δ x = -1 and
⮚ ��+1 = �� − �⮚ Where ��+1= next point and ��= Old /Prev. point ⮚

1
(when the slope is greater than I) we have Δy = -1 with ��+1 = �� − �

Computer Graphics(CSE305) 10

Example
⮚ Consider Line points are A ( 2 , 2 )
and B ( 9 , 2 ) �1 , �1
�2, �2
⮚ Now calculate Δx and Δy:
Δ x= �2 - �1 Δ y = �2- �1
=9–2=7=2–2=0

⮚ Now calculate slop (m)

m=Δ y
0
Δ x = 7= 0

⮚ Now decide the steps = Highest value of Δx and Δy ⮚ Here Δy= 7 > Δx value
therefore Step = 7

⮚ Calculate ���� , ����:

����� = 1 ���� =Δy


���� =Δx
����� = 0

Computer Graphics(CSE305) 11
Example
X value2 Y value

3 2

4 2

5 2

6 2

7 2

8 2

9 2

StartingpointcoordinatevaluesA( 2, 2)
Computer Graphics(CSE305) 12

Grid view
Y axis

10
9
8
7
6
5
4
3
2
1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
xaxis
Computer Graphics(CSE305) 13

DDAAlgosteps
• DDAAlgo accepts as input the two end point pixel positions. • Horizontal and vertical
differences between the endpoint positions areassignedtoparameters dx and dy.
• The difference with the greater magnitude determines the value of parameter steps. • Starting
with pixel position (xa , yb), we determine the offset neededat eachsteptogenerate the next pixel
position along the line path.
• We loop through this process steps times.
• If the magnitude of dx is greater than the magnitude of dy and xa is lessthanxb,thevalues of the
increments in the x and y directions are 1 and m, respectively. • If the greater change is in the x
direction, but xa is greater than xb, then thedecrements-1and -m are used to generate each new
point on the line.
• Otherwise, we use a unit increment (or decrement) in the y direction andanxincrement(or
decrement) of l/m
Computer Graphics(CSE305) 14
DDAAlgo Program

Computer
Graphics(CSE305) 15
Advantages
⮚It is a faster method than method of using direct
useoflineequation.

⮚This method does not use multiplication theorem.

⮚It allows us to detect the change in the valueof xandy,soplotting


of same point twice is not possible.

⮚This method gives overflow indication


whenapointisrepositioned.
⮚It is an easy method because each step involves justtwoadditions.

Computer Graphics(CSE305) 16

Disadvantages ⮚ It involves floating

point additions rounding off is done. ⮚ Accumulations of round off error cause

accumulation of error.

⮚ Rounding off operations and floating point operations consumesalotoftime.

⮚ It is more suitable for generating line using the software. ⮚ But it is

less suited for hardware implementation.


Computer Graphics(CSE305) 17

BresenhamAlgorithm
⮚ An accurate and efficient raster line-generating algorithm,
developedbyBresenham

⮚ In Dispaly Screen Coordinate system ,the vertical axis show-scan-linepositions,


and the horizontal axis identify pixel columns.

⮚ Sampling at unit x intervals in these examples, we needtodecidewhichoftwo


possible pixel positions is closer to the line path at eachsamplestep

⮚ For lines with positive slope less than 1 (m<1) : ⮚ Pixel positions along a line
path are then determinedbysamplingatunitx intervals.

⮚ Starting from the left endpoint (x0, y0) of a given line, ⮚ we step to each
successive column (x position) and plot thepixelwhosescan-line y value is
closest to the line path.
Computer Graphics(CSE305) 18

Pixel positionandlines Computer Graphics(CSE305) 19

Pixel positionandlines
yk+1

yk

xk xk+1

Computer Graphics(CSE305) 20
Computer Graphics(CSE305) 20

display line onscreen


BresenhamAlgorithm
⮚ Assuming we have determined that the pixel
at (xk, yk) is to be displayed,

⮚ we next need to decide which pixel to plot in


column xk+1

⮚ Our choices are the pixels at positions (xk


+1,yk ) and (xk+1,yk+1).

BresenhamAlgorithm
⮚ At sampling position xk+l , we label
vertical pixel separations from the
mathematical line path as d1, and d2
(Fig. 3-8).
⮚ The y coordinate on the mathematical line at pixel column position
xk+l is calculated as

� = �. (�� + �) + �

⮚ Then we calculate the  d1, and d2


�� = � − ��
After putting the value of � ,
               �� = �. (�� + �) +� −��
Computer Graphics(CSE305) 23

BresenhamAlgorithm
⮚ And d2 we can calculate as ,
�� = (�� + �) − �
⮚ After putting value of y, We get:
�� = (�� + �) − �. (�� +�) −�

⮚ The difference between these two separations is

�� − �� = �. (�� + �) + � − �� − {(� � +�) −�. (��+�)


−�}= + + � − �� − �� − � +++�= ��. �� + �� + �� −
��� − � �� − �� = ��(�� + �) + �� − ��� − �

⮚ m is nothing but slop and it gives us float value so


eliminatemfromaboveequation
Computer Graphics(CSE305) 24

CalculatePk
⮚ by substituting m = AyIAx, where Ay and Ax are the vertical andhorizontal
separations of the endpoint positions, and defining:
⮚ At step k + 1, the decision parameter is evaluated from

Computer Graphics(CSE305) 25

BresenhamAlgorithm
⮚ Substract the equation 4 from 3 to calculate decision parameterforallpixel,
pk + 1
- pk= 2Δ� . �� + 1 − 2Δ �. �� + 1
-2� . ��+2�. ��
pk + 1
- pk= 2Δ� (�� + 1 − �� ) − 2Δ �. (�� + 1 − ��)

⮚ Where �� + 1 = xK + 1

is either 0 or 1, depending on the signof parameter


⮚ where the term yk+1 - yk pk. ⮚ The
at thestartingpixel
first parameter, p0, is evaluated from Eq of PK position (xo, yo) and

with m evaluated as Δy/Δx:


Computer Graphics(CSE305) 26

BresenhamAlgorithm
Computer

Graphics(CSE305) 27

Mid-Point Algofor circle


⮚ Properties of Circles : 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)

⮚ Pythagorean theorem in Cartesian coordinates as, � 


(� − ��)� + (� − ��)� = �

⮚ calculating the corresponding y values at each position

 
as, � = �� + �� − (�

−��)�

⮚ One problem with this approach is that it


involves considerable computation at each step.

⮚ the spacing between plotted pixel positions is not


uniform⮚ Above method involves multiplications and square root calculations
Computer Graphics(CSE305) 28

Mid-Point Algofor circle


⮚ Another way to eliminate the unequal spacing shown inFig. 3-13istocalculate
points along the circular boundary using polar coordinatesrandѲ
⮚ Computation can be reduced by considering the symmetryof circles. ⮚ The shape
of the circle is similar in each quadrant.
Computer Graphics(CSE305) 29

Midpoint Circle Algorithm


⮚ we sample at unit intervals and determine the closest pixel
positiontothespecified circle path at each step.

⮚ use a decision parameter to determine which of the two possibleypositionsis


closer to the circle path at each step.
⮚ If the point is in the interior of the circle, The circle

function is negative.
⮚ if the point is outside the circle, the circle function is positive.
Computer Graphics(CSE305) 30

Midpoint Circle Algorithm


⮚ Thus circle function is the decision parameter in the midpoint algorithm,and
we can set up incremental calculations for this functionaswedidinthe line
algorithm.

⮚ we have just plotted the pixel at (xk, yk),

⮚ we next needs to determine whether the pixel at position(xk +1, yk)ortheone at


position (xk + 1, yk - 1) is closer to the circle.

⮚ We have to calculate mid point

⮚ Midpoint: { (xk + 1
)+(xk + 1)
2,yk+ (yk − 1)
, yk- 12)
2} = (xk + 1

⮚ Our decision parameter is the circle function 3-27 evaluatedat


themidpointbetween these two pixels:
�� � � 
�� = (�� + �) + (�� − �) −�
Computer Graphics(CSE305) 31

Midpoint Circle Algorithm⮚ If p < 0, k

is
this midpoirat is inside the circle and the pixel onscanlineyk closer to the circle
boundary.

⮚ Otherwise, the midposition is outside or on the circle boundary, andweselect the


pixel on scan line yk - 1.
⮚ We can obtain a recursive expression for the next
decisionparameterbyevaluating the circle function at sampling position x k+1
+1=xk +2.

⮚ From Pk equation to find pK+1


� � � � 
��+� = (�� + � + �) + (�� + � − �) −�
 
⮚ Difference in decision parameter can be calculated as � } -{(�� +�)� +(��
 
−��)�−�� }

��+� − �� = {(�� + � + �)� + (�� + � −��)� − � ⮚ After simplifying


the above requation ,
Computer Graphics(CSE305) 32

Midpoint Circle Algorithm


��+� = �� + (�� + �) + {(�� + �)� − (��)�}-(�� + � −��) +1⮚ At
each xk position, starting at k = 0, performthe followingtest: Test1:
⮚ If pk ˂ 0, the next point along the circle centered on (0,0) is (xk+1, yk)and��+�
= �� + ��� + �
Test 2
⮚ Otherwise , the next point along the circle is (xk + 1
, yk - 1) and
��+� = �� + ��� + � + � − ��� + � where 2xk+1,= 2xk + 2 and 2yk+1,=
2yk- 2 ⮚ The initial decision parameter is obtained by evaluating the
circlefunctionat the start position (x0, y0) = (0, r ): put xk=0 and yk=r �
� � 
� ) −�
�� = (� + �)� + (� −

�  �
� − � = � −�

� 
�� = � + � − � +
Computer Graphics(CSE305) 33

Steps for Midpoint CircleAlgorithm


Computer Graphics(CSE305) 34

Steps for Midpoint EllipseAlgorithm


Computer

Graphics(CSE305) 35

Steps for Midpoint EllipseAlgorithm


Computer

Graphics(CSE305) 36

Antialiasing
⮚ Definition:
“Displayed primitives generated by the raster algorithms have a jagged, orstairstep,
appearance because the sampling process digitizes coordinatepintsonanobject to discrete
integer pixel positions. This distortion of informationduetolow frequency sampling
(undersampling) is called aliasing.”

⮚ Antialiasing is a technique used in computer


graphics to remove the aliasing effect.

⮚ Undersampling occurs when sampling is done


at a frequency lower than Nyquist sampling
frequency.

⮚ This minimum required frequency is referred to as Nyquist samplingfrequency(fs):fs


=2*fmax

Computer Graphics(CSE305) 37
Methods of Antialiasing(AA)
⮚ Four methods:
1. Using high-resolution display,
2. Post filtering (Supersampling),
3. Pre-filtering (Area Sampling),
4. Pixel phasing.

⮚ Post filtering (Supersampling),

⮚ we are increasing the sampling resolution by treating the screenasif it’smadeof a much

more fine grid, due to which the effective pixel size is reduced.⮚ Now, intensity from each
subpixel is calculated and average intensityofthepixel is found from the average of
intensities of subpixels.

⮚ Thus we do sampling at higher resolution and display the imageatlowerresolution or


resolution of the screen, hence this techniqueiscalledsupersampling.

⮚ This method is also known as post filtration as this procedureisdoneaftergenerating


the rasterized image.

Computer Graphics(CSE305) 38

Methods of Antialiasing(AA)
Computer Graphics(CSE305) 39

Methods of Antialiasing(AA) ⮚ Pre-


filtering (Area Sampling):
⮚ In area sampling, pixel intensities are calculated proportional to areas of overlapofeach pixel
with objects to be displayed.
⮚ Here pixel color is computed based on the overlap of scene’s objectswithapixelarea.

⮚ For example: Suppose, a line passes through two pixels. The pixel
coveringbiggerportion(90%) of line displays 90% intensity while less area(10%)
coveringpixeldisplays 10-15% intensity.

⮚ If pixel area overlaps with different color areas, then the final pixel color istakenasan average
of colors of the overlap area.

⮚ This method is also known as pre-filtering as this procedure is doneBEFOREgenerating


the rasterized image.

⮚ It’s done using some graphics primitive algorithms.


Computer Graphics(CSE305) 40

Methods of Antialiasing(AA)
Comput

er Graphics(CSE305) 41

Methods of Antialiasing(AA)
⮚ Pixel phasing:
⮚ It’s a technique to remove aliasing.

⮚ Here pixel positions are shifted to nearly approximate positionsnearobject


geometry.

⮚ Some systems allow the size of individual pixels tobeadjustedfordistributing


intensities which is helpful in pixel phasing.

Computer Graphics(CSE305) 42

FILLED-AREAPRIMITIVES
⮚ Area filling is a method or process that helps us to fill anobject, area,orimage.
We can easily fill the polygon.

⮚ A polygon can be defined as a shape that is formed by linesegmentsthatare


placed end to end, creating a continuous closed path.

⮚ two basic approaches to area filling:


1. by determine overlap intervals of scan line 2. start from given
interior postion and filling outword

⮚ Algorithms
1. Scan Line Polygon Fill Algorithm 2. Boundary-Fill Algorithm 3. Flood-
Fill Algorithm

Computer Graphics(CSE305) 43
1. Scan Line PolygonFill Algorithm
⮚ For each scan line crossing a polygon, the area-fill
algorithmlocatestheintersection points of the scan line with the polygon
edges.

⮚ These intersection points are then sorted fromleft to right

⮚ The corresponding frame-buffer positions between eachintersectionpairare


set to the specified fill color.
Frame buffer:
10 14 18 24

Computer Graphics(CSE305) 44

1. Scan Line PolygonFill Algorithm⮚


Some scan-line intersections at polygon vertices require special handling.

⮚ A scan line passing through a vertex intersects two edges at that position,adding
two points to the list of intersections for the scan line.

⮚ For scan line y, the two intersecting edges sharing a


vertexareonoppositesides of the scan line.

⮚ But for scan line y'


, the two intersecting edges are both abovethescanline.

⮚ If the endpoint y values of two consecutive edges monotonicallyincreaseor


decrease, we need to count the middle vertex as a singleintersectionpoint for
any scan line passing through that vertex.

⮚ Otherwise, two edge intersections


Computer Graphics(CSE305) 45

1. Scan Line PolygonFill Algorithm


Computer
Graphics(CSE305) 46

1. Scan Line PolygonFill Algorithm


⮚ One way to resolve the question as to whether we shouldcount avertexasone
intersection or two is:
⮚ to shorten some polygon edges to split those vertices that shouldbecounted as one
intersection.

Computer

Graphics(CSE305) 47

2.Boundary-Fill Algorithm
⮚ Another approach to area filling is to start at a point insidearegionandpaint
the interior outward toward the boundary.
⮚ If the boundary is specified in a single color, the fill
algorithmproceedsoutward pixel by pixel until the boundary color is
encountered.

⮚ A boundary-fill procedure accepts as input the coordinates of aninteriorpoint (x,


y), a fill color, and a boundary color.

⮚ Starting from (x, y), the procedure tests neighboring positions


todeterminewhether they are of the boundary color.

⮚ If not, they are painted with the fill color,

Computer Graphics(CSE305) 48

2.Boundary-Fill Algorithm
⮚ two methods for proceeding to neighboring pixels fromthecurrenttestposition.

1. 4-Connected. 2. 8 connected Computer


Graphics(CSE305) 49

For 8 connectedpixel
Computer Graphics(CSE305) 50

2.Boundary-Fill Algorithm
Computer
Graphics(CSE305) 51

3.Flood-Fill Algorithm
⮚ Sometimes we want to fill in (or recolor) an area that is not definedwithin a single
color boundary.

⮚ We can paint such areas by replacing a specified interior color insteadofsearching


for a boundary color value.

⮚ This approach is called a flood-fill algorithm.

Comp

uter Graphics(CSE305) 52

Inside-OutsideTests
⮚ Area-filling algorithms and other graphics processes
oftenneedtoidentifyinterior regions of objects
⮚ two methods are used
1. Odd-even rule, also called the odd parity rule or the evenoddrule• If the
number of polygon edges crossed by this lineisodd, thenPis an interior point.
Otherwise, P is an exterior point.

2. Nonzero winding number rule


• which.counts the number of times the polygon edges
windaroundaparticular point in the counterclockwise direction.
• This count is called the winding number
• Each intersected Edge goes
– Right ----> Left && Top -----> Bottomthenconsider+1– Left ---->
Right && Bottum ----> top thenconsider-1Computer Graphics(CSE305) 53

Inside-OutsideTests
Computer
Graphics(CSE305) 54

You might also like