|
A great Code Editor for Windows If you haven't used EditPadPro yet, I strongly suggest checking it out. I've been using this editor for years and it is truely the best piece of software I've ever used. It even allows you to link your project to your FTP server and upload files as you are editing them automatically. Just save the file in the editor and it will be uploaded to your server -- no waiting for the file to be uploaded.
I usually never pay for software I find online, but I actually went ahead and upgraded to the full version that unlocks a bunch of features, well worth the $49.95 | ![]() | Download Now for FREE and Make your coding life easier A fully functional demo (only some features are missing) |
DirectDraw - 4 - Lines
Source code
|
| JUMP TO - dd1 - dd2 - dd3 - dd4 - dd5 - dd6 - dd7 |
|
In this tutorial I will explain how to draw lines using the Bresenham's line-drawing algorithm. And then show you complete line drawing function. For the sake of this series of tutorials I will use the 16-bit mode, so we will be dealing with ushorts(or words) per pixel. I will also use the 16-bit color macro defined in the previous tutorial. The following is an explanation of how the Bresenham's line-drawing algorithm works, rather than exact implementation.
You might think of using floating variables but I assure you the whole algorithm is done in straight integer math with no multiplication or division in the main loops(no fixed point math either). How is it possible? Basically, during each iteration through the main drawing loop the error term is tossed around to identify the right pixel as close as possible to the true line. Let's consider these two deltas between the length and height of the line: dx = x1 - x0; dy = y1 - y0; This is a matter of precision and since we're working with integers you will need to scale the deltas by 2 generating two new values: dx2 = dx*2; dy2 = dy*2; These are the values that will be used to change the error term. Why scale? The error term must be first initialized to 0.5 and that cannot be done using an integer. To confuse you even further, finally the scaled values must be substracted by either dx or dy(the original, unscaled delta values) depending on what the major axis is (either x or y). It's time for some code. Here is the initialization part. |
|
| All variables are set and it's time to enter the main loop. |
|
| Check out the source code for the full implementation of line-drawing on the primary surface. The good thing about this function is even if it can be optimized it is still pretty fast. I did not optimize it for the sake of a clear explanation of how this algorithm performs. But there are quite a few methods of doing so out there, I'm sure you could find some of them on the internet. It is not 100% precise, but it is not very evident in any resolution higher than 320x240. This is really not a problem when you're working in 640x480 or higher resolution. If you want a super-precise line, I would recommend looking up sub-pixel methods either online or in books. |
DirectDraw - 4 - Lines
Source code
|
| JUMP TO - dd1 - dd2 - dd3 - dd4 - dd5 - dd6 - dd7 |