SlideShare a Scribd company logo
1 of 85
GRAPHICS
Basic raster graphics algorithms
Point and Lines
• Point plotting is accomplished by converting a
single coordinate position furnished by an
application program into appropriate
operations for the output device in use.
• Line drawing is accomplished by calculating
intermediate positions along the line path
between two specified endpoint positions.
• An output device is directed to fill in these
positions between the endpoints.
Plotting with Analog devices
• For analog devices or a random scan display, a
straight line can be drawn smoothly from one
endpoint to the other .
• Linearly varying horizontal and vertical
deflection voltages are generated that are
proportional to the required changes in the x
and y directions to produce a smooth line.
Plotting with digital device
• Digital devices display a straight line segment by
plotting discrete points between the two end
points.
• Discrete coordinate positions along the line path
are calculated from the equation of line .
• For a raster video display the line color is then
loaded into frame buffer at the corresponding
pixel coordinates.
• Reading from the frame buffer the video
controller the plots the screen pixels.
Since screen locations are
referenced with integer values ,
so plotted positions are only the
approximation of the actual line
path between two given points
• A computed line position of 10.48,20.51
would be converted to 10,21.
• This rounding of coordinate values to integers
causes lines to be displayed with a staircase
appearance.
• The characteristic staircase shape is
particularly noticeable on systems with low
resolution, so their appearance can be
improved by somewhat displaying tem on
higher resolution system
• Techniques for smoothing lines can be used
called antialising
Basic incremental algorithm
• The equation of a line in cartesian coordinates is
• y = m.x + b………………1
where m is slope and c as the y intercept.
Where m = ∆y/∆x…………………2
or ∆y = m . ∆x…………………3
For any given x interval ∆x we can
compute the corresponding y interval ∆y
Similarly we can obtain the x interval ∆x corresponding to
specified ∆y as
m = ∆x/∆y………………..4
• Thus we sample line at discrete points and determine the
nearest pixel to the line at each sampled position.
Incremental / DDA algorithm
• For lines with slope magnitudes |m| < 1 , ∆x
can be set to 1 , we sample at unit x intervals
and compute each successive value as
• yi+1 = m. xi+1 + b = m(xi +∆x) + b
= m.xi + b + m. ∆x
= yi + m.∆x ..since ∆x = 1 , we have
• yi+1 = yi + m…………………….5
• For lines with slope greater than 1 we reverse
the role of x and y .thus we sample at unit y
intervals and calculate the succeeding x value
• Xi+1 = xi + 1/m………………6
• Equations 5 and 6 are based on the
assumption that lines are to be processed
from left to right endpoint . If this processing
is reversed so that the endpoint is at the right
then we have ∆x = -1 and
• Yi+1 = yi – m
• Xi=1 = xi – 1/m
DDA : Algorithm
• Step 1: Get the input of two end points (X0, Y0) and (X1, Y1).
• Step 2: Calculate the difference between two end points.
dx = X1 - X0 ,dy = Y1 - Y0
• Step 3: Based on the calculated difference in step-2, you
need to identify the number of steps to put pixel. If dx > dy,
then you need more steps in x coordinate; otherwise in y
coordinate.
if(absolute(dx) > absolute(dy))
Steps = absolute(dx); else
Steps = absolute(dy);
Step 4: Calculate the increment in x and y coordinate.
Xincrement = dx / (float) steps;
Yincrement = dy / (float) steps;
Step 5: Put the pixel by successfully incrementing x and
y coordinates accordingly and complete the drawing of
the line.
for(int v=0; v < Steps; v++)
{
x = x + Xincrement;
y = y + Yincrement;
putpixel(Round(x), Round(y));
}
DDA
• Drawbacks
• round off and floating point procedures are
time consuming.
• Accumulation of round off in successive
additions of the floating point increment
causes pixel positions to drift away from true
line path for long lines.
Improvement : Bresenham’s Line algorithm
Bresenham’s line algorithm
• The drawback of DDA algorithm is that
rounding operations and floating point
arithmetic is time consuming.
• An accurate and efficient raster line
generating algorithm developed by
bresenham scan converts line using only
incremental calculations.
• Sampling at unit x intervals we need to decide
which of the two possible pixel positions is
closer to the line path at each sample step
13
12
11
10
10 11 12 13
Need to decide Whether to plot pixel 11,11 or 11,12
in bresenham’s approach we plot the pixel whose
scan line y value is closest to the line path.
Midpoint line algorithm
• We assume that the line’s slope is between 0
and 1
• Lower left end point is x0,y0 and the upper
end right endpoint x1,y1.
• Assuming that we have just selectedd the pixel
at P (xp,yp) and now we have to decide
whether to plot pixel to the right(called the
east pixel ,E)or the pixel one increment to the
right and one increment up (called the
northeast pixel , NE)
NE
M
E
P= (xp,yp)
•We observe on which side of the line the midpoint
M lies
•If the midpoint lies above the line pixel E is chosen
•If the midpoint lies below the line NE is closer to
line
Now we need to determine which side the
midpoint lies
• Let we represent the line by an function having
coefficients a ,b and c
• F(x,y) = ax + by + c= 0
• If dy = y1-y0 and dx = x1-x0 then
• Y = dy/dx x + B therefore
• F(x,y) = dy.x – dx.y +B.dx =0 where
• A = dy , b = -dx and c =B.dx
• If a point is on the line it satisfies the first equation
and F(x,y) = 0, positive for points below the line and
negative for points above the line
• To apply the midpoint criterion we need only to
compute F(M) = F(xp+1,yp+1/2)
Mid point Algorithm for line
• Since our decision is based on the value of function
at xp+1 ,yp+ ½ we define a dexision variable d =
F(xp+1,yp+1/2)
• If d>0 we choose pixel NE
• If d<0 we choose pixel E
• If d=0 we choose either ,let we pick E
• How we calculate M for next grid line which
depends whether we choose E or NE
• If E is choosen M is incremented one step in the x
direction
• dnew = F(xp+2,yp+1/2)=a(xp+2) +b(yp+1/2) =c
• dold = a(xp+1) + b(yp+1/2)+c
• Subtracting dold from dnew
• dnew = dold +a, let ∆E = a =dy.
• We can derive the value of the decision variable at
the next step incrementally from the value at the
current step merely by adding dy or ∆E.
• If NE is chosen ,M is incremented one step in both x
and y direction thus :
• dnew = F(xp+1/2,yp+3/2)=a(xp+2)+b(yp+3/2)+c
• Subtracting dold from dnew for NE
• dnew=dold+a +b,let we call the increment to add d
after NE as ∆NE= a+b = dy-dx
• Since the first pixel is simply (x0,y0) so the first
midpoint
• F(x0+1,y0+1/2)=a(x0+1) + b(y0+1/2)+c
• =ax0=by0+c+a+b/2
• =F(x0,y0)+a+b/2
• Since initial point lies on line so F(x0,y0)=0.
• dstart= a+b/2 = dy - dx/2
• To eliminate fraction in dstart redefine the original F
by multiplying it by 2
• F(x,y)=2(ax+by+c) which multiplies each constant
and decision variable by 2 but does not affect the
sign of decision variable
So in incremental midpoint
technique the algorithm chooses
between 2 pixels based on the
sign of the decision variable by
adding either ∆E or ∆NE to the old
value depending on the choice of
pixel
Midpoint circle Algorithm
• The equation of a circle centered at originis
• X2 +y2 = r2 solving it for y we get
• Y = +-√r2 - x2
• To draw a quarter circle, we can increment x
from 0 to r in unit steps solving for +y at each
step.
• This approach works but it is inefficient
because of multiply and square root
operations
Furthermore the circle will have large gaps for values of x
close to r, because the slope of the circle becomes infinite
there.
We can avoid the large gaps
, or can have uniform plotting
if we plot (rcosѲ,rsinѲ)
(by stepping Ѳ from
0 degree to 90 degree)
in angular co-ordinates
Drawing process can be improved by taking advantage of
circle symmetry if a point is on the circle than we can
compute seven other points on it
(
(y,x)
(x,y)
(-x,y)
(-y,x)
(-y,-x) (y,-x)
(-x,-y) (x,-y)
Void circlepoints(int x,int y, int value)
{ writepixel(x,y,value);
writepixel(y,x,value);
writepixel(y,-x,value);
writepixel(x,-y,value);
writepixel(-x,-y,value);
writepixel(-y,-x,value);
writepixel(-y,x,value);
writepixel(-x,y,value);
}
Midpoint circle algorithm
• Bresenham developed an incremental circle
generator that generates all points on a circle
centered at origin by incrementing all the way
round the circle.
• We consider only 45 degree of a circle from x=0 to
x= y and use the symmetry of the circle to display
points on the circle.
• If pixel P has already been chosen the next pixel is
plotted by evaluating which out of E and SE is closer
to the circle to the circle by using a function at the
midpoint between the two pixel
M
M.E
M.SE
E
SE
Let F(x,y) = x2 +y2-r2
• This function is 0 on the circle
• Negative inside the circle
• Positive outside the circle
• Therefore if midpoint between pixel E and SE
is outside the circle then pixel SE is closer to
the circle.
• If midpoint is inside the circle pixel E is closer.
Thus if decision parameter is
negative  we plot pixel E
positive we plot pixel SE
• dold = F(xp+1,Yp-1/2)
=(xp+1)2 + (yp-1/2)2 –r2……….1.
• If dold <0, E is chosen and the next midpoint will
be one incremental over x.
• dnew =F(xp+2,yp-1/2)=(xp+2)2+yp-1/2)2-r2…..2
• Subtracting equation 2 from equation 1
• dnew = dold +(2xp+3)
• ∆E = 2xp+3
• If dold >=0,SE is chosen and the next midpoint
will be one increment over x and one decrement
in y
• Dnew = F(xp+2,yp-3/2)
=(xp+2)2 + (yp-3/2)2 –r2
• Dnew = dold + 2xp -2yp +E
• ∆SE = 2xp+(-2yp) +5
• Since these functions are expressed in terms
of (xp,yp) we call p the point of evaluation
Initial conditions for decision
parameter
• The starting pixel lies on the circle at (0,r)
• The next midpoint lies at (1,r-1/2)
• Thus F(1,r-1/2)=1+(r2-2+1/4)-r2
• = 5/4 –r
• Problem : we are forced to do the real
arithmetic because of the fractional
initialization of d.
Solution
• h = d - ¼, so substitute d = h + ¼
so
• h = 1 - R
d < 0 means that h < ¼, but since h treated
as an integer, h < 0 ,call h by d.
C code with integer calculations
• Circle(int R){
• int x,y, d;
• x = 0;
• y = R;
• d = 1 - R;
• Do8Ways(x,y);
• while(y > x){
• if(d < 0){
• d += (2*x + 3);
• ++x;
• }
• else{
• d += (2*(x-y) + 5);
• ++x;
• --y;
• }
• Do8Ways(x,y);
SECOND ORDER DIFFRENCIATION
• improve performance by computing linear equations for DE & DSE
incrementally, as we did for dnew.
calculate first & second-order differences
if choose E (xp yp) -> (xp + 1, yp)
• DEold at (xp, yp) = 2xp + 3
• DEnew at (xp + 1, yp) = 2(xp + 1) + 3
• DEnew - DEold = 2
&
• DSEold at (xp, yp) = 2xp - 2yp + 5
• DSEnew at (xp + 1, yp) = 2(xp + 1) - 2yp + 5
• DSEnew - DSEold = 2
• if choose SE (xp yp) -> (xp + 1, yp - 1)
• DEnew at (xp + 1, yp - 1) = 2(xp + 1) + 3
• DEnew - DEold = 2
• &
• DSEnew at (xp + 1, yp - 1) = 2(xp + 1) - 2(yp -1) + 5
• DSEnew - DSEold = 4
So 1. Choose pixel based on d
2. Update d by DE or DSE
3. Update DE & DSE
Initialize DE & DSE using (0, R)
C Code (with second order differences).
• Circle(int R){
• int x,y, d, dE, dSE;
• x = 0; y = R; d = 1 - R;
• dE = 3;
• dSE = -2*R + 5;
• Do8Ways(x,y);
• while(y > x){
if(d < 0) { d += dE;
dE += 2;
dSE += 2;
++x; }
Else { d += dSE;
dE += 2;
dSE += 4;
++x;
--y; }
Do8Ways(x,y);
}
• }
Midpoint ellipse
• Let the equation of the ellipse centered at (0,0) be
F(x,y)= b2x2 + a2y2 –a2b2 =0
• Where 2a is the length of major axis along the x axis
• 2b is the length of minor axis along y axis
• We draw only the arc of ellipse that lies in the first
quadrant and draw other three with the help of
symmetry
• We divide the quadrant into two regions :the
boundary between the two regions is the point at
which the slope of the curve is -1.
• The vector that is perpendicular to the tangent to
the curve at any point P is called gradient .
• Grad F(x,y) =df/dx i + df/dy j = 2b2xi + 2a2yj
• The i and j components of the gradient are of equal
magnitude at the point ,where the slope of curve is
-1.
• The j component of the gradient is larger than the I
component in region 1 and vice versa in region 2.
• Thus during midpoint calculations if at next
midpoint , a2(yp – ½) <= b2(xp + 1) , we switch from
region 1 to region 2.
• As with many midpoint algorithm, we evaluate the
function at the midpoint between two pixels and
use the sign to determine whether the midpoint lies
inside or outside the ellipse and plot the pixel
which is closer to the ellipse.
• Thus in region 1 if the current pixel is located
at (xp ,yp)then the decision variable for region
1 , d1, is evaluated at (xp+1,Yp – ½) the
midpoint between E and SE.
dold = F(xp+1,yp-1/2) = b2(xp+1)2 + a2(yp-1/2)2-a2b2
• We repeat the process for deriving the two ∆’s
for the circle .For a move to E the next
midpoint is one increment over in x at
(xp+2,yp-1/2)
• dnew = b2(xp+2)2 + a2(yp – ½)2–a2b2
• dnew = dold + b2(2xp +3) the increment ∆E =
b2(2xp + 3)
• For a move to SE ,the next midpoint is at
(xp+2,Yp-3/2).
• dnew = b2(xp+2)2 + a2(yp – 3/2)2 –a2b2
• dnew = dold + b2(2xp +3)+ a2(-2yp + 2)
• increment ∆SE = b2(2xp + 3) + a2(-2yp + 2)
• In region 2 if the current pixel is at (xp,yp) ,
the decision variable d2 is F(xp+1/2, yp-1) the
midpoint between S and SE.computations are
similar as that of region 1
• Initial condition: assuming integer values a
and b, the ellipse starts at (0,b) and the first
midpoint to be calculated is at (1,b-1/2).Then
• F(1,b-1/2) = b2 + a2(b-1/2)2 –a2b2
• = b2+a2(-b+1/4)
Filling the primitive
• Rectangle:
Filling Polygons:
three types of polygons: convex, nonconvex, and
complex.
• convex: If a rubber band stretched around a
polygon touches all vertices in the order they're
defined, then the polygon is convex.
• complex:If a polygon has intersecting edges, it's
complex.
• Nonconvex: If a polygon doesn't have intersecting
edges but isn't convex, it's nonconvex.
Nonconvex is a special case of complex, and convex
is a special case of nonconvex.
Filling polygon cont….
• In order to fill a polygon, we do not want to have to
determine the type of polygon that we are filling.
The easiest way to avoid this situation is to use an
algorithm that works for all three types of polygons.
• Since both convex and concave polygons are
subsets of the complex type, using an algorithm
that will work for complex polygon filling should be
sufficient for all three types.
• The scan-line polygon fill algorithm, works for
complex polygon filling.
Filling polygon cont….
• The basic principle of scan line algorithm is , it
operates computing spans that lie between left and
right edges of the polygon .
• The span extrema are calculated by an incremental
algorithm that computes scan line/edge
intersection from the intersections with the
previous line .
• We must determine which pixels on each scan line
are within the polygon and we must set the
corresponding pixels to their appropriate values.
• By repeating this process for each scan line we scan
convert the entire polygon.
• A straight forward way of deriving extrema is
to use the midpoint line scan conversion
algorithm on each edge and to keep a table of
span extrema for each line updating an entry
if a new pixel is produced for an edge that
extends the span.
• This strategy produces some extrema pixel
that are outside the polygon..the line
algorithm has no notion of exterior or interior
point.
• Since we do not want to draw the pixels
outside the shared edge so we adjust the
algorithm accordingly
Fig shows the pixels defining the extrema of span in red
and the interior pixels on the span in blue.
Interior Pixel Convention
• Pixels that lie in the interior of a polygon
belong to that polygon, and can be filled.
• Pixels that lie on a left boundary or a lower
boundary of a polygon belong to that polygon,
and can be filled.
• Pixels that have centres that fall outside the
polygon, are said to be exterior and should
not be drawn.
• Pixels that lie on a right or an upper boundary
do not belong to that polygon, and should not
drawn.
Do the following for every scan line:
• 1. Compute the interesection of the current
scan line with every polygon edge.
• 2. Sort the intersections in increasing order of
the x coordinate.
• 3. Draw every pixel that lies between each pair
of intersections, using odd even parity rule to
designate the interior from the exterior .
Parity is initially even and each intersection
encountered revert the parity bit draw
when parity is odd , do not draw when its
even
First we will discuss the step 3
• In the following example the sorted list of x
coordinates is (2,4.2,8.5,13). Step 3 requires
four elaborations.
• 3.1 . Given an intersection with arbitrary
fractional x value , how do we determine
which pixel on either side of the intersection is
interior .
• 3.2. how do we deal with the special case of
intersection at integer pixel coordinates.
2 4 6 8 10 12 14
12
10
8
6
4
2
D
F
E
C
A
B
• 3.3.How do we deal with the special case in
3.2 for shared vertex.
• 3.4.How do we deal with the special case in
3.2 in which the vertices define a horizontal
edge.
• Solution:
• Case 3.1 : if we are approaching a fractional
intersection to the right and are inside the
polygon we round down the x coordinate of
the intersection to define the interior pixel , if
we are outside the polygon we round up to be
inside. So 4.2 -> 4 and 8.5->9
• Case 3.2: if the leftmost pixel in a span has integer x
coordinate we define it to be interior (x->2: interior),
if the rightmost pixel has integer x coordinate we
define it to be exterior (x->13:exterior).
• Case 3.3: we count the ymin of an edge in the parity
calculation but not the ymax vertex, therefore a
ymax vertex is only drawn if it is the ymin vertex of
adjacent edge.
• for scan line 3 vertex A counts only once because
it is the ymin vertex of edge FA but the ymax vertex
of edge AB this causes odd parity so we draw the
span from there to 1 pixel to the left of intersection
with edge CB where parity become even and span
terminates
Horizontal edges
We deal with horizontal edges by not counting their vertices
AB a is the ymin of AJ so parity become odd and since AB does not
contribute the line AB is drawn till vertex B which is the ymin of BC and
where parity became even and we terminate the span.
Edge coherence and scan line
algorithm
• Calculations performed in scan conversion and
other graphics algorithms typically take
advantage of various coherence properties of
a scene that is to be displayed.
• Coherence is simply that properties of one
part of a scene that are related in some way to
other parts of the scene , so that the
relationship can be used to reduce processing
Y K+1
YK
(xK, Yk)
(xK+1, Yk+1)
Many edges intersected by scan line i are also intersected
by scan line i+1. so xk+1 = xk + m.
• Using this equation we can perform integer calculations
of the x intercept by initializing a counter to zero , then
incrementing the counter by the value ∆x each time we
move up to a new scan line.
• Whenever the counter value became equal to or greater
than ∆y we increment the current intersection by 1 and
decrease the counter by the value ∆y.
This method is equivalent to maintaining integer and
fractional part for x intercept and incrementing the
fractional part until we reach the next integer value
X1 = x0 + 3/7 = x0 + .4 = x0 ( C = 3 , C< 7 x1 = x0)
X2 = x1 + 3/7 = x0 + .4 +.4 = x0 ( C = 6 , C <7 , x2 = x0)
X3 = x2 + 3/7 = x0 +.4+.4+.4 = x0 + 1.2 = (X0 + 1) + .2 = x1+.2
(C = 9 , C>7, increment x , X3 = x0=1 = x1, C= 2 ( fractional part)
Polygon filling cont..
• We now develop a scan line algorithm that takes advantage
of this edge coherence and for each scan line keeps track of
the set of edges it intersects and the intersection point in a
data structure called Active Edge Table or AET.
• The edges in the AET are sorted on their x value so that we
can fill the span defined by pairs of intersection values – that
is the span extrema
• As we move to the next scan line AET is updated
• Edges currently in the AET but not intersected by the next
scan line are deleted (ymax =y)
• New edges inserted by this next scan line ymin = y+1 are
added to the AET.
• Finally , new intersection are calculated using the preceding
incremental edge algorithm, for edges that were in the AET
but are not yet completed
• Initializing All of the Edges: The first thing that
needs to be done is determine how the
polygon's vertices are related. The all_edges
table will hold this information.
• Each adjacent set of vertices (the first and
second, second and third, ..., last and first)
defines an edge. For each edge, the following
information needs to be kept in a table:
– The minimum y value of the two vertices.
– The maximum y value of the two vertices.
– The x value associated with the minimum y value.
– The slope of the edge.
Global edge table
• To make the addition of edges to the AET
efficiently we initially create a global edge
table (ET) containing all edges sorted by their
smaller y coordinate.
• ET is typically built by bucket sort with as
many buckets as there are scan lines.
• Within each bucktetedges are kept in order of
increasing x coordinate of the lower endpoint.
Each entry in the ET contains
• the ymax co-ordinate of the edge
• The x co-ordinate of the bottom endpoint
xmin.
• X increment used in stepping from one scan
line to another
• Once the ET has been formed the processing steps
for the scan line algorithm are as follows:
• Set y to the smallest y co-ordinate that has an entry
in the ET i.e y for the first non empty bucket.
• Initialize the AET to be empty.
• Repeat until the AET and ET are empty
• 3.1 : move from the ET bucket y to the AET those edges
whose ymin = y.
• 3.2 : remove from the AET those entries for which y = ymax,
than sort the AET on x.
• 3.3 : fill in the desired pixel value on scan line y by using
pairs of x co-ordinate from the AET
• Increment y by 1(to the co-ordinate of next scan line)
• For each non vertical edge remaining in AET update x for the
new y
Global ET
8
7
6
5
4
3
2
1
0
9 2 -5/2 EF 11 7 6/4 DE
Ymax Xmin - 1/m
9 2 0 FA
11 13 0 CD
3 7 -5/2 AB 5 7 6/4 BC
AET
Scan
line
edge ymax Ymin Xmin 1/m edge ymax Ymin Xmin 1/m
1 AB 3 1 7 -5/2 BC 5 1 7 6/4
FILL ( 7,1) CALCULATE NEXT POINT USING INC REMENTAL ALGO FOR AB AND BC
FILL (7,2)
X=XMIN 7
NUM = XMAX – XMIN -5
DEM = YMAX - YMIN 2
INC = DEM 2
FOR(Y=YMIN ;
Y<=YMAX;y++)
Y=1 ,2,3
{PUTPIXEL(X,Y) {(7,1)(7,
2)(7,3)
INC += NUM -3 ,-8
IF(INC > DEM){X++ ;
INC=DEM }}}
(NO,NO
,
clipping
POINT CLIPPING
Display P = (x, y) if
xwmin <= x <= xwmax
ywmin <= y <= ywmax
LINE CLIPPING
If both points lie inside
the window
 display segment
If both points lie outside
the same boundary
discard segment
LINE CLIPPING
If both end points of the line does not
Lie across the same boundary
We need to perform further
calculations to determine
Whether there are any
Intersections and if there are where
They occur.
Brute force approach
Parametric Line Formulation
For Clipping
• Parametric form for line segment
• X = x0 + t(x1 – x0) 0 < t < 1
• Y = y0 + t(y1 – y0)
• P(t) = P0 + t(P1 – P0)
•“true,” i.e., interior intersection, if the value of Sedge &
tline lies in interval [0,1]
Outcodes for Cohen-Sutherland
Line Clipping
Divide plane into 9 regions
4 bit outcode records results of four bounds tests:
First bit: outside halfplane of top edge, above tope
edge
Second bit: outside halfplane of bottom edge, below
bottom edge
Third bit: outside halfplane of right edge, to right of
right edge
Fourth bit: outside halfplane of left edge, to left of
left edge
• Lines with OC = 0000 for both end points can be
trivially accepted
• Lines lying entirely in a half plane on outside of an
edge can be trivially rejected: OC0 & OC1 != 0 (I.e.,
ANDING OF OUTCODE OF BOTH ENDPOINTS IS
NOT 0000)
Cohen-Sutherland Algorithm
• If we can neither trivial reject nor accept, divide and
conquer: subdivide line into two segments, then can
T/A or T/R one or both segments:
• – Use a clip edge to cut the line
• – Use outcodes to choose edge that is crossed
• – Pick an order for checking edges:
top – bottom – right - left
• – Compute the intersection point
• – Iterate for the newly shortened line
• – “extra” clips may happen, e.g., E-I at H
EXAMPLE
Cyrus-Beck/Liang-Barsky Parametric Line Clipping
• Use parametric line formulation
P(t) = P0 + (P1 – P0)t
• Find the four t’s for the four clip edges, then decide which form
true intersections and calculate (x,y) for those only (<2)
For any point PEi on edge Ei
C-B/L-B Param. Line Clipping
• Now we can solve for the value of t at the Intersection of P0 P1 with the edge Ei:
• Ni • [P(t) – PEi] = 0.
• First, substitute for P(t): Ni • [P0 + (P1 – P0)t – PEi] = 0.
• Next, group terms and distribute the dot product: Ni • [P0 – PEi] + Ni • [P1 –
P0]t = 0
• Let D be the vector from P0 to P1 = (P1 – P0), and solve for t: t = Ni • [P0 –
PEi] / -Ni • D
• This gives a valid value of t only if the denominator of the expression is nonzero.
For this to be True
Ni ≠ 0 (that is, the normal should not be 0;this could occur only as
a mistake)
D ≠ 0 (that is, P1 = P0)
Ni • D ≠ 0 (edge Ei and line D are not parallel;if they are, no intersection).
• The algorithm checks these conditions
C-B/L-B Param. Line Clipping
• Eliminate t’s outside [0,1] on the line
• Which remaining t’s produce interior intersections?
• Can’t just take the innermost t values!
• Move from P0 to P1; for a given edge, just before
crossing:
if Ni • D < 0 Potentially Entering (PE)
if Ni • D > 0 Potentially Leaving (PL)
Pick inner PE, PL pair: tE for PPE with max t,tL for PPL with
min t, and tE > 0, tL < 1.
• If tL < tE, no intersection
EXAMPLE
POLYGON CLIPPING
• – Edges of polygon need to be tested against
clipping rectangle
• – May need to add new edges
• – Edges discarded or divided
• – Multiple polygons can result from a single
polygon
POLYGON CLIPPING
• Input:
• – v1, v2, … vn the vertices defining the polygon
• – Single infinite clip edge w/ inside/outside info
• Output:
• – v’1, v’2, … v’m, vertices of the clipped polygon
• Do this 4 (or ne) times for each edge of the clip
window
• Add vertices one-at-a-time to output polygon
– Using inside/outside info
– Edge intersections
Polygon Clipping Cont…..
• Can be done incrementally
• If first point inside add. If outside, don’t add
• Move around polygon from vi to vn and back
to v1
• Check vi,vi+1 wrt the clip edge
• Need vi,vi+1‘s inside/outside status
• Add vertex one at a time. There are 4 cases
4 CASES
» Case 1 : Output vi+1
» Case 2 : Output intersection point
» Case 3 : No output
» Case 4 : Output intersection point and Vi+1
Polygon clipping :Example
Polygon clipping :Example
Issues with Sutherland-
Hodgman Algorithm
• Clipping of the concave polygon
Can produce two CONNECTED areas
antialising

More Related Content

Similar to chapter 3 , foley.pptxhuujjjjjjjkjmmmm. Ibibhvucufucuvivihohi

Line drawing algorithm and antialiasing techniques
Line drawing algorithm and antialiasing techniquesLine drawing algorithm and antialiasing techniques
Line drawing algorithm and antialiasing techniquesAnkit Garg
 
Lines and curves algorithms
Lines and curves algorithmsLines and curves algorithms
Lines and curves algorithmsMohammad Sadiq
 
Computer Graphics - Introduction in Brief By: Prof. Manisha Waghmare- Butkar
Computer Graphics - Introduction in Brief By: Prof. Manisha Waghmare- ButkarComputer Graphics - Introduction in Brief By: Prof. Manisha Waghmare- Butkar
Computer Graphics - Introduction in Brief By: Prof. Manisha Waghmare- ButkarVishal Butkar
 
Lec02 03 rasterization
Lec02 03 rasterizationLec02 03 rasterization
Lec02 03 rasterizationMaaz Rizwan
 
Lec02 03 rasterization
Lec02 03 rasterizationLec02 03 rasterization
Lec02 03 rasterizationMaaz Rizwan
 
Calculus Review for semester 1 at University
Calculus Review for semester 1 at UniversityCalculus Review for semester 1 at University
Calculus Review for semester 1 at Universitynetaf56543
 
Digital differential analyzer
Digital differential analyzerDigital differential analyzer
Digital differential analyzerSajid Hasan
 
Mid point line Algorithm - Computer Graphics
Mid point line Algorithm - Computer GraphicsMid point line Algorithm - Computer Graphics
Mid point line Algorithm - Computer GraphicsDrishti Bhalla
 
Computer graphics notes 2 tutorials duniya
Computer graphics notes 2   tutorials duniyaComputer graphics notes 2   tutorials duniya
Computer graphics notes 2 tutorials duniyaTutorialsDuniya.com
 
Unit 2
Unit 2Unit 2
Unit 2ypnrao
 
Output Primitives in Computer Graphics and Multimedia
Output Primitives in Computer Graphics and MultimediaOutput Primitives in Computer Graphics and Multimedia
Output Primitives in Computer Graphics and Multimediasaranyan75
 
Computer Graphics and Multimedia Output primitives
Computer Graphics and Multimedia Output primitivesComputer Graphics and Multimedia Output primitives
Computer Graphics and Multimedia Output primitivessaranyan75
 
Rasterisation of a circle by the bresenham algorithm
Rasterisation of a circle by the bresenham algorithmRasterisation of a circle by the bresenham algorithm
Rasterisation of a circle by the bresenham algorithmKALAIRANJANI21
 
Rasterisation of a circle by the bresenham algorithm
Rasterisation of a circle by the bresenham algorithmRasterisation of a circle by the bresenham algorithm
Rasterisation of a circle by the bresenham algorithmKALAIRANJANI21
 
scan conversion of point , line and circle
scan conversion of point , line and circlescan conversion of point , line and circle
scan conversion of point , line and circleDivy Kumar Gupta
 

Similar to chapter 3 , foley.pptxhuujjjjjjjkjmmmm. Ibibhvucufucuvivihohi (20)

Line drawing algorithm and antialiasing techniques
Line drawing algorithm and antialiasing techniquesLine drawing algorithm and antialiasing techniques
Line drawing algorithm and antialiasing techniques
 
99995320.ppt
99995320.ppt99995320.ppt
99995320.ppt
 
Lines and curves algorithms
Lines and curves algorithmsLines and curves algorithms
Lines and curves algorithms
 
Computer Graphics - Introduction in Brief By: Prof. Manisha Waghmare- Butkar
Computer Graphics - Introduction in Brief By: Prof. Manisha Waghmare- ButkarComputer Graphics - Introduction in Brief By: Prof. Manisha Waghmare- Butkar
Computer Graphics - Introduction in Brief By: Prof. Manisha Waghmare- Butkar
 
Line circle draw
Line circle drawLine circle draw
Line circle draw
 
Lec02 03 rasterization
Lec02 03 rasterizationLec02 03 rasterization
Lec02 03 rasterization
 
Lec02 03 rasterization
Lec02 03 rasterizationLec02 03 rasterization
Lec02 03 rasterization
 
03.Scan Conversion.ppt
03.Scan Conversion.ppt03.Scan Conversion.ppt
03.Scan Conversion.ppt
 
Calculus Review for semester 1 at University
Calculus Review for semester 1 at UniversityCalculus Review for semester 1 at University
Calculus Review for semester 1 at University
 
Digital differential analyzer
Digital differential analyzerDigital differential analyzer
Digital differential analyzer
 
CG-Lecture3.pptx
CG-Lecture3.pptxCG-Lecture3.pptx
CG-Lecture3.pptx
 
Mid point line Algorithm - Computer Graphics
Mid point line Algorithm - Computer GraphicsMid point line Algorithm - Computer Graphics
Mid point line Algorithm - Computer Graphics
 
Computer graphics notes 2 tutorials duniya
Computer graphics notes 2   tutorials duniyaComputer graphics notes 2   tutorials duniya
Computer graphics notes 2 tutorials duniya
 
Unit 2
Unit 2Unit 2
Unit 2
 
Output Primitives in Computer Graphics and Multimedia
Output Primitives in Computer Graphics and MultimediaOutput Primitives in Computer Graphics and Multimedia
Output Primitives in Computer Graphics and Multimedia
 
Computer Graphics and Multimedia Output primitives
Computer Graphics and Multimedia Output primitivesComputer Graphics and Multimedia Output primitives
Computer Graphics and Multimedia Output primitives
 
Rasterisation of a circle by the bresenham algorithm
Rasterisation of a circle by the bresenham algorithmRasterisation of a circle by the bresenham algorithm
Rasterisation of a circle by the bresenham algorithm
 
Rasterisation of a circle by the bresenham algorithm
Rasterisation of a circle by the bresenham algorithmRasterisation of a circle by the bresenham algorithm
Rasterisation of a circle by the bresenham algorithm
 
module 1.pdf
module 1.pdfmodule 1.pdf
module 1.pdf
 
scan conversion of point , line and circle
scan conversion of point , line and circlescan conversion of point , line and circle
scan conversion of point , line and circle
 

Recently uploaded

Issues in the Philippines (Unemployment and Underemployment).pptx
Issues in the Philippines (Unemployment and Underemployment).pptxIssues in the Philippines (Unemployment and Underemployment).pptx
Issues in the Philippines (Unemployment and Underemployment).pptxJenniferPeraro1
 
Call Girl in Low Price Delhi Punjabi Bagh 9711199012
Call Girl in Low Price Delhi Punjabi Bagh  9711199012Call Girl in Low Price Delhi Punjabi Bagh  9711199012
Call Girl in Low Price Delhi Punjabi Bagh 9711199012sapnasaifi408
 
Final Completion Certificate of Marketing Management Internship
Final Completion Certificate of Marketing Management InternshipFinal Completion Certificate of Marketing Management Internship
Final Completion Certificate of Marketing Management InternshipSoham Mondal
 
Escort Service Andheri WhatsApp:+91-9833363713
Escort Service Andheri WhatsApp:+91-9833363713Escort Service Andheri WhatsApp:+91-9833363713
Escort Service Andheri WhatsApp:+91-9833363713Riya Pathan
 
VIP Call Girls Firozabad Aaradhya 8250192130 Independent Escort Service Firoz...
VIP Call Girls Firozabad Aaradhya 8250192130 Independent Escort Service Firoz...VIP Call Girls Firozabad Aaradhya 8250192130 Independent Escort Service Firoz...
VIP Call Girls Firozabad Aaradhya 8250192130 Independent Escort Service Firoz...Suhani Kapoor
 
原版定制copy澳洲查尔斯达尔文大学毕业证CDU毕业证成绩单留信学历认证保障质量
原版定制copy澳洲查尔斯达尔文大学毕业证CDU毕业证成绩单留信学历认证保障质量原版定制copy澳洲查尔斯达尔文大学毕业证CDU毕业证成绩单留信学历认证保障质量
原版定制copy澳洲查尔斯达尔文大学毕业证CDU毕业证成绩单留信学历认证保障质量sehgh15heh
 
办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一
办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一
办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一A SSS
 
办理学位证(Massey证书)新西兰梅西大学毕业证成绩单原版一比一
办理学位证(Massey证书)新西兰梅西大学毕业证成绩单原版一比一办理学位证(Massey证书)新西兰梅西大学毕业证成绩单原版一比一
办理学位证(Massey证书)新西兰梅西大学毕业证成绩单原版一比一A SSS
 
Call Girls In Bhikaji Cama Place 24/7✡️9711147426✡️ Escorts Service
Call Girls In Bhikaji Cama Place 24/7✡️9711147426✡️ Escorts ServiceCall Girls In Bhikaji Cama Place 24/7✡️9711147426✡️ Escorts Service
Call Girls In Bhikaji Cama Place 24/7✡️9711147426✡️ Escorts Servicejennyeacort
 
Ch. 9- __Skin, hair and nail Assessment (1).pdf
Ch. 9- __Skin, hair and nail Assessment (1).pdfCh. 9- __Skin, hair and nail Assessment (1).pdf
Ch. 9- __Skin, hair and nail Assessment (1).pdfJamalYaseenJameelOde
 
定制(NYIT毕业证书)美国纽约理工学院毕业证成绩单原版一比一
定制(NYIT毕业证书)美国纽约理工学院毕业证成绩单原版一比一定制(NYIT毕业证书)美国纽约理工学院毕业证成绩单原版一比一
定制(NYIT毕业证书)美国纽约理工学院毕业证成绩单原版一比一2s3dgmej
 
定制(UOIT学位证)加拿大安大略理工大学毕业证成绩单原版一比一
 定制(UOIT学位证)加拿大安大略理工大学毕业证成绩单原版一比一 定制(UOIT学位证)加拿大安大略理工大学毕业证成绩单原版一比一
定制(UOIT学位证)加拿大安大略理工大学毕业证成绩单原版一比一Fs sss
 
Kindergarten-DLL-MELC-Q3-Week 2 asf.docx
Kindergarten-DLL-MELC-Q3-Week 2 asf.docxKindergarten-DLL-MELC-Q3-Week 2 asf.docx
Kindergarten-DLL-MELC-Q3-Week 2 asf.docxLesterJayAquino
 
办理老道明大学毕业证成绩单|购买美国ODU文凭证书
办理老道明大学毕业证成绩单|购买美国ODU文凭证书办理老道明大学毕业证成绩单|购买美国ODU文凭证书
办理老道明大学毕业证成绩单|购买美国ODU文凭证书saphesg8
 
Ethics of Animal Research Laika mission.ppt
Ethics of Animal Research Laika mission.pptEthics of Animal Research Laika mission.ppt
Ethics of Animal Research Laika mission.pptShafqatShakeel1
 
Digital Marketing Training Institute in Mohali, India
Digital Marketing Training Institute in Mohali, IndiaDigital Marketing Training Institute in Mohali, India
Digital Marketing Training Institute in Mohali, IndiaDigital Discovery Institute
 
8377877756 Full Enjoy @24/7 Call Girls in Pitampura Delhi NCR
8377877756 Full Enjoy @24/7 Call Girls in Pitampura Delhi NCR8377877756 Full Enjoy @24/7 Call Girls in Pitampura Delhi NCR
8377877756 Full Enjoy @24/7 Call Girls in Pitampura Delhi NCRdollysharma2066
 
Escorts Service Near Surya International Hotel, New Delhi |9873777170| Find H...
Escorts Service Near Surya International Hotel, New Delhi |9873777170| Find H...Escorts Service Near Surya International Hotel, New Delhi |9873777170| Find H...
Escorts Service Near Surya International Hotel, New Delhi |9873777170| Find H...nitagrag2
 
MIdterm Review International Trade.pptx review
MIdterm Review International Trade.pptx reviewMIdterm Review International Trade.pptx review
MIdterm Review International Trade.pptx reviewSheldon Byron
 
do's and don'ts in Telephone Interview of Job
do's and don'ts in Telephone Interview of Jobdo's and don'ts in Telephone Interview of Job
do's and don'ts in Telephone Interview of JobRemote DBA Services
 

Recently uploaded (20)

Issues in the Philippines (Unemployment and Underemployment).pptx
Issues in the Philippines (Unemployment and Underemployment).pptxIssues in the Philippines (Unemployment and Underemployment).pptx
Issues in the Philippines (Unemployment and Underemployment).pptx
 
Call Girl in Low Price Delhi Punjabi Bagh 9711199012
Call Girl in Low Price Delhi Punjabi Bagh  9711199012Call Girl in Low Price Delhi Punjabi Bagh  9711199012
Call Girl in Low Price Delhi Punjabi Bagh 9711199012
 
Final Completion Certificate of Marketing Management Internship
Final Completion Certificate of Marketing Management InternshipFinal Completion Certificate of Marketing Management Internship
Final Completion Certificate of Marketing Management Internship
 
Escort Service Andheri WhatsApp:+91-9833363713
Escort Service Andheri WhatsApp:+91-9833363713Escort Service Andheri WhatsApp:+91-9833363713
Escort Service Andheri WhatsApp:+91-9833363713
 
VIP Call Girls Firozabad Aaradhya 8250192130 Independent Escort Service Firoz...
VIP Call Girls Firozabad Aaradhya 8250192130 Independent Escort Service Firoz...VIP Call Girls Firozabad Aaradhya 8250192130 Independent Escort Service Firoz...
VIP Call Girls Firozabad Aaradhya 8250192130 Independent Escort Service Firoz...
 
原版定制copy澳洲查尔斯达尔文大学毕业证CDU毕业证成绩单留信学历认证保障质量
原版定制copy澳洲查尔斯达尔文大学毕业证CDU毕业证成绩单留信学历认证保障质量原版定制copy澳洲查尔斯达尔文大学毕业证CDU毕业证成绩单留信学历认证保障质量
原版定制copy澳洲查尔斯达尔文大学毕业证CDU毕业证成绩单留信学历认证保障质量
 
办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一
办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一
办理学位证(UoM证书)北安普顿大学毕业证成绩单原版一比一
 
办理学位证(Massey证书)新西兰梅西大学毕业证成绩单原版一比一
办理学位证(Massey证书)新西兰梅西大学毕业证成绩单原版一比一办理学位证(Massey证书)新西兰梅西大学毕业证成绩单原版一比一
办理学位证(Massey证书)新西兰梅西大学毕业证成绩单原版一比一
 
Call Girls In Bhikaji Cama Place 24/7✡️9711147426✡️ Escorts Service
Call Girls In Bhikaji Cama Place 24/7✡️9711147426✡️ Escorts ServiceCall Girls In Bhikaji Cama Place 24/7✡️9711147426✡️ Escorts Service
Call Girls In Bhikaji Cama Place 24/7✡️9711147426✡️ Escorts Service
 
Ch. 9- __Skin, hair and nail Assessment (1).pdf
Ch. 9- __Skin, hair and nail Assessment (1).pdfCh. 9- __Skin, hair and nail Assessment (1).pdf
Ch. 9- __Skin, hair and nail Assessment (1).pdf
 
定制(NYIT毕业证书)美国纽约理工学院毕业证成绩单原版一比一
定制(NYIT毕业证书)美国纽约理工学院毕业证成绩单原版一比一定制(NYIT毕业证书)美国纽约理工学院毕业证成绩单原版一比一
定制(NYIT毕业证书)美国纽约理工学院毕业证成绩单原版一比一
 
定制(UOIT学位证)加拿大安大略理工大学毕业证成绩单原版一比一
 定制(UOIT学位证)加拿大安大略理工大学毕业证成绩单原版一比一 定制(UOIT学位证)加拿大安大略理工大学毕业证成绩单原版一比一
定制(UOIT学位证)加拿大安大略理工大学毕业证成绩单原版一比一
 
Kindergarten-DLL-MELC-Q3-Week 2 asf.docx
Kindergarten-DLL-MELC-Q3-Week 2 asf.docxKindergarten-DLL-MELC-Q3-Week 2 asf.docx
Kindergarten-DLL-MELC-Q3-Week 2 asf.docx
 
办理老道明大学毕业证成绩单|购买美国ODU文凭证书
办理老道明大学毕业证成绩单|购买美国ODU文凭证书办理老道明大学毕业证成绩单|购买美国ODU文凭证书
办理老道明大学毕业证成绩单|购买美国ODU文凭证书
 
Ethics of Animal Research Laika mission.ppt
Ethics of Animal Research Laika mission.pptEthics of Animal Research Laika mission.ppt
Ethics of Animal Research Laika mission.ppt
 
Digital Marketing Training Institute in Mohali, India
Digital Marketing Training Institute in Mohali, IndiaDigital Marketing Training Institute in Mohali, India
Digital Marketing Training Institute in Mohali, India
 
8377877756 Full Enjoy @24/7 Call Girls in Pitampura Delhi NCR
8377877756 Full Enjoy @24/7 Call Girls in Pitampura Delhi NCR8377877756 Full Enjoy @24/7 Call Girls in Pitampura Delhi NCR
8377877756 Full Enjoy @24/7 Call Girls in Pitampura Delhi NCR
 
Escorts Service Near Surya International Hotel, New Delhi |9873777170| Find H...
Escorts Service Near Surya International Hotel, New Delhi |9873777170| Find H...Escorts Service Near Surya International Hotel, New Delhi |9873777170| Find H...
Escorts Service Near Surya International Hotel, New Delhi |9873777170| Find H...
 
MIdterm Review International Trade.pptx review
MIdterm Review International Trade.pptx reviewMIdterm Review International Trade.pptx review
MIdterm Review International Trade.pptx review
 
do's and don'ts in Telephone Interview of Job
do's and don'ts in Telephone Interview of Jobdo's and don'ts in Telephone Interview of Job
do's and don'ts in Telephone Interview of Job
 

chapter 3 , foley.pptxhuujjjjjjjkjmmmm. Ibibhvucufucuvivihohi

  • 2. Point and Lines • Point plotting is accomplished by converting a single coordinate position furnished by an application program into appropriate operations for the output device in use. • Line drawing is accomplished by calculating intermediate positions along the line path between two specified endpoint positions. • An output device is directed to fill in these positions between the endpoints.
  • 3. Plotting with Analog devices • For analog devices or a random scan display, a straight line can be drawn smoothly from one endpoint to the other . • Linearly varying horizontal and vertical deflection voltages are generated that are proportional to the required changes in the x and y directions to produce a smooth line.
  • 4. Plotting with digital device • Digital devices display a straight line segment by plotting discrete points between the two end points. • Discrete coordinate positions along the line path are calculated from the equation of line . • For a raster video display the line color is then loaded into frame buffer at the corresponding pixel coordinates. • Reading from the frame buffer the video controller the plots the screen pixels.
  • 5. Since screen locations are referenced with integer values , so plotted positions are only the approximation of the actual line path between two given points
  • 6. • A computed line position of 10.48,20.51 would be converted to 10,21. • This rounding of coordinate values to integers causes lines to be displayed with a staircase appearance. • The characteristic staircase shape is particularly noticeable on systems with low resolution, so their appearance can be improved by somewhat displaying tem on higher resolution system • Techniques for smoothing lines can be used called antialising
  • 7. Basic incremental algorithm • The equation of a line in cartesian coordinates is • y = m.x + b………………1 where m is slope and c as the y intercept. Where m = ∆y/∆x…………………2 or ∆y = m . ∆x…………………3 For any given x interval ∆x we can compute the corresponding y interval ∆y Similarly we can obtain the x interval ∆x corresponding to specified ∆y as m = ∆x/∆y………………..4 • Thus we sample line at discrete points and determine the nearest pixel to the line at each sampled position.
  • 8. Incremental / DDA algorithm • For lines with slope magnitudes |m| < 1 , ∆x can be set to 1 , we sample at unit x intervals and compute each successive value as • yi+1 = m. xi+1 + b = m(xi +∆x) + b = m.xi + b + m. ∆x = yi + m.∆x ..since ∆x = 1 , we have • yi+1 = yi + m…………………….5 • For lines with slope greater than 1 we reverse the role of x and y .thus we sample at unit y intervals and calculate the succeeding x value
  • 9. • Xi+1 = xi + 1/m………………6 • Equations 5 and 6 are based on the assumption that lines are to be processed from left to right endpoint . If this processing is reversed so that the endpoint is at the right then we have ∆x = -1 and • Yi+1 = yi – m • Xi=1 = xi – 1/m
  • 10. DDA : Algorithm • Step 1: Get the input of two end points (X0, Y0) and (X1, Y1). • Step 2: Calculate the difference between two end points. dx = X1 - X0 ,dy = Y1 - Y0 • Step 3: Based on the calculated difference in step-2, you need to identify the number of steps to put pixel. If dx > dy, then you need more steps in x coordinate; otherwise in y coordinate. if(absolute(dx) > absolute(dy)) Steps = absolute(dx); else Steps = absolute(dy); Step 4: Calculate the increment in x and y coordinate. Xincrement = dx / (float) steps; Yincrement = dy / (float) steps;
  • 11. Step 5: Put the pixel by successfully incrementing x and y coordinates accordingly and complete the drawing of the line. for(int v=0; v < Steps; v++) { x = x + Xincrement; y = y + Yincrement; putpixel(Round(x), Round(y)); }
  • 12. DDA • Drawbacks • round off and floating point procedures are time consuming. • Accumulation of round off in successive additions of the floating point increment causes pixel positions to drift away from true line path for long lines. Improvement : Bresenham’s Line algorithm
  • 13. Bresenham’s line algorithm • The drawback of DDA algorithm is that rounding operations and floating point arithmetic is time consuming. • An accurate and efficient raster line generating algorithm developed by bresenham scan converts line using only incremental calculations. • Sampling at unit x intervals we need to decide which of the two possible pixel positions is closer to the line path at each sample step
  • 14. 13 12 11 10 10 11 12 13 Need to decide Whether to plot pixel 11,11 or 11,12 in bresenham’s approach we plot the pixel whose scan line y value is closest to the line path.
  • 15. Midpoint line algorithm • We assume that the line’s slope is between 0 and 1 • Lower left end point is x0,y0 and the upper end right endpoint x1,y1. • Assuming that we have just selectedd the pixel at P (xp,yp) and now we have to decide whether to plot pixel to the right(called the east pixel ,E)or the pixel one increment to the right and one increment up (called the northeast pixel , NE)
  • 16. NE M E P= (xp,yp) •We observe on which side of the line the midpoint M lies •If the midpoint lies above the line pixel E is chosen •If the midpoint lies below the line NE is closer to line
  • 17. Now we need to determine which side the midpoint lies • Let we represent the line by an function having coefficients a ,b and c • F(x,y) = ax + by + c= 0 • If dy = y1-y0 and dx = x1-x0 then • Y = dy/dx x + B therefore • F(x,y) = dy.x – dx.y +B.dx =0 where • A = dy , b = -dx and c =B.dx • If a point is on the line it satisfies the first equation and F(x,y) = 0, positive for points below the line and negative for points above the line • To apply the midpoint criterion we need only to compute F(M) = F(xp+1,yp+1/2)
  • 18. Mid point Algorithm for line • Since our decision is based on the value of function at xp+1 ,yp+ ½ we define a dexision variable d = F(xp+1,yp+1/2) • If d>0 we choose pixel NE • If d<0 we choose pixel E • If d=0 we choose either ,let we pick E • How we calculate M for next grid line which depends whether we choose E or NE • If E is choosen M is incremented one step in the x direction • dnew = F(xp+2,yp+1/2)=a(xp+2) +b(yp+1/2) =c • dold = a(xp+1) + b(yp+1/2)+c
  • 19. • Subtracting dold from dnew • dnew = dold +a, let ∆E = a =dy. • We can derive the value of the decision variable at the next step incrementally from the value at the current step merely by adding dy or ∆E. • If NE is chosen ,M is incremented one step in both x and y direction thus : • dnew = F(xp+1/2,yp+3/2)=a(xp+2)+b(yp+3/2)+c • Subtracting dold from dnew for NE • dnew=dold+a +b,let we call the increment to add d after NE as ∆NE= a+b = dy-dx
  • 20. • Since the first pixel is simply (x0,y0) so the first midpoint • F(x0+1,y0+1/2)=a(x0+1) + b(y0+1/2)+c • =ax0=by0+c+a+b/2 • =F(x0,y0)+a+b/2 • Since initial point lies on line so F(x0,y0)=0. • dstart= a+b/2 = dy - dx/2 • To eliminate fraction in dstart redefine the original F by multiplying it by 2 • F(x,y)=2(ax+by+c) which multiplies each constant and decision variable by 2 but does not affect the sign of decision variable
  • 21. So in incremental midpoint technique the algorithm chooses between 2 pixels based on the sign of the decision variable by adding either ∆E or ∆NE to the old value depending on the choice of pixel
  • 22. Midpoint circle Algorithm • The equation of a circle centered at originis • X2 +y2 = r2 solving it for y we get • Y = +-√r2 - x2 • To draw a quarter circle, we can increment x from 0 to r in unit steps solving for +y at each step. • This approach works but it is inefficient because of multiply and square root operations
  • 23. Furthermore the circle will have large gaps for values of x close to r, because the slope of the circle becomes infinite there. We can avoid the large gaps , or can have uniform plotting if we plot (rcosѲ,rsinѲ) (by stepping Ѳ from 0 degree to 90 degree) in angular co-ordinates Drawing process can be improved by taking advantage of circle symmetry if a point is on the circle than we can compute seven other points on it
  • 24. ( (y,x) (x,y) (-x,y) (-y,x) (-y,-x) (y,-x) (-x,-y) (x,-y) Void circlepoints(int x,int y, int value) { writepixel(x,y,value); writepixel(y,x,value); writepixel(y,-x,value); writepixel(x,-y,value); writepixel(-x,-y,value); writepixel(-y,-x,value); writepixel(-y,x,value); writepixel(-x,y,value); }
  • 25. Midpoint circle algorithm • Bresenham developed an incremental circle generator that generates all points on a circle centered at origin by incrementing all the way round the circle. • We consider only 45 degree of a circle from x=0 to x= y and use the symmetry of the circle to display points on the circle. • If pixel P has already been chosen the next pixel is plotted by evaluating which out of E and SE is closer to the circle to the circle by using a function at the midpoint between the two pixel
  • 27. Let F(x,y) = x2 +y2-r2 • This function is 0 on the circle • Negative inside the circle • Positive outside the circle • Therefore if midpoint between pixel E and SE is outside the circle then pixel SE is closer to the circle. • If midpoint is inside the circle pixel E is closer.
  • 28. Thus if decision parameter is negative  we plot pixel E positive we plot pixel SE
  • 29. • dold = F(xp+1,Yp-1/2) =(xp+1)2 + (yp-1/2)2 –r2……….1. • If dold <0, E is chosen and the next midpoint will be one incremental over x. • dnew =F(xp+2,yp-1/2)=(xp+2)2+yp-1/2)2-r2…..2 • Subtracting equation 2 from equation 1 • dnew = dold +(2xp+3) • ∆E = 2xp+3 • If dold >=0,SE is chosen and the next midpoint will be one increment over x and one decrement in y
  • 30. • Dnew = F(xp+2,yp-3/2) =(xp+2)2 + (yp-3/2)2 –r2 • Dnew = dold + 2xp -2yp +E • ∆SE = 2xp+(-2yp) +5 • Since these functions are expressed in terms of (xp,yp) we call p the point of evaluation
  • 31. Initial conditions for decision parameter • The starting pixel lies on the circle at (0,r) • The next midpoint lies at (1,r-1/2) • Thus F(1,r-1/2)=1+(r2-2+1/4)-r2 • = 5/4 –r • Problem : we are forced to do the real arithmetic because of the fractional initialization of d.
  • 32. Solution • h = d - ¼, so substitute d = h + ¼ so • h = 1 - R d < 0 means that h < ¼, but since h treated as an integer, h < 0 ,call h by d.
  • 33. C code with integer calculations • Circle(int R){ • int x,y, d; • x = 0; • y = R; • d = 1 - R; • Do8Ways(x,y); • while(y > x){ • if(d < 0){ • d += (2*x + 3); • ++x; • } • else{ • d += (2*(x-y) + 5); • ++x; • --y; • } • Do8Ways(x,y);
  • 34. SECOND ORDER DIFFRENCIATION • improve performance by computing linear equations for DE & DSE incrementally, as we did for dnew. calculate first & second-order differences if choose E (xp yp) -> (xp + 1, yp) • DEold at (xp, yp) = 2xp + 3 • DEnew at (xp + 1, yp) = 2(xp + 1) + 3 • DEnew - DEold = 2 & • DSEold at (xp, yp) = 2xp - 2yp + 5 • DSEnew at (xp + 1, yp) = 2(xp + 1) - 2yp + 5 • DSEnew - DSEold = 2 • if choose SE (xp yp) -> (xp + 1, yp - 1) • DEnew at (xp + 1, yp - 1) = 2(xp + 1) + 3 • DEnew - DEold = 2 • & • DSEnew at (xp + 1, yp - 1) = 2(xp + 1) - 2(yp -1) + 5 • DSEnew - DSEold = 4 So 1. Choose pixel based on d 2. Update d by DE or DSE 3. Update DE & DSE Initialize DE & DSE using (0, R)
  • 35. C Code (with second order differences). • Circle(int R){ • int x,y, d, dE, dSE; • x = 0; y = R; d = 1 - R; • dE = 3; • dSE = -2*R + 5; • Do8Ways(x,y); • while(y > x){ if(d < 0) { d += dE; dE += 2; dSE += 2; ++x; } Else { d += dSE; dE += 2; dSE += 4; ++x; --y; } Do8Ways(x,y); } • }
  • 36. Midpoint ellipse • Let the equation of the ellipse centered at (0,0) be F(x,y)= b2x2 + a2y2 –a2b2 =0 • Where 2a is the length of major axis along the x axis • 2b is the length of minor axis along y axis • We draw only the arc of ellipse that lies in the first quadrant and draw other three with the help of symmetry • We divide the quadrant into two regions :the boundary between the two regions is the point at which the slope of the curve is -1. • The vector that is perpendicular to the tangent to the curve at any point P is called gradient .
  • 37. • Grad F(x,y) =df/dx i + df/dy j = 2b2xi + 2a2yj • The i and j components of the gradient are of equal magnitude at the point ,where the slope of curve is -1. • The j component of the gradient is larger than the I component in region 1 and vice versa in region 2. • Thus during midpoint calculations if at next midpoint , a2(yp – ½) <= b2(xp + 1) , we switch from region 1 to region 2. • As with many midpoint algorithm, we evaluate the function at the midpoint between two pixels and use the sign to determine whether the midpoint lies inside or outside the ellipse and plot the pixel which is closer to the ellipse.
  • 38. • Thus in region 1 if the current pixel is located at (xp ,yp)then the decision variable for region 1 , d1, is evaluated at (xp+1,Yp – ½) the midpoint between E and SE. dold = F(xp+1,yp-1/2) = b2(xp+1)2 + a2(yp-1/2)2-a2b2
  • 39. • We repeat the process for deriving the two ∆’s for the circle .For a move to E the next midpoint is one increment over in x at (xp+2,yp-1/2) • dnew = b2(xp+2)2 + a2(yp – ½)2–a2b2 • dnew = dold + b2(2xp +3) the increment ∆E = b2(2xp + 3) • For a move to SE ,the next midpoint is at (xp+2,Yp-3/2). • dnew = b2(xp+2)2 + a2(yp – 3/2)2 –a2b2 • dnew = dold + b2(2xp +3)+ a2(-2yp + 2) • increment ∆SE = b2(2xp + 3) + a2(-2yp + 2)
  • 40. • In region 2 if the current pixel is at (xp,yp) , the decision variable d2 is F(xp+1/2, yp-1) the midpoint between S and SE.computations are similar as that of region 1 • Initial condition: assuming integer values a and b, the ellipse starts at (0,b) and the first midpoint to be calculated is at (1,b-1/2).Then • F(1,b-1/2) = b2 + a2(b-1/2)2 –a2b2 • = b2+a2(-b+1/4)
  • 42. Filling Polygons: three types of polygons: convex, nonconvex, and complex. • convex: If a rubber band stretched around a polygon touches all vertices in the order they're defined, then the polygon is convex. • complex:If a polygon has intersecting edges, it's complex. • Nonconvex: If a polygon doesn't have intersecting edges but isn't convex, it's nonconvex. Nonconvex is a special case of complex, and convex is a special case of nonconvex.
  • 43. Filling polygon cont…. • In order to fill a polygon, we do not want to have to determine the type of polygon that we are filling. The easiest way to avoid this situation is to use an algorithm that works for all three types of polygons. • Since both convex and concave polygons are subsets of the complex type, using an algorithm that will work for complex polygon filling should be sufficient for all three types. • The scan-line polygon fill algorithm, works for complex polygon filling.
  • 44. Filling polygon cont…. • The basic principle of scan line algorithm is , it operates computing spans that lie between left and right edges of the polygon . • The span extrema are calculated by an incremental algorithm that computes scan line/edge intersection from the intersections with the previous line . • We must determine which pixels on each scan line are within the polygon and we must set the corresponding pixels to their appropriate values. • By repeating this process for each scan line we scan convert the entire polygon.
  • 45. • A straight forward way of deriving extrema is to use the midpoint line scan conversion algorithm on each edge and to keep a table of span extrema for each line updating an entry if a new pixel is produced for an edge that extends the span. • This strategy produces some extrema pixel that are outside the polygon..the line algorithm has no notion of exterior or interior point. • Since we do not want to draw the pixels outside the shared edge so we adjust the algorithm accordingly
  • 46. Fig shows the pixels defining the extrema of span in red and the interior pixels on the span in blue.
  • 47. Interior Pixel Convention • Pixels that lie in the interior of a polygon belong to that polygon, and can be filled. • Pixels that lie on a left boundary or a lower boundary of a polygon belong to that polygon, and can be filled. • Pixels that have centres that fall outside the polygon, are said to be exterior and should not be drawn. • Pixels that lie on a right or an upper boundary do not belong to that polygon, and should not drawn.
  • 48. Do the following for every scan line: • 1. Compute the interesection of the current scan line with every polygon edge. • 2. Sort the intersections in increasing order of the x coordinate. • 3. Draw every pixel that lies between each pair of intersections, using odd even parity rule to designate the interior from the exterior . Parity is initially even and each intersection encountered revert the parity bit draw when parity is odd , do not draw when its even
  • 49. First we will discuss the step 3 • In the following example the sorted list of x coordinates is (2,4.2,8.5,13). Step 3 requires four elaborations. • 3.1 . Given an intersection with arbitrary fractional x value , how do we determine which pixel on either side of the intersection is interior . • 3.2. how do we deal with the special case of intersection at integer pixel coordinates.
  • 50. 2 4 6 8 10 12 14 12 10 8 6 4 2 D F E C A B
  • 51. • 3.3.How do we deal with the special case in 3.2 for shared vertex. • 3.4.How do we deal with the special case in 3.2 in which the vertices define a horizontal edge. • Solution: • Case 3.1 : if we are approaching a fractional intersection to the right and are inside the polygon we round down the x coordinate of the intersection to define the interior pixel , if we are outside the polygon we round up to be inside. So 4.2 -> 4 and 8.5->9
  • 52. • Case 3.2: if the leftmost pixel in a span has integer x coordinate we define it to be interior (x->2: interior), if the rightmost pixel has integer x coordinate we define it to be exterior (x->13:exterior). • Case 3.3: we count the ymin of an edge in the parity calculation but not the ymax vertex, therefore a ymax vertex is only drawn if it is the ymin vertex of adjacent edge. • for scan line 3 vertex A counts only once because it is the ymin vertex of edge FA but the ymax vertex of edge AB this causes odd parity so we draw the span from there to 1 pixel to the left of intersection with edge CB where parity become even and span terminates
  • 53. Horizontal edges We deal with horizontal edges by not counting their vertices AB a is the ymin of AJ so parity become odd and since AB does not contribute the line AB is drawn till vertex B which is the ymin of BC and where parity became even and we terminate the span.
  • 54. Edge coherence and scan line algorithm • Calculations performed in scan conversion and other graphics algorithms typically take advantage of various coherence properties of a scene that is to be displayed. • Coherence is simply that properties of one part of a scene that are related in some way to other parts of the scene , so that the relationship can be used to reduce processing
  • 55. Y K+1 YK (xK, Yk) (xK+1, Yk+1) Many edges intersected by scan line i are also intersected by scan line i+1. so xk+1 = xk + m. • Using this equation we can perform integer calculations of the x intercept by initializing a counter to zero , then incrementing the counter by the value ∆x each time we move up to a new scan line. • Whenever the counter value became equal to or greater than ∆y we increment the current intersection by 1 and decrease the counter by the value ∆y. This method is equivalent to maintaining integer and fractional part for x intercept and incrementing the fractional part until we reach the next integer value
  • 56. X1 = x0 + 3/7 = x0 + .4 = x0 ( C = 3 , C< 7 x1 = x0) X2 = x1 + 3/7 = x0 + .4 +.4 = x0 ( C = 6 , C <7 , x2 = x0) X3 = x2 + 3/7 = x0 +.4+.4+.4 = x0 + 1.2 = (X0 + 1) + .2 = x1+.2 (C = 9 , C>7, increment x , X3 = x0=1 = x1, C= 2 ( fractional part)
  • 57. Polygon filling cont.. • We now develop a scan line algorithm that takes advantage of this edge coherence and for each scan line keeps track of the set of edges it intersects and the intersection point in a data structure called Active Edge Table or AET. • The edges in the AET are sorted on their x value so that we can fill the span defined by pairs of intersection values – that is the span extrema • As we move to the next scan line AET is updated • Edges currently in the AET but not intersected by the next scan line are deleted (ymax =y) • New edges inserted by this next scan line ymin = y+1 are added to the AET. • Finally , new intersection are calculated using the preceding incremental edge algorithm, for edges that were in the AET but are not yet completed
  • 58. • Initializing All of the Edges: The first thing that needs to be done is determine how the polygon's vertices are related. The all_edges table will hold this information. • Each adjacent set of vertices (the first and second, second and third, ..., last and first) defines an edge. For each edge, the following information needs to be kept in a table: – The minimum y value of the two vertices. – The maximum y value of the two vertices. – The x value associated with the minimum y value. – The slope of the edge.
  • 59.
  • 60. Global edge table • To make the addition of edges to the AET efficiently we initially create a global edge table (ET) containing all edges sorted by their smaller y coordinate. • ET is typically built by bucket sort with as many buckets as there are scan lines. • Within each bucktetedges are kept in order of increasing x coordinate of the lower endpoint.
  • 61. Each entry in the ET contains • the ymax co-ordinate of the edge • The x co-ordinate of the bottom endpoint xmin. • X increment used in stepping from one scan line to another
  • 62. • Once the ET has been formed the processing steps for the scan line algorithm are as follows: • Set y to the smallest y co-ordinate that has an entry in the ET i.e y for the first non empty bucket. • Initialize the AET to be empty. • Repeat until the AET and ET are empty • 3.1 : move from the ET bucket y to the AET those edges whose ymin = y. • 3.2 : remove from the AET those entries for which y = ymax, than sort the AET on x. • 3.3 : fill in the desired pixel value on scan line y by using pairs of x co-ordinate from the AET • Increment y by 1(to the co-ordinate of next scan line) • For each non vertical edge remaining in AET update x for the new y
  • 63. Global ET 8 7 6 5 4 3 2 1 0 9 2 -5/2 EF 11 7 6/4 DE Ymax Xmin - 1/m 9 2 0 FA 11 13 0 CD 3 7 -5/2 AB 5 7 6/4 BC
  • 64. AET Scan line edge ymax Ymin Xmin 1/m edge ymax Ymin Xmin 1/m 1 AB 3 1 7 -5/2 BC 5 1 7 6/4 FILL ( 7,1) CALCULATE NEXT POINT USING INC REMENTAL ALGO FOR AB AND BC FILL (7,2) X=XMIN 7 NUM = XMAX – XMIN -5 DEM = YMAX - YMIN 2 INC = DEM 2 FOR(Y=YMIN ; Y<=YMAX;y++) Y=1 ,2,3 {PUTPIXEL(X,Y) {(7,1)(7, 2)(7,3) INC += NUM -3 ,-8 IF(INC > DEM){X++ ; INC=DEM }}} (NO,NO ,
  • 66.
  • 67. POINT CLIPPING Display P = (x, y) if xwmin <= x <= xwmax ywmin <= y <= ywmax
  • 68. LINE CLIPPING If both points lie inside the window  display segment If both points lie outside the same boundary discard segment
  • 69. LINE CLIPPING If both end points of the line does not Lie across the same boundary We need to perform further calculations to determine Whether there are any Intersections and if there are where They occur. Brute force approach
  • 70. Parametric Line Formulation For Clipping • Parametric form for line segment • X = x0 + t(x1 – x0) 0 < t < 1 • Y = y0 + t(y1 – y0) • P(t) = P0 + t(P1 – P0) •“true,” i.e., interior intersection, if the value of Sedge & tline lies in interval [0,1]
  • 71. Outcodes for Cohen-Sutherland Line Clipping Divide plane into 9 regions 4 bit outcode records results of four bounds tests: First bit: outside halfplane of top edge, above tope edge Second bit: outside halfplane of bottom edge, below bottom edge Third bit: outside halfplane of right edge, to right of right edge Fourth bit: outside halfplane of left edge, to left of left edge • Lines with OC = 0000 for both end points can be trivially accepted • Lines lying entirely in a half plane on outside of an edge can be trivially rejected: OC0 & OC1 != 0 (I.e., ANDING OF OUTCODE OF BOTH ENDPOINTS IS NOT 0000)
  • 72. Cohen-Sutherland Algorithm • If we can neither trivial reject nor accept, divide and conquer: subdivide line into two segments, then can T/A or T/R one or both segments: • – Use a clip edge to cut the line • – Use outcodes to choose edge that is crossed • – Pick an order for checking edges: top – bottom – right - left • – Compute the intersection point • – Iterate for the newly shortened line • – “extra” clips may happen, e.g., E-I at H
  • 74. Cyrus-Beck/Liang-Barsky Parametric Line Clipping • Use parametric line formulation P(t) = P0 + (P1 – P0)t • Find the four t’s for the four clip edges, then decide which form true intersections and calculate (x,y) for those only (<2) For any point PEi on edge Ei
  • 75. C-B/L-B Param. Line Clipping • Now we can solve for the value of t at the Intersection of P0 P1 with the edge Ei: • Ni • [P(t) – PEi] = 0. • First, substitute for P(t): Ni • [P0 + (P1 – P0)t – PEi] = 0. • Next, group terms and distribute the dot product: Ni • [P0 – PEi] + Ni • [P1 – P0]t = 0 • Let D be the vector from P0 to P1 = (P1 – P0), and solve for t: t = Ni • [P0 – PEi] / -Ni • D • This gives a valid value of t only if the denominator of the expression is nonzero. For this to be True Ni ≠ 0 (that is, the normal should not be 0;this could occur only as a mistake) D ≠ 0 (that is, P1 = P0) Ni • D ≠ 0 (edge Ei and line D are not parallel;if they are, no intersection). • The algorithm checks these conditions
  • 76. C-B/L-B Param. Line Clipping • Eliminate t’s outside [0,1] on the line • Which remaining t’s produce interior intersections? • Can’t just take the innermost t values! • Move from P0 to P1; for a given edge, just before crossing: if Ni • D < 0 Potentially Entering (PE) if Ni • D > 0 Potentially Leaving (PL) Pick inner PE, PL pair: tE for PPE with max t,tL for PPL with min t, and tE > 0, tL < 1. • If tL < tE, no intersection
  • 78. POLYGON CLIPPING • – Edges of polygon need to be tested against clipping rectangle • – May need to add new edges • – Edges discarded or divided • – Multiple polygons can result from a single polygon
  • 79. POLYGON CLIPPING • Input: • – v1, v2, … vn the vertices defining the polygon • – Single infinite clip edge w/ inside/outside info • Output: • – v’1, v’2, … v’m, vertices of the clipped polygon • Do this 4 (or ne) times for each edge of the clip window • Add vertices one-at-a-time to output polygon – Using inside/outside info – Edge intersections
  • 80. Polygon Clipping Cont….. • Can be done incrementally • If first point inside add. If outside, don’t add • Move around polygon from vi to vn and back to v1 • Check vi,vi+1 wrt the clip edge • Need vi,vi+1‘s inside/outside status • Add vertex one at a time. There are 4 cases
  • 81. 4 CASES » Case 1 : Output vi+1 » Case 2 : Output intersection point » Case 3 : No output » Case 4 : Output intersection point and Vi+1
  • 84. Issues with Sutherland- Hodgman Algorithm • Clipping of the concave polygon Can produce two CONNECTED areas

Editor's Notes

  1. Or lines with slpoe