SlideShare a Scribd company logo
1 of 7
Download to read offline
LESSON 4: Drawing Line Segments 1
Drawing a line segment
Line segments are basic graphics primitives. Drawing good-quality line
segments efficiently is a fundamental task in real-time computer graphics.
Three methods for drawing a line segment will be discussed in this lesson,
leading to Bresenham’s algorithm which uses on average one integer addition
per pixel to rasterize a line segment.
Input: starting point (xs, ys) and ending point (xe, ye), where xs, ys, xe, ye
are integers.
Assumptions:
• ye ≥ ys, xe > xs, and |ye − ys| ≤ |xe − xs|. So 0 ≤ k = (ye − ys)/(xe −
xs) ≤ 1, k the slope. Note that any other line segment can be transformed
to such a position by properly choosing the starting point or swapping x and
y coordinates, if necessary.
• One pixel is to be found on each vertical line intersecting the given line
segment.
• A sequence of pixels will be determined to approximate the line segment.
Method 1:
For each unit increment in x-direction, y is increased by k, the slope. If
the intersection between the vertical line x = i and the given line segment is
(i, yi), the intersection between the next vertical line x = i + 1 and the given
LESSON 4: Drawing Line Segments 2
k
x=i
(i,Yi)
(i+1,Yi+k)
e
Raster Line Drawing
line is (i + 1, yi + k). See the figure.
Note that, since pixel positions are needed, we must round (i, yi), xs ≤ i ≤
xe, to the nearest integer point (i, yi + 0.5 ). The pseudo code is as follows.
Line Drawing 1:
long x, y;
float k, yy;
k = (ye - ys)/(xe - xs);
yy = ys;
for(x=xs; x<=xe; x++)
LESSON 4: Drawing Line Segments 3
{
y = ftrunc(yy + 0.5);
write_pixel(x,y);
yy = yy + k;
}
Remarks: Floating-point operations are used in this solution. Floating
point operations are slower than integer operations.
Method 2:
Idea: Suppose that the distance e of (i, yi) to the horizontal grid line right
below it is recorded. Then the lower pixel should be chosen if and only if
e < 0.5. To facilitate this test, we denote e − 0.5 by e instead. Thus, the
lower pixel is chosen if and only if e < 0. Pay attention to how e is updated
in each step.
long x, y;
float k, e;
k = (ye - ys)/(xe - xs);
x = xs; y = ys;
e = -0.5;
for(x=xs; x<=xe; x++)
{
if(e < 0)
write_pixel(x,y);
LESSON 4: Drawing Line Segments 4
else
{
y = y + 1;
write_pixel(x,y);
e = e - 1;
}
e = e + k;
}
Remark: Floating-point operations are still used in method 2.
Method 3:
Idea: We use program transformation to translate method 2 into a new
algorithm. The key observation is: it is the sign of e, not its value, that
determines the next pixel to be selected.
Let a = xe−xs, b = ye−ys. Then k = b/a. All the statements in method
2 that affect the value of e are
e = −0.5; e = e − 1; e = e +
b
a
.
Multiplying 2a to both sides of these three expressions, we obtain
2a ∗ e = −a; 2a ∗ e = 2a ∗ e − 2a; 2a ∗ e = 2a ∗ e + 2b;
Naming 2a ∗ e by d yields
d = −a; d = d − 2a; d = d + 2b.
LESSON 4: Drawing Line Segments 5
Using these three expressions to replace the original statements that are used
to generate e in method 2 yields the following pseudo code.
long x, y, dx, dy, d;
x = xs; y = ys;
dx = 2*(xe - xs); /* dx = 2a */
dy = 2*(ye - ys); /* dy = 2b */
d = -(xe - xs); /* d = -a */
for(x=xs; x<=xe; x++)
{
if(d < 0)
write_pixel(x, y);
else
{
y = y + 1;
write_pixel(x,y);
d = d - dx;
}
d = d + dy;
}
Remarks: This algorithm uses integer operations only. It can be further
simplified by re-arranging some statements.
LESSON 4: Drawing Line Segments 6
Bresenham’s algorithm
By combining the statement d = d - dx; and d = d + dy; in the case
of moving up diagonally, we have the final algorithm, which on average uses
one integer addition and one sign testing per pixel.
long x, y, dx, dy, dy_x, d;
x = xs; y = ys;
dx = 2*(xe - xs); /* dx = 2a */
dy = 2*(ye - ys); /* dy = 2b */
dy_x = dy - dx;
d = -(xe - xs); /* d = -a */
for(x=xs; x<=xe; x++)
{
if(d < 0)
d = d + dy;
else
{
y = y + 1;
d = d + dy_x;
}
write_pixel(x,y);
}
LESSON 4: Drawing Line Segments 7
Question:
Can you come up with a line drawing algorithm that is faster than Bresen-
ham’s algorithm?

