Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does computer draw a line??

Tags:

graphics

gdi

Windows GDI has these functions:

MoveTo();

LineTo();

They accept coordinates where to start drawing and where to stop drawing.

But how are these functions implemented?? (especially LineTo)

Do they need to calculate all points between point A and point B??

How is this line drawn exactly??

like image 990
DrStrangeLove Avatar asked Jul 29 '11 19:07

DrStrangeLove


3 Answers

Yes, they calculate each individual point between A and B.

The most common way to do this efficiently is known as Bresenham's Line Algorithm.

Note that Windows LineTo does not draw the last point. When line segments are drawn one after another, this prevents the endpoints from being double drawn.

like image 96
Mark Ransom Avatar answered Oct 20 '22 00:10

Mark Ransom


nobody who never saw the Windows source code can answer this in-depth... BUT Windows is just as any other software: it needs some algorithm to draw a line... one such algorithm you can see here http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm

The Moveto is "easier" in that it just updates the current coordinates the system know of...

like image 33
Yahia Avatar answered Oct 19 '22 22:10

Yahia


It doesn't need to calculate all points between A and B (which are infinite) but only the discrete pixels between A and B. That's usually a standard line rasterization algorithm. See Wikipedia for Bresenham's line rasterization algorithm, which is the standard school book example and usually the base for more flexible rasterization algorithms.

like image 37
Christian Rau Avatar answered Oct 20 '22 00:10

Christian Rau