Professional Documents
Culture Documents
r2 = radius * radius;
for (x = -radius; x <= radius; x++)
{
y = (int)(sqrt(r2 - x*x) + 0.5);
setPixel(xCenter + x, yCenter + y, c);
setPixel(xCenter + x, yCenter – y, c);
}
}
y0 20 2 0 2 20
y1 20 2 12 20
y2 20 2 2 2 20
y19 20 2 19 2 6
y20 20 2 20 2 0
October 6, 2020 Computer Graphics 5
Scan Converting Circle
– However, unsurprisingly this is not a brilliant solution!
– Firstly, it involves all floating point calculations
– Secondly, the calculations are not very efficient
• The square (multiply) operations
• The square root operation – try really hard to avoid these!
– Thirdly, the resulting circle has large gaps where the slope
approaches the vertical (why ?)
put_circle_pixel(x,y,h,k)
{
Put_pixel(h+x, k+y)
Put_pixel(h-x, k+y)
Put_pixel(h-x, k-y)
Put_pixel(h+x, k-y)
Put_pixel(h+y, k+x)
Put_pixel(h-y, k+x)
Put_pixel(h-y, k-x)
Put_pixel(h+y, k-x)
}
M
5
6
M
5
– If pk < 0 ,
• this midpoint is inside the circle and
• the pixel on the scan line yk is closer to the circle boundary.
Otherwise,
• the mid position is outside or on the circle boundary,
• and we select the pixel on the scan line yk-1
Put k = k+1
pk+1 = [(xk+1)+1]2 + (yk+1 – ½ )2 – r2
= (xk+2)2 + (yk+1 – ½ )2 – r2
2 2
b x xc y yc
1
a b
a
put_ellipse_pixel(x,y,h,k)
{
Put_pixel(h+x, k+y)
Put_pixel(h-x, k+y)
Put_pixel(h+x, k-y)
Put_pixel(h-x, k-y)
}
– Region 1:
• Sampling is at x direction
• Choose between (xk+1, yk), or (xk+1, yk-1)
Region 1
• Midpoint: (xk+1, yk-0.5)
Region 2
– Region 2:
• Sampling is at y direction
• Choose between (xk, yk-1), or (xk+1, yk-1)
• Midpoint: (xk+0.5, yk-1)
– If p1k < 0 ,
• this midpoint is inside the ellipse and
• the pixel on the scan line yk is closer to the ellipse boundary.
Otherwise,
• the mid position is outside or on the ellipse boundary,
• and we select the pixel on the scan line yk-1
Put k = k+1
p1k+1 = b2 [(xk+1)+1]2 + a2(yk+1 – ½ )2 – a2b2
= b2 (xk+2)2 + a2(yk+1 – ½ )2 – a2b2
subtracting pk from pk+1
p1k+1 – p1k = b2(xk+2)2 + a2(yk+1 – ½ )2 – [b2(xk +1)2 + a2(yk – ½ )2] or
p1k+1 = p1k+2 b2(xk+1) + a2 [(yk+1 – ½)2 – (yk – ½)2]+b2
Where yk+1 is either yk or yk-1, depending on the sign of p1k
= b2 – a2b + ¼ a2
– If p2k <0 ,
• tthe mid position is inside or on the ellipse boundary,
• and we select the pixel on the scan line xk+1
Otherwise,
• his midpoint is outside the ellipse and
• the pixel on the scan line xk is closer to the ellipse boundary.