More Related Content

What's hot (20)

Es272 ch6
Es272 ch6Es272 ch6
Es272 ch6
 
Chapter 5 Point Slope Form
Chapter 5 Point Slope FormChapter 5 Point Slope Form
Chapter 5 Point Slope Form
 
Presentation on application of numerical method in our life
Presentation on application of numerical method in our lifePresentation on application of numerical method in our life
Presentation on application of numerical method in our life
 
Es272 ch4b
Es272 ch4bEs272 ch4b
Es272 ch4b
 
Curve fitting
Curve fittingCurve fitting
Curve fitting
 
Presentation on Numerical Method (Trapezoidal Method)
Presentation on Numerical Method (Trapezoidal Method)Presentation on Numerical Method (Trapezoidal Method)
Presentation on Numerical Method (Trapezoidal Method)
 
Algorithm chapter 11
Algorithm chapter 11Algorithm chapter 11
Algorithm chapter 11
 
Matlab polynimials and curve fitting
Matlab polynimials and curve fittingMatlab polynimials and curve fitting
Matlab polynimials and curve fitting
 
Secant Method
Secant MethodSecant Method
Secant Method
 
Chapter 5 Identifying Linear Functions
Chapter 5 Identifying Linear FunctionsChapter 5 Identifying Linear Functions
Chapter 5 Identifying Linear Functions
 
Es272 ch2
Es272 ch2Es272 ch2
Es272 ch2
 
1552 limits graphically and nume
1552 limits graphically and nume1552 limits graphically and nume
1552 limits graphically and nume
 
Chapter 5 The Slope Formula
Chapter 5 The Slope FormulaChapter 5 The Slope Formula
Chapter 5 The Slope Formula
 
Least square method
Least square methodLeast square method
Least square method
 
numerical methods
numerical methodsnumerical methods
numerical methods
 
False Point Method / Regula falsi method
False Point Method / Regula falsi methodFalse Point Method / Regula falsi method
False Point Method / Regula falsi method
 
Numerical method
Numerical methodNumerical method
Numerical method
 
Es272 ch4a
Es272 ch4aEs272 ch4a
Es272 ch4a
 
Lecture 8 power & exp rules
Lecture 8   power & exp rulesLecture 8   power & exp rules
Lecture 8 power & exp rules
 
Lecture 6 limits with infinity
Lecture 6   limits with infinityLecture 6   limits with infinity
Lecture 6 limits with infinity
 

Viewers also liked

7. pemrograman struktur
7. pemrograman struktur7. pemrograman struktur
7. pemrograman strukturRoziq Bahtiar
 
Flowchart progrm linear bilangan bulat
Flowchart progrm linear bilangan bulatFlowchart progrm linear bilangan bulat
Flowchart progrm linear bilangan bulatRoziq Bahtiar
 
Pengantar algoritma pemrograman
Pengantar algoritma pemrogramanPengantar algoritma pemrograman
Pengantar algoritma pemrogramanRoziq Bahtiar
 
Tarby magazine salafiyah kajen
Tarby magazine  salafiyah kajenTarby magazine  salafiyah kajen
Tarby magazine salafiyah kajenRoziq Bahtiar
 
Perintah perintah dasar linux Operating Sistem
Perintah perintah dasar linux Operating SistemPerintah perintah dasar linux Operating Sistem
Perintah perintah dasar linux Operating SistemRoziq Bahtiar
 
