CG Chapter 4
CG Chapter 4
➢ Line 𝑥 = 3 2
➢ Line 𝑦 = 2 0
➢ Point 𝑃 (3, 2) 0 2 4 6
▪ Discrete coordinate
0 positions along the line path are calculated
1 2 3 4
from the equation of the line. 6
0
0 5
Contd.
▪ Screen locations are referenced with integer values.
▪ So plotted positions may only approximate actual line
positions between two specified endpoints. For example
line position of (12.36, 23.87) would be converted to
pixel position (12, 24).
▪ This rounding of coordinate values to integers causes
lines to be displayed with a stair step appearance (“the
Jaggies”).
Line Drawing Algorithms
▪ The Cartesian slop-intercept equation for a straight line is
• 𝒚 = 𝒎𝒙 + 𝒃
• with ‘𝒎’ representing slop and ‘𝒃’ as the intercept.
▪ It is also known as Direct use of Line Equation
▪ It is possible to draw line using this equation but for
efficiency purpose we use different line drawing
algorithm.
• DDA Algorithm
• Bresenham’s Line Algorithm
▪ We can also use this algorithm in parallel if we have
more number of processors.
▪ The slope-intercept equation of a straight line is:
▪ Y=mx+b …………….(i) Where m = slope of line and, b = y-intercept.
Fig: Line path between two end points (x1, y1), and (x2, y2)
▪ Suppose (x1, y1), and (x2, y2) are the two end points of a line
segment as shown in fig above.
y 2 − y1
▪ Then slope (m) = x2 − x1
▪ From above equation (i) becomes,
▪ Therefore b= y- y 2 − y1 x …………………….(ii)
x2 − x1
▪ For any point (xk,yk), equation (i) becomes,
▪ Yk =mxk +b……………. (iii)
▪ For any point (xk+1,yk+1), equation (i) becomes,
▪ yk+1 = mxk+1 +b ……………(iv)
▪ Subtracting equation (iii) from (iv) we get,
▪ yk+1 – yk = (mxk+1 +b) – (mxk +b) ………….(v)
▪ = m(xk+1 -xk )
▪ So, Δy= m.Δx, where Δx and Δy are x and y-increment
respectively.
▪ m = Δy/Δx …………………..(vi)
Introduction to DDA Algorithm
▪ Full form of DDA is Digital Differential Analyzer
▪ DDA is scan conversion line drawing algorithm based on
calculating either ∆𝑦 or ∆𝑥 using line equation.
▪ We sample the line at unit intervals in one coordinate and
find corresponding integer values nearest the line path for
the other coordinate.
▪ Selecting unit interval in either 𝑥 or 𝑦 direction based on
way we process line.
Unit Step Direction in DDA
Algorithm
▪ Processing from left to right.
✓ Slope is “+ve”, & Magnitude is Less than 1
Δ𝑋 = 1
✓ Slope is “-ve”, & Magnitude is Less than 1
✓ Slope is “+ve”, & Magnitude is greater than 1 Δ𝑌 = 1
✓ Slope is “-ve”, & Magnitude is greater than 1 Δ𝑌 = −1
▪ 𝑥1 = 3, 𝑦1 = 𝑦0 + 𝑚 = 2 + 0.5 = 2.5 0 1 2 3 4 5 6 7 8
[Plot the first intermediate point by rounding it to (3, 3)]
▪ 𝑥2 = 4, 𝑦2 = 𝑦1 + 𝑚 = 2.5 + 0.5 = 3
[Plot the second intermediate point by rounding it to (4, 3)]
▪ 𝑥3 = 5, 𝑦3 = 𝑦2 + 𝑚 = 3 + 0.5 = 3.5
[Plot the third intermediate point by rounding it to (5, 4)]
▪ 𝑥4 = 6, 𝑦4 = 4
[Plot End point given]
Example 1: Digitize the line with endpoints (1, 5) and (7, 2) using DDA
algorithm
▪ Here, dx = 7-1=6 and dy = 2-5 = -3
▪ So, steplength = 6 (since abs(dx)>abs(dy))
▪ Therefore, xIncrement = dx/steplength = 6/6 = 1
▪ yIncrement = dy/steplength = -3/6 = -1/2 = -0.5
▪ Based on these values the intermediate pixel calculation is shown in the table below
k xk+1 yk+1 (xk+1, Plot in screen (xk+1, yk+1)
yk+1)
1 2 4.5 (2, 4.5) (2, 5)
2 3 4 (3, 4) (3, 4)
3 4 3.5 (4, 3.5) (4,4)
4 5 3 (5,3) (5,3)
5 6 2.5 (6,2.5) (6,3)
6 7 2 (7,2) (7,2)
▪ Example 2: Digitize the line with endpoints (1, -6) and (4, 4) using DDA algorithm.
▪ Example 3: Digitize the line with endpoints (1, 6), (6, 10) using DDA algorithm.
▪ Example 4: Trace DDA algorithm for line with endpoints (1, 2), (5, 6).
▪ Example 5: Trace DDA algorithm for endpoints (1, 7), (6, 3).
Introduction to Bresenham’s Line
Algorithm
▪ An accurate and efficient raster line-generating algorithm,
developed by Bresenham.
▪ It scan converts line using only incremental integer calculations.
▪ That can be modified to display circles and other curves.
▪ Based on slop we take unit step in one direction and decide pixel
of other direction from two candidate pixel.
▪ If |Δ𝑥| > |Δ𝑦| we sample at unit 𝒙 interval and vice versa.
Line Path & Candidate pixel
▪ Example |ΔX| > |ΔY|, and ΔY is “+ve”. 4
3
➢ Initial point (Xk, Yk) 2
➢ Line path 1
0
➢ Candidate pixels {(Xk+1, Yk), (Xk+1, Yk+1)}
0 1 2 3 4
1. Digitize the line with endpoints (1, -6) and (4, 4) using BSA/BLA
algorithm.
2. Digitize the line with endpoints (1, 6), (6, 10) using BSA algorithm.
3. Trace BSA for line with endpoints (15, 15), (10, 11).
4. Trace BSA for line with endpoints (20, 10), (30, 18).
5. Trace BSA for endpoints (5, 10), (10, 7).
6. Trace BSA for endpoints (20, 30), (30, 22)
▪ Example: Draw line 𝑨𝑩 with coordinates 𝑨(𝟐, 𝟐), 𝑩(𝟔, 𝟒).
▪ Plot left end point 𝐴(2, 2).
▪ Calculate:
• ∆𝑥 = 4,
• ∆𝑦 = 2, 4
3
• 2∆𝑦 = 4, 2
• 2∆𝑦 − 2∆𝑥 = −4, 1
▪ 𝑝0 = 2∆𝑦 − ∆𝑥 = 0 0
Center
(𝑥𝑐 , 𝑦𝑐 )
r
Radius
Properties of Circle- Cartesion
Coordinate
▪ Cartesian coordinates equation :
▪ (𝑥 − 𝑥𝑐 )2 +(𝑦 − 𝑦𝑐 )2 = 𝑟 2
▪ We could use this equation to calculate circular boundary points.
▪ We increment 1 in 𝑥 direction in every steps from 𝑥𝑐 − 𝑟 to 𝑥𝑐 +
𝑟 and calculate corresponding 𝑦 values at each position as:
▪ (𝑥 − 𝑥𝑐 )2 +(𝑦 − 𝑦𝑐 )2 = 𝑟 2
▪ (𝑦 − 𝑦𝑐 )2 = 𝑟 2 − (𝑥 − 𝑥𝑐 )2
▪ 𝑦 − 𝑦𝑐 = ± 𝑟 2 − (𝑥𝑐 − 𝑥)2
▪ y = 𝑦𝑐 ± 𝑟 2 − (𝑥𝑐 − 𝑥)2
Contd.
▪ But this is not best method as it requires more number of
calculations which take more time to execute.
▪ And also spacing between the plotted pixel positions is not
uniform.
(-3, 4) (3, 4)
(-Y, X) (Y, X)
(-4, 3) (4, 3)
(-X, Y) (X, Y)
45𝑂
(-4, -3) (4, -3)
(-X, -Y) (X, -Y)
yk Midpoint
Candidate
yk+1 Pixel
xk xk+1 xk+2
2 2
▪ 𝑝𝑘+1 − 𝑝𝑘 = 𝑥𝑘 + 1 + 1 + 𝑦𝑘+1 − 1
2
− 𝑟 2 − ൬ 𝑥𝑘 + 1 2
+
2
𝑦𝑘 − 1
2
− 𝑟2൰
2
▪ 𝑝𝑘+1 − 𝑝𝑘 = 𝑥𝑘 + 1 + 2 𝑥𝑘 + 1 + 1 + 𝑦𝑘+1 2 − 𝑦𝑘+1 + 14 − 𝑟 2 −
2
𝑥𝑘 + 1 − 𝑦𝑘 2 + 𝑦𝑘 − 14 + 𝑟 2
Contd.
2
▪ 𝑝𝑘+1 − 𝑝𝑘 = 𝑥𝑘 + 1 + 2 𝑥𝑘 + 1 + 1 + 𝑦𝑘+1 2 − 𝑦𝑘+1 + 14 −
𝑟 2 − 𝑥𝑘 + 1 2
− 𝑦𝑘 2 + 𝑦𝑘 − 14 + 𝑟 2
▪ 𝑝𝑘+1 − 𝑝𝑘 = 2 𝑥𝑘 + 1 + 1 + 𝑦𝑘+1 2 − 𝑦𝑘+1 − 𝑦𝑘 2 + 𝑦𝑘
▪ 𝑝𝑘+1 − 𝑝𝑘 = 2 𝑥𝑘 + 1 + (𝑦𝑘+1 2 − 𝑦𝑘 2 ) − (𝑦𝑘+1 −𝑦𝑘 ) + 1
▪ 𝑝𝑘+1 = 𝑝𝑘 + 2 𝑥𝑘 + 1 + (𝑦𝑘+1 2 − 𝑦𝑘 2 ) − (𝑦𝑘+1 −𝑦𝑘 ) + 1
▪ Now we can put 2𝑥𝑘+1 = 2(𝑥𝑘 + 1)
▪ 𝑝𝑘+1 = 𝑝𝑘 + 2𝑥𝑘+1 + (𝑦𝑘+1 2 − 𝑦𝑘 2 ) − (𝑦𝑘+1 −𝑦𝑘 ) + 1
Contd.
▪ 𝑝𝑘+1 = 𝑝𝑘 + 2𝑥𝑘+1 + (𝑦𝑘+1 2 − 𝑦𝑘 2 ) − (𝑦𝑘+1 −𝑦𝑘 ) + 1
▪ In above equation 𝑦𝑘+1 is either 𝑦𝑘 or 𝑦𝑘 − 1 depending on the
sign of the 𝑝𝑘 .
▪ If we select 𝑦𝑘+1 = 𝑦𝑘 .
▪ 𝑝𝑘+1 = 𝑝𝑘 + 2𝑥𝑘+1 + 1
▪ If we select 𝑦𝑘+1 = 𝑦𝑘 – 1.
▪ 𝑝𝑘+1 = 𝑝𝑘 + 2𝑥𝑘+1 + (𝑦𝑘+1 2 − 𝑦𝑘 2 ) − (𝑦𝑘+1 −𝑦𝑘 ) + 1
▪ 𝑝𝑘+1 = 𝑝𝑘 + 2𝑥𝑘+1 + (𝑦𝑘+1 +𝑦𝑘 )(𝑦𝑘+1 −𝑦𝑘 ) − (𝑦𝑘+1 −𝑦𝑘 ) + 1
▪ 𝑝𝑘+1 = 𝑝𝑘 + 2𝑥𝑘+1 + (𝑦𝑘 – 1 + 𝑦𝑘 )(𝑦𝑘 – 1 − 𝑦𝑘 ) − (𝑦𝑘 – 1 −
𝑦𝑘 ) + 1
▪ 𝑝𝑘+1 = 𝑝𝑘 + 2𝑥𝑘+1 + (2𝑦𝑘 – 1)(– 1) − (– 1) + 1
Contd.
▪ 𝑝𝑘+1 = 𝑝𝑘 + 2𝑥𝑘+1 − 2𝑦𝑘 + 1 + 1 + 1
▪ 𝑝𝑘+1 = 𝑝𝑘 + 2𝑥𝑘+1 − (2𝑦𝑘 − 2) + 1
▪ Now put 2𝑦𝑘+1 = 2𝑦𝑘 − 2.
▪ 𝑝𝑘+1 = 𝑝𝑘 + 2𝑥𝑘+1 − 2𝑦𝑘+1 + 1
IDP Midpoint Circle Algorithm
▪ The initial decision parameter(IDP) is obtained by evaluating the
circle function at the start position 𝑥0 , 𝑦0 = (0, 𝑟) as follows.
▪ 𝑝0 = 𝑓𝑐𝑖𝑟𝑐𝑙𝑒 0 + 1, 𝑟 − 12
2 2
▪ 𝑝0 = 1 + 𝑟 − 1
2
− 𝑟2
▪ 𝑝0 = 1 + 𝑟 2 − 𝑟 + 14 − 𝑟 2
▪ 𝑝0 = 54 − 𝑟
▪ 𝑝0 ≈ 1 − 𝑟
Algorithm for Midpoint Circle Generation
1. Input radius r and circle center (𝑥𝑐 , 𝑦𝑐 ), and obtain the first point on the
circumference of a circle centered on the origin as
𝑥0 , 𝑦0 = (0, 𝑟)
2. calculate the initial value of the decision parameter as
𝑝0 = 54 − 𝑟
3. At each 𝑥𝑘 position, starting at 𝑘 = 0, perform the following test:
If 𝑝𝑘 < 0, the next point along the circle centered on (0, 0) is 𝑥𝑘 + 1, 𝑦𝑘 &
𝑝𝑘+1 = 𝑝𝑘 + 2𝑥𝑘+1 + 1
Otherwise, the next point along the circle is 𝑥𝑘 + 1, 𝑦𝑘 − 1 &
𝑝𝑘+1 = 𝑝𝑘 + 2𝑥𝑘+1 + 1 − 2𝑦𝑘+1
Where 2𝑥𝑘+1 = 2𝑥𝑘 + 2, & 2𝑦𝑘+1 = 2𝑦𝑘 − 2.
4. Determine symmetry points in the other seven octants.
5. Move each calculated pixel position (𝑥, 𝑦) onto the circular path centered on
(𝑥𝑐 , 𝑦𝑐 ) and plot the coordinate values:
𝑥 = 𝑥 + 𝑥𝑐 , 𝑦 = 𝑦 + 𝑦𝑐
6. Repeat steps 3 through 5 until 𝑥 ≥ 𝑦.
Example Midpoint Circle
Algorithm
▪ Example: Draw circle with radius 𝑟 = 10, and center of circle is
1, 1 (Only one octant 𝑥 = 0 to 𝑥 = 𝑦 )
▪ First we find pixel position for octant 𝑥 = 0 to 𝑥 = 𝑦 for center
0, 0
𝒌 𝒑𝒌 (𝒙𝒌+𝟏 , 𝒚𝒌+𝟏)
▪ 𝑝0 = 1 − 𝑟 0 -9 (1, 10)
▪ 𝑝0 = 1 − 10 = −9 1 -6 (2, 10)
2 -1 (3, 10)
▪ Now 𝑝0 < 0 we select (1, 10)
3 6 (4, 9)
▪ 𝑝1 = 𝑝0 + 2𝑥1 + 1 4 -3 (5, 9)
▪ 𝑝1 = −9 + 2 + 1 = −6 5 8 (6, 8)
▪ 𝑝3 = −1 + 6 + 1 = 6 4 -3 (5, 9)
5 8 (6, 8)
▪ Now 𝑝3 ≮ 0 we select (4, 9)
6 5 (7, 7)
▪ 𝑝4 = 𝑝3 + 2𝑥4 + 1 − 2𝑦4
▪ 𝑝4 = 6 + 8 + 1 − 18 = −3
▪ Now 𝑝4 < 0 we select (5, 9)
Contd.
▪ 𝑝5 = 𝑝4 + 2𝑥5 + 1 𝒌 𝒑𝒌 (𝒙𝒌+𝟏 , 𝒚𝒌+𝟏)
0 -9 (1, 10)
▪ 𝑝5 = −3 + 10 + 1 = 8
1 -6 (2, 10)
▪ Now 𝑝5 ≮ 0 we select (6, 8) 2 -1 (3, 10)
▪ 𝑝6 = 𝑝5 + 2𝑥6 + 1 − 2𝑦6 3 6 (4, 9)
▪ 𝑝6 = 8 + 12 + 1 − 16 = 5 4 -3 (5, 9)
5 8 (6, 8)
▪ Now 𝑝6 ≮ 0 we select (7, 7)
6 5 (7, 7)
▪ Now Loop exit as 𝑥 ≥ 𝑦, as
▪ in our case 7 ≥ 7
Contd.
▪ Than we calculate pixel position for given center 1, 1 using
equations:
Center (0, 0) Center (1, 1)
▪ 𝑥 = 𝑥 + 𝑥𝑐 = 𝑥 + 1
(1, 10) (2, 11)
▪ 𝑦 = 𝑦 + 𝑦𝑐 = 𝑦 + 1
(2, 10) (3, 11)
(3, 10) (4, 11)
(4, 9) (5, 10)
(5, 9) (6, 10)
(6, 8) (7, 9)
(7, 7) (8, 8)
Contd.
▪ Plot the pixel.
12
▪ First plot initial point. 11
(1, 11) 10
9
Center (1, 1)
8
(2, 11) 7
6
(3, 11)
5
(4, 11) 4
(5, 10) 3
(6, 10) 2
Center
1
(7, 9) (1, 1)
0
(8, 8)
0 1 2 3 4 5 6 7 8 9 10 11
Ellipse
▪ AN ellipse is defined as the set of points such that the sum of the
distances from two fixed positions (foci) is same for all points.
𝑝(𝑥, 𝑦)
𝑑1
𝑑2
𝑓1 𝑓2
Contd.
▪ If we labeled distance from two foci to any point on ellipse
boundary as 𝑑1 and 𝑑2 then the general equation of an ellipse can
be written as:
𝑑1 + 𝑑2 = 𝐶𝑜𝑛𝑠𝑡𝑎𝑛𝑡
▪ Expressing distance in terms of focal coordinates 𝑓1 = 𝑥1 , 𝑦1 and
𝑓2 = 𝑥2 , 𝑦2 we have
▪ 𝑥 − 𝑥1 2 + 𝑦 − 𝑦1 2 + 𝑥 − 𝑥2 2 + 𝑦 − 𝑦2 2 = 𝐶𝑜𝑛𝑠𝑡𝑎𝑛𝑡
[Using Distance formula]
Properties of Ellipse-Specifying
Equations
▪ An interactive method for specifying an ellipse in an arbitrary
orientation is to input
✓ two foci and
✓ a point on the ellipse boundary.
▪ With this three coordinates we can evaluate constant in equation:
𝑥 − 𝑥1 2 + 𝑦 − 𝑦1 2 + 𝑥 − 𝑥2 2 + 𝑦 − 𝑦2 2 = 𝐶𝑜𝑛𝑠𝑡𝑎𝑛𝑡
▪ We can also write this equation in the form
𝐴𝑥 2 + 𝐵𝑦 2 + 𝐶𝑥𝑦 + 𝐷𝑥 + 𝐸𝑦 + 𝐹 = 0
▪ Where the coefficients 𝐴, 𝐵, 𝐶, 𝐷, 𝐸, and 𝐹 are evaluated in terms
of the focal coordinates and the dimensions of the major and
minor axes of the ellipse.
Contd.
▪ Then coefficient in 𝐴𝑥 2 + 𝐵𝑦 2 + 𝐶𝑥𝑦 + 𝐷𝑥 + 𝐸𝑦 + 𝐹 = 0 can be
evaluated and used to generate pixels along the elliptical path.
▪ We can say ellipse is in standard position if their major and minor
axes are parallel to x-axis and y-axis.
▪ Ellipse equation are greatly simplified if we align major and minor
axis with coordinate axes i.e. x-axis and y-axis.
X-axis
Y-axis
Contd.
▪ Equation of ellipse can be written in terms of the ellipse center
coordinates (𝑥𝑐 , 𝑦𝑐 ) and parameters 𝑟𝑥 and 𝑟𝑦 as.
2 2
𝑥 − 𝑥𝑐 𝑦 − 𝑦𝑐
+ =1
𝑟𝑥 𝑟𝑦
▪ Using the polar coordinates r and θ, we can also describe the
ellipse in standard position with the parametric equations:
𝑥 = 𝑥𝑐 + 𝑟𝑥 cos θ
𝑦 = 𝑦𝑐 + 𝑟𝑦 sin θ
𝑟𝑦
𝑟𝑥
(𝑥𝑐 , 𝑦𝑐 )
Properties of Ellipse-Symmetry
▪ Symmetry property further reduced computations.
▪ An ellipse in standard position is symmetric between quadrant.
(−2, 4) (2, 4)
(−𝑥, 𝑦) (𝑥, 𝑦)
𝑟𝑦
𝑟𝑥
(𝑥𝑐 , 𝑦𝑐 )
ry2x2+rx2y2-rx2ry2=0
yk Midpoint
yk-1 Candidate
Pixel
xk xk+1 xk+2
2 2 2 1 2
▪ 𝑝1𝑘 = 𝑟𝑦 (𝑥𝑘 + 1) +𝑟𝑥 𝑦𝑘 − − 𝑟𝑥 2 𝑟𝑦 2
2
▪ If 𝑝1𝑘 < 0, the midpoint is inside the ellipse and the pixel on scan
line 𝑦𝑘 is closer to ellipse boundary
▪ Otherwise the midpoint is outside or on the ellipse boundary and
we select the pixel 𝑦𝑘 − 1.
Contd.
▪ At the next sampling position decision parameter for region 1 is
evaluated as.
1
▪ 𝑝1𝑘+1 = 𝑓𝑒𝑙𝑙𝑖𝑝𝑠𝑒 𝑥𝑘+1 + 1, 𝑦𝑘+1 −
2
2 2 2 1 2
▪ 𝑝1𝑘+1 = 𝑟𝑦 𝑥𝑘 + 1 + 1 + 𝑟𝑥 𝑦𝑘+1 − − 𝑟𝑥 2 𝑟𝑦 2
2
▪ Now subtract 𝑝1𝑘 from 𝑝1𝑘+1
2 2 2 1 2
▪ 𝑝1𝑘+1 − 𝑝1𝑘 = 𝑟𝑦 𝑥𝑘 + 1 + 1 + 𝑟𝑥 𝑦𝑘+1 − − 𝑟𝑥 2 𝑟𝑦 2 −
2
2 2 2 1 2
𝑟𝑦 (𝑥𝑘 + 1) −𝑟𝑥 𝑦𝑘 − + 𝑟𝑥 2 𝑟𝑦 2
2
Contd.
2 2 2 1 2
▪ 𝑝1𝑘+1 − 𝑝1𝑘 = 𝑟𝑦 𝑥𝑘 + 1 + 1 + 𝑟𝑥 𝑦𝑘+1 − −
2
2 2 2 1 2
𝑟𝑦 (𝑥𝑘 + 1) −𝑟𝑥 𝑦𝑘 −
2
▪ 𝑝1𝑘+1 − 𝑝1𝑘 = 𝑟𝑦 2 𝑥𝑘 + 1 2
+ 2𝑟𝑦 2 𝑥𝑘 + 1 + 𝑟𝑦 2 + 𝑟𝑥 2 ቀ𝑦𝑘+1 −
1 2 2 2 2 1 2
ቁ − 𝑟𝑦 (𝑥𝑘 + 1) −𝑟𝑥 𝑦𝑘 −
2 2
2 2 2 1 2 1 2
▪ 𝑝1𝑘+1 − 𝑝1𝑘 = 2𝑟𝑦 𝑥𝑘 + 1 + 𝑟𝑦 + 𝑟𝑥 𝑦𝑘+1 − − 𝑦𝑘 −
2 2
2 2 2 1 2 1 2
▪ 𝑝1𝑘+1 = 𝑝1𝑘 + 2𝑟𝑦 𝑥𝑘 + 1 + 𝑟𝑦 + 𝑟𝑥 𝑦𝑘+1 − − 𝑦𝑘 −
2 2
Contd.
2 2 2 1 2
▪ 𝑝1𝑘+1 = 𝑝1𝑘 + 2𝑟𝑦 𝑥𝑘 + 1 + 𝑟𝑦 + 𝑟𝑥 𝑦𝑘+1 − −
2
1 2
𝑦𝑘 − ൨
2
2 2 2 1 2
▪ 𝑝10 = 𝑟𝑦 (1) +𝑟𝑥 𝑟𝑦 − − 𝑟𝑥 2 𝑟𝑦 2
2
2 2 1 2
▪ 𝑝10 = 𝑟𝑦 + 𝑟𝑥 𝑟𝑦 − − 𝑟𝑥 2 𝑟𝑦 2
2
1
▪ 𝑝10 = 𝑟𝑦 − 𝑟𝑥 𝑟𝑦 + 𝑟𝑥 2
2 2
4
Midpoint between Candidate pixel in
Region 2
▪ Now we similarly calculate over region-2.
▪ Unit stepping in negative 𝑦 direction and the midpoint is now
taken between horizontal pixels at each step.
ry2x2+rx2y2-rx2ry2=0
yk Midpoint
yk-1 Candidate
Pixel
xk xk+1 xk+2
▪ For this region, the decision parameter is evaluated as follows.
1
▪ 𝑝2𝑘 = 𝑓𝑒𝑙𝑙𝑖𝑝𝑠𝑒 𝑥𝑘 + , 𝑦𝑘 − 1
2
Derivation for Region 2
1
▪ 𝑝2𝑘 = 𝑓𝑒𝑙𝑙𝑖𝑝𝑠𝑒 𝑥𝑘 + , 𝑦𝑘 − 1
2
2 1 2
▪ 𝑝2𝑘 = 𝑟𝑦 𝑥𝑘 + + 𝑟𝑥 2 𝑦𝑘 − 1 2
− 𝑟𝑥 2 𝑟𝑦 2
2
▪ If 𝑝2𝑘 > 0 the midpoint is outside the ellipse boundary, and we
select the pixel at 𝑥𝑘 .
▪ If 𝑝2𝑘 ≤ 0 the midpoint is inside or on the ellipse boundary and
we select 𝑥𝑘 + 1.
Contd.
▪ At the next sampling position decision parameter for region 2 is
evaluated as.
1
▪ 𝑝2𝑘+1 = 𝑓𝑒𝑙𝑙𝑖𝑝𝑠𝑒 𝑥𝑘+1 + , 𝑦𝑘+1 − 1
2
2 1 2
▪ 𝑝2𝑘+1 = 𝑟𝑦 𝑥𝑘+1 + + 𝑟𝑥 2 𝑦𝑘 − 1 − 1 2
− 𝑟𝑥 2 𝑟𝑦 2
2
▪ Now subtract 𝑝2𝑘 from 𝑝2𝑘+1
2 1 2
▪ 𝑝2𝑘+1 − 𝑝2𝑘 = 𝑟𝑦 𝑥𝑘+1 + + 𝑟𝑥 2 𝑦𝑘 − 1 − 1 2
− 𝑟𝑥 2 𝑟𝑦 2 −
2
2 1 2
𝑟𝑦 𝑥𝑘 + − 𝑟𝑥 2 𝑦𝑘 − 1 2
+ 𝑟𝑥 2 𝑟𝑦 2
2
Contd.
2 1 2
▪ 𝑝2𝑘+1 − 𝑝2𝑘 = 𝑟𝑦 𝑥𝑘+1 + + 𝑟𝑥 2 𝑦𝑘 − 1 − 1 2
− 𝑟𝑥 2 𝑟𝑦 2 −
2
2 1 2
𝑟𝑦 𝑥𝑘 + − 𝑟𝑥 2 𝑦𝑘 − 1 2
+ 𝑟𝑥 2 𝑟𝑦 2
2
2 1 2
▪ 𝑝2𝑘+1 − 𝑝2𝑘 = 𝑟𝑦 𝑥𝑘+1 + + 𝑟𝑥 2 𝑦𝑘 − 1 2
− 2𝑟𝑥 2 𝑦𝑘 − 1 +
2
2 2 1 2
𝑟𝑥 − 𝑟𝑦 𝑥𝑘 + − 𝑟𝑥 2 𝑦𝑘 − 1 2
2
2 1 2 2 2 2 1 2
▪ 𝑝2𝑘+1 − 𝑝2𝑘 = 𝑟𝑦 𝑥𝑘+1 + − 2𝑟𝑥 𝑦𝑘 − 1 + 𝑟𝑥 − 𝑟𝑦 𝑥𝑘 +
2 2
2 2 2 1 2 1 2
▪ 𝑝2𝑘+1 − 𝑝2𝑘 = −2𝑟𝑥 𝑦𝑘 − 1 + 𝑟𝑥 + 𝑟𝑦 𝑥𝑘+1 + − 𝑥𝑘 +
2 2
Contd.
2 2 2 1 2
▪ 𝑝2𝑘+1 − 𝑝2𝑘 = −2𝑟𝑥 𝑦𝑘 − 1 + 𝑟𝑥 + 𝑟𝑦 𝑥𝑘+1 + −
2
1 2
𝑥𝑘 + ൨
2
2 2 2 1 2
▪ 𝑝2𝑘+1 = 𝑝2𝑘 − 2𝑟𝑥 𝑦𝑘 − 1 + 𝑟𝑥 + 𝑟𝑦 𝑥𝑘+1 + −
2
1 2
𝑥𝑘 + ൨
2
1 2
▪ 𝑝20 = 𝑟𝑦 2 𝑥0 + + 𝑟𝑥 2 𝑦0 − 1 2
− 𝑟𝑥 2 𝑟𝑦 2
2
▪ For simplify calculation of 𝑝20 we could also select pixel position
in counterclockwise order starting at (𝑟𝑥 , 0).
Algorithm for Midpoint Ellipse Generation
1. Input 𝑟𝑥 , 𝑟𝑦 and ellipse center (𝑥𝑐 , 𝑦𝑐 ), and obtain the first point on an ellipse
centered on the origin as
𝑥0 , 𝑦0 = (0, 𝑟𝑦 )
7. Move each calculated pixel position (𝑥, 𝑦) onto the elliptical path centered on
(𝑥𝑐 , 𝑦𝑐 ) and plot the coordinate values:
𝑥 = 𝑥 + 𝑥𝑐 , 𝑦 = 𝑦 + 𝑦𝑐
2 1 2 1 2
6 8+ − 7+ = 361
2 2
2 1 2 1 2
6 8+ − 8+ = 297
2 2
Scan line
Contd.
▪ Scan line intersects at vertex are required special handling.
▪ For vertex we must look at the other endpoints of the two line
segments which meet at this vertex.
• If these points lie on the same (up or down) side of the scan line, then that
point is counts as two intersection points.
• If they lie on opposite sides of the scan line, then the point is counted as
single intersection.
Scan line
Scan line
Scan line
Edge Intersection Calculation with
Scan-Line
▪ Coherence methods often involve incremental calculations applied
along a single scan line or between successive scan lines.
▪ In determining edge intersections, we can set up incremental
coordinate calculations along any edge by exploiting the fact that
the slope of the edge is constant from one scan line to the next.
▪ For above figure we can write slope equation for polygon
boundary as follows.
𝑦𝑘+1 −𝑦𝑘
▪ 𝑚=
𝑥𝑘+1 −𝑥𝑘
▪ Since change in 𝑦 coordinates between the two scan lines is
simply
▪ 𝑦𝑘+1 − 𝑦𝑘 = 1
Contd.
▪ So slope equation can be modified as follows
𝑦𝑘+1 −𝑦𝑘
▪ 𝑚=
𝑥𝑘+1 −𝑥𝑘
1
▪ 𝑚=
𝑥𝑘+1 −𝑥𝑘
1
▪ 𝑥𝑘+1 − 𝑥𝑘 =
𝑚
1
▪ 𝑥𝑘+1 = 𝑥𝑘 +
𝑚
▪ Each successive 𝑥 intercept can thus be calculated by adding the
inverse of the slope and rounding to the nearest integer.
Edge Intersection Calculation with Scan-Line for parallel
execution
▪ For parallel execution of this algorithm we assign each scan line to
separate processor in that case instead of using previous 𝑥 values
for calculation we use initial 𝑥 values by using equation as.
𝑘
▪ 𝑥𝑘 = 𝑥0 +
𝑚
∆𝑦
▪ Now if we put 𝑚 = in incremental calculation equation 𝑥𝑘+1 =
∆𝑥
1
𝑥𝑘 + then we obtain equation as.
𝑚
∆𝑥
▪ 𝑥𝑘+1 = 𝑥𝑘 +
∆𝑦
▪ Using this equation we can perform integer evaluation of 𝑥
intercept.
Simplified Method for Edge Intersection Calculation with
Scan-Line
1. Suppose 𝑚 = 7/3
2. Initially, set counter to 0, and increment to 3 (which is Δ𝑥).
3. When move to next scan line, increment counter by adding ∆𝑥
4. When counter is equal to or greater than 7 (which is Δ𝑦),
increment the 𝑥 − 𝑖𝑛𝑡𝑒𝑟𝑐𝑒𝑝𝑡 (in other words, the 𝑥 − 𝑖𝑛𝑡𝑒𝑟𝑐𝑒𝑝𝑡
for this scan line is one more than the previous scan line), and
decrement counter by 7(which is ∆𝑦). ∆𝑥 = 3, ∆𝑦 = 7
Counter=3
Counter=2
Counter=4
Counter=5
Counter=6
Counter=1
Counter=0
𝑦0
𝑥0
Use of Sorted Edge table in Scan-Line Polygon Fill
Algorithm
▪ To efficiently perform a polygon fill, we can first store the polygon
boundary in a sorted edge table.
▪ It contains all the information necessary to process the scan lines
efficiently.
▪ We use bucket sort to store the edge sorted on the smallest 𝑦
value of each edge in the correct scan line positions.
▪ Only the non-horizontal edges are entered into the sorted edge
table.
Contd.
Scan Line
Number
𝑦𝐶 𝑦𝐵 𝑥𝐶 1/𝑚𝐶𝐵
B .
.
.
𝑦𝐷 𝑦𝐶 𝑥𝐷 1/𝑚𝐷𝐶
.
C Scan Line 𝑦𝐶 . 𝑦𝐸 𝑥𝐷 1/𝑚𝐷𝐸
.
C E
Scan Line 𝑦𝐷 𝑦𝐴 𝑦𝐸 𝑥𝐴 1/𝑚𝐴𝐸
D .
Scan Line 𝑦𝐴 .
. 𝑦𝐵 𝑥𝐴 1/𝑚𝐴𝐵
A
1
0
Inside-Outside Tests
▪ In area filling and other graphics operation often required to find
particular point is inside or outside the polygon.
▪ For finding which region is inside or which region is outside most
graphics package use either
1. Odd even rule OR
2. Nonzero winding number rule
Odd Even/ Odd Parity/ Even
Odd Rule
▪ By conceptually drawing a line from any position 𝑝 to a distant
point outside the coordinate extents of the object.
▪ Than counting the number of edges crossing by this line.
1. If Edge count is odd, than p is an interior point.
2. Otherwise p is exterior point.
▪ To obtain accurate edge count we must sure that line selected is
does not pass from any vertices. Boundary of Screen
1 2
𝑝
𝑟
1
𝑞
Nonzero Winding Number Rule
▪ This method counts the number of times the polygon edges wind
around a particular point in the counterclockwise direction.
▪ This count is called the winding number.
▪ We apply this rule by initializing winding number with 0.
▪ Then draw a line for any point 𝑝 to distant point beyond the
coordinate extents of the object.
Boundary of Screen
Winding number=0
𝑝
Contd.
▪ The line we choose must not pass through vertices.
▪ Then we move along that line we find number of intersecting
edges.
1. If edge cross our line from right to left We add 1 to winding number
2. Otherwise subtract 1 from winding number
▪ IF the final value of winding number is nonzero then the point is
interior otherwise point is exterior. Boundary of Screen
-1 -1
Winding number=+1-1=0
number=0
number=-1
𝑝
𝑟
+1
𝑞
Comparison between Odd Even Rule and Nonzero
Winding Rule
▪ For standard polygons and simple object both rule gives same
result but for more complicated shape both rule gives different
result.
Seed
Boundary / Edge Fill Algorithm
Procedure:
boundary-fill4(x, y, f-colour, b-colour)
{
if(getpixel (x,y) ! = b-colour && gepixel (x, y) ! = f-colour)
{
putpixel (x, y, f-colour)
boundary-fill4(x + 1, y, f-colour, b-colour);
boundary-fill4(x, y + 1, f-colour, b-colour);
boundary-fill4(x - 1, y, f-colour, b-colour);
boundary-fill4(x, y - l, f-colour, b-colour);
}
}
Problem of Staking and Efficient
Method
▪ Same procedure can be modified according to 8 connected region
algorithm by including four additional statements to test diagonal
positions.
▪ This procedure requires considerable stacking of neighbouring
points more, efficient methods are generally employed.
▪ Efficient method fill horizontal pixel spans across scan lines,
instead of proceeding to 4 connected or 8 connected
neighbouring points.
▪ Then we need only stack a beginning position for each horizontal
pixel span, instead of stacking all unprocessed neighbouring
positions around the current position.
▪ Starting from the initial interior point with this method, we first fill
in the contiguous span of pixels on this starting scan line.
Contd.
▪ Then we locate and stack starting positions for spans on the
adjacent scan lines.
▪ Spans are defined as the contiguous horizontal string of positions
bounded by pixels displayed in the area border colour.
▪ At each subsequent step, we unstack the next start position and
repeat the process.
▪ For e.g.
Contd.
3
2
(a) (b)
1 2 1 3
1 1
5 6 5
6
(c) 4 5 (d) 4 5
1 4 1 4
1 1
Introduction to Flood-Fill
Algorithm
▪ Sometimes it is required to fill in an area that is not defined within
a single colour boundary.
▪ In such cases we can fill areas by replacing a specified interior
colour instead of searching for a boundary colour.
▪ This approach is called a flood-fill algorithm. Like boundary fill
algorithm, here we start with some seed and examine the
neighbouring pixels.
▪ However, here pixels are checked for a specified interior colour
instead of boundary colour and they are replaced by new colour.
▪ Using either a 4-connected or 8-connected approach, we can step
through pixel positions until all interior point have been filled.
Flood-Fill Algorithm
Procedure :
flood-fill4(x, y, new-colour, old-colour)
{
if(getpixel (x,y) = = old-colour)
{
putpixel (x, y, new-colour)
flood-fill4 (x + 1, y, new-colour, old -colour);
flood-fill4 (x, y + 1, new -colour, old -colour);
flood-fill4 (x - 1, y, new -colour, old -colour);
flood-fill4 (x, y - l, new -colour, old-colour);
}
}
Character Generation
▪ We can display letters and numbers in variety of size and style.
▪ The overall design style for the set of character is called typeface.
▪ Today large numbers of typefaces are available for computer
application for example Helvetica, Arial etc.
▪ Originally, the term font referred to a set of cast metal character
forms in a particular size and format.
▪ Example: 10-point Courier Italic or 12- point Palatino Bold.
Contd.
▪ Now, the terms font and typeface are often used interchangeably,
since printing is no longer done with cast metal forms.
▪ Methods of character generation are:
➢ Bitmap Font/ Bitmapped Font
➢ Outline Font
➢ Stroke Method
➢ Starbust Method
Bitmap Font/ Bitmapped Font
▪ A simple method for representing the 0 0 1 1 1 1 0 0
0 1 1 1 1 1 1 0
character shapes in a particular typeface is to 1 1 0 0 0 0 1 1
use rectangular grid patterns. 1 1 0 0 0 0 1 1
1 1 1 1 1 1 1 1
▪ In frame buffer, the 1 bits designate which 1 1 1 1 1 1 1 1
pixel positions are to be displayed on the 1 1 0 0 0 0 1 1
1 1 0 0 0 0 1 1
monitor.
▪ Bitmap fonts are the simplest to define and display.
▪ Bitmap fonts require more space.
▪ It is possible to generate different size and other variation from
one set but this usually does not produce good result.
Outline Font
▪ In this method character is generated using curve
section and straight line as combine assembly.
▪ To display the character we need to fill interior
region of the character.
▪ This method requires less storage since each
variation does not required a distinct font cache.
▪ We can produce boldface, italic, or different sizes
by manipulating the curve definitions for the
character outlines.
▪ But this will take more time to process the outline
fonts, because they must be scan converted into
the frame buffer.
Stroke Method
• It uses small line segments to generate
a character.
• The small series of line segments are
drawn like a stroke of a pen to form a
character.
• We can generate our own stroke method by calling line
drawing algorithm.
• Here it is necessary to decide which line segments are
needs for each character and then draw that line to
display character.
• It support scaling by changing length of line segment.
Starbust Method
▪ In this method a fix pattern of lines (24 line) segments are used to
generate characters. 03 04
13 14
02 23 05
17 18
01 06
21
12 22 07
20
24 19
11 08
16 15
10 09
2 Dashed
3 Dotted
4 Dotdash
▪ We modify a line –drawing algorithm to generate such lines by setting
the length and spacing of displayed solid sections along the line path.
▪ To set line type attributes in a PHIGS application program, a user invokes
the function: setLinetype(It)
▪ Where parameter lt is assigned a positive integer value of 1, 2, 3, 4… etc.
to generate lines that are, respectively solid, dashed, dotted, or dotdash
etc.
Line Width
▪ Implementation of line-width options depends on the capabilities
of the output device.
▪ A heavy line on a video monitor could be displayed as adjacent
parallel lines, while a pen plotter might require pen changes.
▪ To set line width attributes in a PHIGS application program, a user
invokes the function: setLinewidthScalFactor (lw)
▪ Line-width parameter lw is assigned a positive number to indicate
the relative width of the line to be displayed.
▪ Values greater than 1 produce lines thicker than the standard line
width and values less than the 1 produce line thinner than the
standard line width.
Contd.
▪ In raster graphics we generate thick line by plotting
• above and below pixel of line path when slope |m|<1. &
• left and right pixel of line path when slope |m|>1.
Line Width at Endpoints and
Join
▪ As we change width of the line we can also change line end and
join of two lines which are shown below
255
Greyscale
▪ With monitors that have no color capability, color function can be
used in an application program to set the shades of grey
(greyscale) for display primitives.
▪ Numeric values between 0-to-1 can be used to specify greyscale
levels.
▪ This numeric values is converted in binary code for store in raster
system. Example: frame buffer with 2 bits per pixel.
Intensity Code Stored Intensity Values In The Displayed Greyscale
Frame Buffer Binary Code
0.0 0 00 Black
0.33 1 01 Dark grey
0.67 2 10 Light grey
1.0 3 11 White
Area-Fill Attributes
▪ For filling any area we have choice between solid colors or pattern
to fill all these are include in area fill attributes. Which are:
• Fill Styles
• Pattern Fill
• Soft Fill
Fill Styles
▪ Area are generally displayed with three basic style.
1. hollow with color border
2. filled with solid color
3. filled with some design
▪ In PHIGS package fill style is selected by following function:
setInteriorStyle (fs)
▪ Value of fs include hollow ,solid, pattern etc.
Contd.
▪ Another values for fill style is hatch, which is patterns of line like
parallel line or crossed line.