SlideShare ist ein Scribd-Unternehmen logo
1 von 7
Downloaden Sie, um offline zu lesen
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?

Weitere ähnliche Inhalte

Was ist angesagt? (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
 

Andere mochten auch

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
 

Andere mochten auch (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
 

Ähnlich wie 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
 

Ähnlich wie 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
 

Mehr von Roziq Bahtiar

Mehr von 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
 

Kürzlich hochgeladen

Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 

Kürzlich hochgeladen (20)

Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . 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?