static and dynamic routing
static and dynamic routingstatic and dynamic routing
static and dynamic routingRoziq Bahtiar
 

Viewers also liked (9)

7. pemrograman struktur
7. pemrograman struktur7. pemrograman struktur
7. pemrograman struktur
 
Flowchart progrm linear bilangan bulat
Flowchart progrm linear bilangan bulatFlowchart progrm linear bilangan bulat
Flowchart progrm linear bilangan bulat
 
Pengantar algoritma pemrograman
Pengantar algoritma pemrogramanPengantar algoritma pemrograman
Pengantar algoritma pemrograman
 
Tarby magazine salafiyah kajen
Tarby magazine  salafiyah kajenTarby magazine  salafiyah kajen
Tarby magazine salafiyah kajen
 
Pcd 10
Pcd 10Pcd 10
Pcd 10
 
Pcd 11
Pcd 11Pcd 11
Pcd 11
 
Perintah perintah dasar linux Operating Sistem
Perintah perintah dasar linux Operating SistemPerintah perintah dasar linux Operating Sistem
Perintah perintah dasar linux Operating Sistem
 
static and dynamic routing
static and dynamic routingstatic and dynamic routing
static and dynamic routing
 
OpenGL Basics
OpenGL BasicsOpenGL Basics
OpenGL Basics
 

Similar to Open GL T0074 56 sm2

cgrchapter2version-1-200729063505 (1).pdf
cgrchapter2version-1-200729063505 (1).pdfcgrchapter2version-1-200729063505 (1).pdf
cgrchapter2version-1-200729063505 (1).pdfmeenasp
 
Line Drawing Algorithms - Computer Graphics - Notes
Line Drawing Algorithms - Computer Graphics - NotesLine Drawing Algorithms - Computer Graphics - Notes
Line Drawing Algorithms - Computer Graphics - NotesOmprakash Chauhan
 
4 CG_U1_M3_PPT_4 DDA.pptx
4 CG_U1_M3_PPT_4 DDA.pptx4 CG_U1_M3_PPT_4 DDA.pptx
4 CG_U1_M3_PPT_4 DDA.pptxssuser255bf1
 
Computer graphics LINE DRAWING algorithm.pptx
Computer graphics LINE DRAWING algorithm.pptxComputer graphics LINE DRAWING algorithm.pptx
Computer graphics LINE DRAWING algorithm.pptxR S Anu Prabha
 
Unit-2 raster scan graphics,line,circle and polygon algorithms
Unit-2 raster scan graphics,line,circle and polygon algorithmsUnit-2 raster scan graphics,line,circle and polygon algorithms
Unit-2 raster scan graphics,line,circle and polygon algorithmsAmol Gaikwad
 
Rasterisation of a circle by the bresenham algorithm
Rasterisation of a circle by the bresenham algorithmRasterisation of a circle by the bresenham algorithm
Rasterisation of a circle by the bresenham algorithmKALAIRANJANI21
 
Rasterisation of a circle by the bresenham algorithm
Rasterisation of a circle by the bresenham algorithmRasterisation of a circle by the bresenham algorithm
Rasterisation of a circle by the bresenham algorithmKALAIRANJANI21
 
8.further calculus Further Mathematics Zimbabwe Zimsec Cambridge
8.further calculus   Further Mathematics Zimbabwe Zimsec Cambridge8.further calculus   Further Mathematics Zimbabwe Zimsec Cambridge
8.further calculus Further Mathematics Zimbabwe Zimsec Cambridgealproelearning
 
Computer Graphics - Introduction in Brief By: Prof. Manisha Waghmare- Butkar
Computer Graphics - Introduction in Brief By: Prof. Manisha Waghmare- ButkarComputer Graphics - Introduction in Brief By: Prof. Manisha Waghmare- Butkar
Computer Graphics - Introduction in Brief By: Prof. Manisha Waghmare- ButkarVishal Butkar
 
Open GL T0074 56 sm4
Open GL T0074 56 sm4Open GL T0074 56 sm4
Open GL T0074 56 sm4Roziq Bahtiar
 
Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1Philip Schwarz
 
