connecting multiple anti-aliased lines together
I implemented/copied the wu line algorithm from pseudo-code on wiki-pedia and other places.
When drawing a sine wave it breaks down at the point where the line changes from y dominant to x dominant (or vice versa). (I did not copy the endpoint code because it looks terrible and I do not need them for my purposes. )
Does anyone know a solution for this issue? If not I will modify the algorithm myself to get it to work. I am just curious if someone else has run into this and knows exactly how to fix it. Is it possible to impl开发者_如何学运维ement without the lines being globally aware of each other? Or is this why drawing API's implement moveto and lineto functions?
The pseudo code
If performance is not a big issue you can take a more naive approach to drawing anti-aliased lines, e.g.:
- Draw the lines in a higher resolution grid (e.g. some integer multiple of your target resolution) - you'll need to adjust their thickness.
- Downsample to your target resolution by converting each block of n x m pixels to a single pixels, e.g. by averaging the RGB values.
Depending on what language or OS you're developing with there may be built in functionality to simplying this.
Alternatively you can calculate what portion of each pixel is covered by your line (by intersecting the pixel square with the line) to calculate the pixels value, make the pixel intensity relative to the portion of the pixel area covered by the line.
I just wanted to follow up with this and close it out. I thought I had removed the endpoint code from the posted algorithm on the website. However, I made a mistake and did not remove all of it so that is why the lines were breaking. Here is the final result:
If you look closely enough you can see the intensity jump a bit when the lines change from being y-dominant to x dominant and vice versa. It is also because the line is less than one pixel long. I suppose drawing a line at a higher resolution and then down sampling would produce a higher quality result like Guy Sirton suggested. But this is good enough for my purposes.
精彩评论