DrStrangeLove
DrStrangeLove

Reputation: 11557

How does computer draw a line??

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??

Upvotes: 6

Views: 1902

Answers (4)

Emond
Emond

Reputation: 50672

I suspect there is more going on that just (a form of) Bresenham as there is also (optional) anti-aliasing. see this article for what might be the implemented algoritm (Xiaolin Wu's line algorithm)

Upvotes: 0

Christian Rau
Christian Rau

Reputation: 45948

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.

Upvotes: 1

Yahia
Yahia

Reputation: 70369

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...

Upvotes: 2

Mark Ransom
Mark Ransom

Reputation: 308140

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.

Upvotes: 7

Related Questions