Computer Graphics Unit 2
Computer Graphics Unit 2Computer Graphics Unit 2
Computer Graphics Unit 2SanthiNivas
 
Output primitives in Computer Graphics
Output primitives in Computer GraphicsOutput primitives in Computer Graphics
Output primitives in Computer GraphicsKamal Acharya
 
Mid point line Algorithm - Computer Graphics
Mid point line Algorithm - Computer GraphicsMid point line Algorithm - Computer Graphics
Mid point line Algorithm - Computer GraphicsDrishti Bhalla
 
February 18 2016
February 18 2016February 18 2016
February 18 2016khyps13
 
Matlab-free course by Mohd Esa
Matlab-free course by Mohd EsaMatlab-free course by Mohd Esa
Matlab-free course by Mohd EsaMohd Esa
 
INTRODUCTION TO MATLAB presentation.pptx
INTRODUCTION TO MATLAB presentation.pptxINTRODUCTION TO MATLAB presentation.pptx
INTRODUCTION TO MATLAB presentation.pptxDevaraj Chilakala
 
How to graph Functions
How to graph FunctionsHow to graph Functions
How to graph Functionscoolhanddav
 

Similar to Open GL T0074 56 sm2 (20)

cgrchapter2version-1-200729063505 (1).pdf
cgrchapter2version-1-200729063505 (1).pdfcgrchapter2version-1-200729063505 (1).pdf
cgrchapter2version-1-200729063505 (1).pdf
 
Line Drawing Algorithms - Computer Graphics - Notes
Line Drawing Algorithms - Computer Graphics - NotesLine Drawing Algorithms - Computer Graphics - Notes
Line Drawing Algorithms - Computer Graphics - Notes
 
Line circle draw
Line circle drawLine circle draw
Line circle draw
 
4 CG_U1_M3_PPT_4 DDA.pptx
4 CG_U1_M3_PPT_4 DDA.pptx4 CG_U1_M3_PPT_4 DDA.pptx
4 CG_U1_M3_PPT_4 DDA.pptx
 
Computer graphics LINE DRAWING algorithm.pptx
Computer graphics LINE DRAWING algorithm.pptxComputer graphics LINE DRAWING algorithm.pptx
Computer graphics LINE DRAWING algorithm.pptx
 
raster algorithm.pdf
raster algorithm.pdfraster algorithm.pdf
raster algorithm.pdf
 
Unit-2 raster scan graphics,line,circle and polygon algorithms
Unit-2 raster scan graphics,line,circle and polygon algorithmsUnit-2 raster scan graphics,line,circle and polygon algorithms
Unit-2 raster scan graphics,line,circle and polygon algorithms
 
Rasterisation of a circle by the bresenham algorithm
Rasterisation of a circle by the bresenham algorithmRasterisation of a circle by the bresenham algorithm
Rasterisation of a circle by the bresenham algorithm
 
Rasterisation of a circle by the bresenham algorithm
Rasterisation of a circle by the bresenham algorithmRasterisation of a circle by the bresenham algorithm
Rasterisation of a circle by the bresenham algorithm
 
8.further calculus Further Mathematics Zimbabwe Zimsec Cambridge
8.further calculus   Further Mathematics Zimbabwe Zimsec Cambridge8.further calculus   Further Mathematics Zimbabwe Zimsec Cambridge
8.further calculus Further Mathematics Zimbabwe Zimsec Cambridge
 
Computer Graphics - Introduction in Brief By: Prof. Manisha Waghmare- Butkar
Computer Graphics - Introduction in Brief By: Prof. Manisha Waghmare- ButkarComputer Graphics - Introduction in Brief By: Prof. Manisha Waghmare- Butkar
Computer Graphics - Introduction in Brief By: Prof. Manisha Waghmare- Butkar
 
Open GL T0074 56 sm4
Open GL T0074 56 sm4Open GL T0074 56 sm4
Open GL T0074 56 sm4
 
Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1
 
Computer Graphics Unit 2
Computer Graphics Unit 2Computer Graphics Unit 2
Computer Graphics Unit 2
 
