Chapter 3, Foley
Chapter 3, Foley
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
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.
• ∆SE = 2xp+(-2yp) +5
• 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.
D
10
8 F
E
6
4
C
2 A
B
2 4 6 8 10 12
14
• 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
(xK, Yk) YK
As we move up to the next 3 scan lines along this edge the counter is successfully
assigned the value 3,6 and 9 .
On the third scan line above the initial scan line the counter now has a value greater
than 7 so we increment the x intersection coordinate by 1 and reset the counter to
the value 9 – 7 =2
We continue determining the scan line intersection to this way until we reach the
upper edge point of the edge
7 9 2 -5/2 EF 11 7 6/4 DE
6
5 11 13 0 CD
4
3 9 2 0 FA
2
1 3 7 -5/2 AB 5 7 6/4 BC
0
AET
Scan edge ymax Ymin Xmin 1/m edge ymax Ymin Xmin 1/m
line
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
INC = DEM 2
Display P = (x, y) if