Computer Graphics Line drawing Algorithms soham jisitgmail com

  • Slides: 26
Download presentation
Computer Graphics: Line drawing Algorithms soham. jisit@gmail. com 1/23/2022 1

Computer Graphics: Line drawing Algorithms soham. jisit@gmail. com 1/23/2022 1

Draw a Straight Line in MS-Paint Now increase Line Width

Draw a Straight Line in MS-Paint Now increase Line Width

Is It a Stair-step or what! Pixels are discrete, they belong to Computer’s Display

Is It a Stair-step or what! Pixels are discrete, they belong to Computer’s Display Unit Why did these happen? • It is not a continuous Line Coordinates are Continuous

Into The Straight Line c intercept on Y-axis

Into The Straight Line c intercept on Y-axis

q m=0 the Line is parallel to X-axis q m=∞ the Line is parallel

q m=0 the Line is parallel to X-axis q m=∞ the Line is parallel to Y-axis q m=1 the line is inclined at 45 degree Note: See-Saw Steps are less in 3 cases above

Because, Pixels are Discrete ∴ Let is Plot now! x y 0 1 2

Because, Pixels are Discrete ∴ Let is Plot now! x y 0 1 2 3 4 Decision? 2 Exactly 2 3. 732 5. 464 7. 196 8. 928 3. 8 (Impossible) or 4? 5 or 5. 5 (Impossible) 7. 2 (Impossible) or 7 9 Or 9. 3 (Impossible) Thus, plotting on Graph is not accurate For, pixels are discrete while points are not!

Line drawing Algorithm-1: DDA

Line drawing Algorithm-1: DDA

Case -1 : |m| ≤ 1 Switch roles of x and y for |m|

Case -1 : |m| ≤ 1 Switch roles of x and y for |m| >1

DDA: Easier and Generic Approach Plotting a Pixel Differs across Programming languages

DDA: Easier and Generic Approach Plotting a Pixel Differs across Programming languages

Do Yourself: Deduction from DDA Algorithm

Do Yourself: Deduction from DDA Algorithm

Implementing DDA: A sample Turbo C-Code This is need for Graphics in Turbo C

Implementing DDA: A sample Turbo C-Code This is need for Graphics in Turbo C This is need for mathematical functions Common to standard Graphics programs Ø initgraph(int* graphics. Driver , int* graphics. Mode, char* driver. Path. As. String): starts graphics system Ø DETECT: is a macro whose value is 0: indicates to detect graphics system automatically soham. jisit@gmail. com 1/23/2022 12

Implementing DDA: A sample Turbo C-Code This is need for Graphics in Turbo C

Implementing DDA: A sample Turbo C-Code This is need for Graphics in Turbo C This is need for mathematical functions putpixel(int x. Coord, int y. Coord, int color. As. Int) soham. jisit@gmail. com 1/23/2022 13

Draw back of DDA Algorithm 1. Stair –case Output is not optimized considerably 2.

Draw back of DDA Algorithm 1. Stair –case Output is not optimized considerably 2. Ceil() or Floor() : Rounding-off is not dynamically decided 3. Floating point Arithmetic is an overhead 4. Rounding-off is another computational overhead, too! It also yields low accuracy soham. jisit@gmail. com 1/23/2022 14