Output primitives in Computer Graphics
Output primitives in Computer GraphicsOutput primitives in Computer Graphics
Output primitives in Computer Graphics
 
Mid point line Algorithm - Computer Graphics
Mid point line Algorithm - Computer GraphicsMid point line Algorithm - Computer Graphics
Mid point line Algorithm - Computer Graphics
 
February 18 2016
February 18 2016February 18 2016
February 18 2016
 
Matlab-free course by Mohd Esa
Matlab-free course by Mohd EsaMatlab-free course by Mohd Esa
Matlab-free course by Mohd Esa
 
INTRODUCTION TO MATLAB presentation.pptx
INTRODUCTION TO MATLAB presentation.pptxINTRODUCTION TO MATLAB presentation.pptx
INTRODUCTION TO MATLAB presentation.pptx
 
How to graph Functions
How to graph FunctionsHow to graph Functions
How to graph Functions
 

More from Roziq Bahtiar

More from Roziq Bahtiar (20)

Techarea company profile
Techarea company profileTecharea company profile
Techarea company profile
 
6. pemrograman pointer
6. pemrograman pointer6. pemrograman pointer
6. pemrograman pointer
 
5. pemrograman array dan_string
5. pemrograman array dan_string5. pemrograman array dan_string
5. pemrograman array dan_string
 
4. pemrograman fungsi
4. pemrograman fungsi4. pemrograman fungsi
4. pemrograman fungsi
 
3. teknik looping dalam_pemrograman
3. teknik looping dalam_pemrograman3. teknik looping dalam_pemrograman
3. teknik looping dalam_pemrograman
 
2. teknik pemilihan dalam_pemrograman
2. teknik pemilihan dalam_pemrograman2. teknik pemilihan dalam_pemrograman
2. teknik pemilihan dalam_pemrograman
 
1. variable identifier dan_tipe_data
1. variable identifier dan_tipe_data1. variable identifier dan_tipe_data
1. variable identifier dan_tipe_data
 
Alpro tutor
Alpro tutorAlpro tutor
Alpro tutor
 
Pcd 7
Pcd 7Pcd 7
Pcd 7
 
Pcd 5
Pcd 5Pcd 5
Pcd 5
 
Pcd 4
Pcd 4Pcd 4
Pcd 4
 
Eigen
EigenEigen
Eigen
 
3 piksel_dan_histogram
 3 piksel_dan_histogram 3 piksel_dan_histogram
3 piksel_dan_histogram
 
Pcd 8
Pcd 8Pcd 8
Pcd 8
 
2 pengolahan_citra
 2 pengolahan_citra 2 pengolahan_citra
2 pengolahan_citra
 
Pertemuan 1
Pertemuan 1Pertemuan 1
Pertemuan 1
 
Open GL T0074 56 sm3
Open GL T0074 56 sm3Open GL T0074 56 sm3
Open GL T0074 56 sm3
 
Open GL T0074 56 sm1
Open GL T0074 56 sm1Open GL T0074 56 sm1
Open GL T0074 56 sm1
 
Open GL 09 scan conversion
Open GL 09 scan conversionOpen GL 09 scan conversion
Open GL 09 scan conversion
 
Open GL 04 linealgos
Open GL 04 linealgosOpen GL 04 linealgos
Open GL 04 linealgos
 

Recently uploaded

USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Food processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsFood processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsManeerUddin
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxMusic 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxleah joy valeriano
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 

Recently uploaded (20)

USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Food processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsFood processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture hons
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxMusic 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 

