Drawing lines is an interesting programming exercise, because it is surprisingly complex. While a line is a continuous entity, it has to be mapped to pixels, which are descrete rectangles on the screen. This mapping requires floating point arithmetic to calculate the points along the line, and integer arithmetic to map the line to pixels on the screen. The calculation involves loops to calculate all of the points along the line, and conditions to take into account different drawing approaches for different categories of line slope.
I drew six lines to test the correctness of the code: slanted up, mostly horizontal; slanted down,
mostly horizontal; slanted right, mostly verticle; slanted left, mostly vertical, purely vertical;
and purely horizontal. These six types of lines all require slightly different algorithms to draw.
There is also a human factors component to drawing lines. They have to look like proper lines to us. This influences how line drawing algorithms place pixels.
Another comment, on an earlier hub about learning programming on your own, suggested that I needed to intruduce concepts in an orderly squence and build upon the basics in order to provide an accessable tutorial. I may be lazy about this. The point is well taken, but it also missinterprets my intention, somewhat. There are lots of good tutorials already on the net. I am not trying to supplant them, just give some pointers that someone might want to research on their own.
I remember a professor once asked his class, "Who thinks you're here to learn from me?". We all raised our hands. He responded, "You're wrong. If everyone just learns from someone else, the end result would be eventual ignorance, because the transmition of knowlegde is imperfect. You are here to learn things that I don't know."
I plan to build some more complex examples from this line drawing example, later.
line drawing code example
Screen view of the sample code in action
links to other hubs in this series
- Learning to Program Computers on Your Own
Computer programming constantly uses encapsulation; someone else figures out how to do something, and you just use it without having to worry about the details. Functions are one mechanism for encapsulation,...
More by this Author
x squared, integrated, differentiated and re-integrated. This hub builds on work done in previous hubs and uses some powerful techniques from both mathematics and programming. If you recall calculus, derivatives and...
A brief introduction to some techniques and principals for leaving the dock under sail in a sailboat.
No comments yet.