Bresenham’s Line Drawing Algorithm Given two End Points (X 1, Y 1) and (X

Bresenham’s Line Drawing Algorithm Given two End Points (X 1, Y 1) and (X 2, Y 2) Assumption: X 1 < X 2 ∴ (X 1, Y 1) is to the left y= m*x + c y. K= m*x K + c Increment of X happens from left to right ∴ x. K+1 = x K +1 But YK+1 =? Should be: y. K+1= m*(x. K+1) + c But it is often a fraction!!! So, YK+1 is either YK or (YK +1) Original value of YK+1 But it is neither y. K nor (y. K +1) D 1 = y. True – y. K D 2 = (YK +1) – y. True IF (D 1 < D 2 ) YK+1 = YK Else YK+1 = YK + 1 Bresenhams’ dynamically optimizes it 1/23/2022 15

Bresenhams Line Drawing Algorithm D 1 = y True (K+1) – y. K D

Bresenhams Line Drawing Algorithm D 1 = y True (K+1) – y. K D 2 = (YK +1) – y True(K +1) y= m*x + c y True-(K+1)= m* x K+1 + c 1/23/2022 16

Bresenham’s Line Drawing Algorithm p 0 =? y= m*x + c y True-(K+1)= m*

Bresenham’s Line Drawing Algorithm p 0 =? y= m*x + c y True-(K+1)= m* x K+1 + c 1/23/2022 This is the foundation of Bresenham’s Algorithm 17

Bresenhams Line Drawing Algorithm This is Bresenhem’s Algorithm 1/23/2022 18

Bresenhams Line Drawing Algorithm This is Bresenhem’s Algorithm 1/23/2022 18

Implementation in Turbo C When do you get p. K = 0 ? Browse

Implementation in Turbo C When do you get p. K = 0 ? Browse any study material (maybe, on Internet ) and tell this old teacher “How Bresenham’s Algorithm is different from DDA Algorithm” soham. jisit@gmail. com 1/23/2022 19

Everything is not Straight: Circles Ahead x 2 + y 2= r 2 Example:

Everything is not Straight: Circles Ahead x 2 + y 2= r 2 Example: (3, 4) is a point on a circle whose center is the origin (0, 0) and radius r= 5 unit x 2 + y 2= r 2 ∵ 3 2 + 4 2= 5 2 Again, (4, 3) is another point on the same circle! ∵ 42 + 32= 52 ∴ Interestingly, the same Quadrant, there exists to isometric points, (a, b) and (b, a) on the same Circle! ∴ There is 8 way Isometry in all 4 Quadrants This is very useful to ease drawing a Circle Generate points for only one Quadrant And use 8 -way Isometry for rest 3 quadrants soham. jisit@gmail. com 8 way Isometry in all 4 Quadrants 1/23/2022 20

Ready-On your mark-… Mid-Point Circle Algorithm Next Point XK +1 , YK XK ,

Ready-On your mark-… Mid-Point Circle Algorithm Next Point XK +1 , YK XK , YK 6 Which point should I plot next? This one ? (XK +1 , YK) IT lies outside the circle Then, go for (XK , YK -1) ? 5 XK , YK-1 4 3 1 2 3 4 It’s a fraction value!

Mid-Point Circle Algorithm f( x, y)= x 2 +y 2 –r 2 A point

Mid-Point Circle Algorithm f( x, y)= x 2 +y 2 –r 2 A point ( a, b) lies outside, on or inside the circle according as f( a, b)( x, y)= ⋛ 0 XK +1 , YK XK , YK 6 Mid point ü q Ø • v v 5 Kth (XK , YK) is the point We are in the 1 st Quadrant We move from left to right ∴ XK+1 = X K + 1 But YK = ? Is it YK or YK+1 X K , Y K- 1 4 3 1 2 3 4

Mid-Point Circle Algorithm f( x, y)= x 2 +y 2 –r 2 XK +1

Mid-Point Circle Algorithm f( x, y)= x 2 +y 2 –r 2 XK +1 , YK XK , YK 6 Mid point ü q Ø • v v 5 Kth (XK , YK) is the point We are in the 1 st Quadrant We move from left to right ∴ XK+1 = X K + 1 But YK = ? Is it YK or YK+1 X K , Y K- 1 4 3 1 2 3 4

Mid-Point Circle Algorithm The same can be But pas 0 =? written

Mid-Point Circle Algorithm The same can be But pas 0 =? written

Circle Drawing Algorithm: For one region K=0 XK =0 , YK = r p.

Circle Drawing Algorithm: For one region K=0 XK =0 , YK = r p. K <0? XK+1 = XK + 1 YK+1 = YK K=K+1 XK <YK ? soham. jisit@gmail. com XK+1 = XK + 1 YK+1 = YK -1 1/23/2022 25

Circle Drawing Algorithm: For one region Shift origin from (0, 0) to (xc, yc)

Circle Drawing Algorithm: For one region Shift origin from (0, 0) to (xc, yc) 1/23/2022 soham. jisit@gmail. com 26