![Page 1: Line Drawing Algorithm - University of California, Berkeleyinst.eecs.berkeley.edu/~cs184/fa06/lectures/line-drawing.pdfFrom Prof. James O' Brien CS184 Sp06 notes: Scan Conversion 2](https://reader031.vdocument.in/reader031/viewer/2022030608/5ad6e3987f8b9a9d5c8b68fb/html5/thumbnails/1.jpg)
From Prof. James O' Brien CS184 Sp06notes: Scan Conversion
1
1
Line Drawing Algorithm
• Modified from Prof. James O’ Brien’slecture note on Scan Conversion for CS184SP06
2
Drawing a Line
![Page 2: Line Drawing Algorithm - University of California, Berkeleyinst.eecs.berkeley.edu/~cs184/fa06/lectures/line-drawing.pdfFrom Prof. James O' Brien CS184 Sp06 notes: Scan Conversion 2](https://reader031.vdocument.in/reader031/viewer/2022030608/5ad6e3987f8b9a9d5c8b68fb/html5/thumbnails/2.jpg)
From Prof. James O' Brien CS184 Sp06notes: Scan Conversion
2
3
Drawing a Line
4
Drawing a Line
Some things to consider
How thick are lines?
How should they join up?
Which pixels are the right ones?
For example:
![Page 3: Line Drawing Algorithm - University of California, Berkeleyinst.eecs.berkeley.edu/~cs184/fa06/lectures/line-drawing.pdfFrom Prof. James O' Brien CS184 Sp06 notes: Scan Conversion 2](https://reader031.vdocument.in/reader031/viewer/2022030608/5ad6e3987f8b9a9d5c8b68fb/html5/thumbnails/3.jpg)
From Prof. James O' Brien CS184 Sp06notes: Scan Conversion
3
5
Drawing a Line
InclusiveEndpoints
6
Drawing a Line
![Page 4: Line Drawing Algorithm - University of California, Berkeleyinst.eecs.berkeley.edu/~cs184/fa06/lectures/line-drawing.pdfFrom Prof. James O' Brien CS184 Sp06 notes: Scan Conversion 2](https://reader031.vdocument.in/reader031/viewer/2022030608/5ad6e3987f8b9a9d5c8b68fb/html5/thumbnails/4.jpg)
From Prof. James O' Brien CS184 Sp06notes: Scan Conversion
4
7
Drawing a Line
x=x1y=y1while(x<=x2) plot(x,y) x++ y+=Dy
8
Drawing a Line
After rounding
![Page 5: Line Drawing Algorithm - University of California, Berkeleyinst.eecs.berkeley.edu/~cs184/fa06/lectures/line-drawing.pdfFrom Prof. James O' Brien CS184 Sp06 notes: Scan Conversion 2](https://reader031.vdocument.in/reader031/viewer/2022030608/5ad6e3987f8b9a9d5c8b68fb/html5/thumbnails/5.jpg)
From Prof. James O' Brien CS184 Sp06notes: Scan Conversion
5
9
Drawing a Line
Accumulation ofroundoff errors
How slow is float-to-int conversion?
10
Drawing a Line
![Page 6: Line Drawing Algorithm - University of California, Berkeleyinst.eecs.berkeley.edu/~cs184/fa06/lectures/line-drawing.pdfFrom Prof. James O' Brien CS184 Sp06 notes: Scan Conversion 2](https://reader031.vdocument.in/reader031/viewer/2022030608/5ad6e3987f8b9a9d5c8b68fb/html5/thumbnails/6.jpg)
From Prof. James O' Brien CS184 Sp06notes: Scan Conversion
6
11
Drawing a Linevoid drawLine-Error1(int x1,x2, int y1,y2)
float m = float(y2-y1)/(x2-x1) int x = x1 float y = y1
while (x <= x2)
setPixel(x,round(y),PIXEL_ON)
x += 1 y += m
Not exact math
Accumulates errors
12
No more rounding
Drawing a Linevoid drawLine-Error2(int x1,x2, int y1,y2)
float m = float(y2-y1)/(x2-x1) int x = x1 int y = y1 float e = 0.0
while (x <= x2)
setPixel(x,y,PIXEL_ON)
x += 1 e += m if (e >= 0.5) y+=1 e-=1.0
![Page 7: Line Drawing Algorithm - University of California, Berkeleyinst.eecs.berkeley.edu/~cs184/fa06/lectures/line-drawing.pdfFrom Prof. James O' Brien CS184 Sp06 notes: Scan Conversion 2](https://reader031.vdocument.in/reader031/viewer/2022030608/5ad6e3987f8b9a9d5c8b68fb/html5/thumbnails/7.jpg)
From Prof. James O' Brien CS184 Sp06notes: Scan Conversion
7
13
Drawing a Linevoid drawLine-Error3(int x1,x2, int y1,y2)
int x = x1 int y = y1 float e = -0.5
while (x <= x2)
setPixel(x,y,PIXEL_ON)
x += 1 e += float(y2-y1)/(x2-x1) if (e >= 0.0) y+=1 e-=1.0
14
Drawing a Linevoid drawLine-Error4(int x1,x2, int y1,y2)
int x = x1 int y = y1 float e = -0.5*(x2-x1) // was -0.5
while (x <= x2)
setPixel(x,y,PIXEL_ON)
x += 1 e += y2-y1 // was /(x2-x1) if (e >= 0.0) // no change y+=1 e-=(x2-x1) // was 1.0
![Page 8: Line Drawing Algorithm - University of California, Berkeleyinst.eecs.berkeley.edu/~cs184/fa06/lectures/line-drawing.pdfFrom Prof. James O' Brien CS184 Sp06 notes: Scan Conversion 2](https://reader031.vdocument.in/reader031/viewer/2022030608/5ad6e3987f8b9a9d5c8b68fb/html5/thumbnails/8.jpg)
From Prof. James O' Brien CS184 Sp06notes: Scan Conversion
8
15
Drawing a Linevoid drawLine-Error5(int x1,x2, int y1,y2)
int x = x1 int y = y1 int e = -(x2-x1) // removed *0.5
while (x <= x2)
setPixel(x,y,PIXEL_ON)
x += 1 e += 2*(y2-y1) // added 2* if (e >= 0.0) // no change y+=1 e-=2*(x2-x1) // added 2*
16
Drawing a Linevoid drawLine-Bresenham(int x1,x2, int y1,y2)
int x = x1 int y = y1 int e = -(x2-x1)
while (x <= x2)
setPixel(x,y,PIXEL_ON)
x += 1 e += 2*(y2-y1) if (e >= 0.0) y+=1 e-=2*(x2-x1)
FasterNot wrong
![Page 9: Line Drawing Algorithm - University of California, Berkeleyinst.eecs.berkeley.edu/~cs184/fa06/lectures/line-drawing.pdfFrom Prof. James O' Brien CS184 Sp06 notes: Scan Conversion 2](https://reader031.vdocument.in/reader031/viewer/2022030608/5ad6e3987f8b9a9d5c8b68fb/html5/thumbnails/9.jpg)
From Prof. James O' Brien CS184 Sp06notes: Scan Conversion
9
17
Drawing a Line
How thick?
Ends?
18
Drawing a Line
Joining?
Ugly Bevel Round Miter