Open GL T0074 56 sm2

  • 1. LESSON 4: Drawing Line Segments 1 Drawing a line segment Line segments are basic graphics primitives. Drawing good-quality line segments efficiently is a fundamental task in real-time computer graphics. Three methods for drawing a line segment will be discussed in this lesson, leading to Bresenham’s algorithm which uses on average one integer addition per pixel to rasterize a line segment. Input: starting point (xs, ys) and ending point (xe, ye), where xs, ys, xe, ye are integers. Assumptions: • ye ≥ ys, xe > xs, and |ye − ys| ≤ |xe − xs|. So 0 ≤ k = (ye − ys)/(xe − xs) ≤ 1, k the slope. Note that any other line segment can be transformed to such a position by properly choosing the starting point or swapping x and y coordinates, if necessary. • One pixel is to be found on each vertical line intersecting the given line segment. • A sequence of pixels will be determined to approximate the line segment. Method 1: For each unit increment in x-direction, y is increased by k, the slope. If the intersection between the vertical line x = i and the given line segment is (i, yi), the intersection between the next vertical line x = i + 1 and the given
  • 2. LESSON 4: Drawing Line Segments 2 k x=i (i,Yi) (i+1,Yi+k) e Raster Line Drawing line is (i + 1, yi + k). See the figure. Note that, since pixel positions are needed, we must round (i, yi), xs ≤ i ≤ xe, to the nearest integer point (i, yi + 0.5 ). The pseudo code is as follows. Line Drawing 1: long x, y; float k, yy; k = (ye - ys)/(xe - xs); yy = ys; for(x=xs; x<=xe; x++)
  • 3. LESSON 4: Drawing Line Segments 3 { y = ftrunc(yy + 0.5); write_pixel(x,y); yy = yy + k; } Remarks: Floating-point operations are used in this solution. Floating point operations are slower than integer operations. Method 2: Idea: Suppose that the distance e of (i, yi) to the horizontal grid line right below it is recorded. Then the lower pixel should be chosen if and only if e < 0.5. To facilitate this test, we denote e − 0.5 by e instead. Thus, the lower pixel is chosen if and only if e < 0. Pay attention to how e is updated in each step. long x, y; float k, e; k = (ye - ys)/(xe - xs); x = xs; y = ys; e = -0.5; for(x=xs; x<=xe; x++) { if(e < 0) write_pixel(x,y);
  • 4. LESSON 4: Drawing Line Segments 4 else { y = y + 1; write_pixel(x,y); e = e - 1; } e = e + k; } Remark: Floating-point operations are still used in method 2. Method 3: Idea: We use program transformation to translate method 2 into a new algorithm. The key observation is: it is the sign of e, not its value, that determines the next pixel to be selected. Let a = xe−xs, b = ye−ys. Then k = b/a. All the statements in method 2 that affect the value of e are e = −0.5; e = e − 1; e = e + b a . Multiplying 2a to both sides of these three expressions, we obtain 2a ∗ e = −a; 2a ∗ e = 2a ∗ e − 2a; 2a ∗ e = 2a ∗ e + 2b; Naming 2a ∗ e by d yields d = −a; d = d − 2a; d = d + 2b.
  • 5. LESSON 4: Drawing Line Segments 5 Using these three expressions to replace the original statements that are used to generate e in method 2 yields the following pseudo code. long x, y, dx, dy, d; x = xs; y = ys; dx = 2*(xe - xs); /* dx = 2a */ dy = 2*(ye - ys); /* dy = 2b */ d = -(xe - xs); /* d = -a */ for(x=xs; x<=xe; x++) { if(d < 0) write_pixel(x, y); else { y = y + 1; write_pixel(x,y); d = d - dx; } d = d + dy; } Remarks: This algorithm uses integer operations only. It can be further simplified by re-arranging some statements.
  • 6. LESSON 4: Drawing Line Segments 6 Bresenham’s algorithm By combining the statement d = d - dx; and d = d + dy; in the case of moving up diagonally, we have the final algorithm, which on average uses one integer addition and one sign testing per pixel. long x, y, dx, dy, dy_x, d; x = xs; y = ys; dx = 2*(xe - xs); /* dx = 2a */ dy = 2*(ye - ys); /* dy = 2b */ dy_x = dy - dx; d = -(xe - xs); /* d = -a */ for(x=xs; x<=xe; x++) { if(d < 0) d = d + dy; else { y = y + 1; d = d + dy_x; } write_pixel(x,y); }
  • 7. LESSON 4: Drawing Line Segments 7 Question: Can you come up with a line drawing algorithm that is faster than Bresen- ham’s algorithm?