- Loading...
- No images or files uploaded yet.
|
|
gatopeich's Graphic Algorithmsgatopeich's Graphic Algorithms
This page is about algorithms to rasterize (represent with pixels) graphic objects (lines, circles, textures) using simple integer math.
Some history...These algorithms I developed in my young coding times (1992~1995) for usage in assembler code routines that conformed a i386-architecture 320x200 resolution graphic library I would use for games & demos.
At that time, Bressenham's line algorithms appealed to me as too 'slow', as it used some non-integer operations. I felt I could do a little bit 'better', specially faster by not using anything else but integers and the simplest integer operations. I must say I had very imperfect knowledge of Bressenham's work.
The original intention in using just integer operations was to speed up the code. Later I discovered that integers can deliver optimal accuracy and nicest results, apart from the best speed in typical PC architectures.
Years later (ang ago :-)) I included a few of these algorithms in "4bpp" library (http://fourbpp.sourceforge.net/), aimed at 4 bits-per-pixel (2 pixel pet byte) framebuffers. and several unpublished pieces of work.
Simple MathThe key ideas are:
Line Drawing by Integer Operations
Line drawing is the basic problem in rasterized graphics. It consists of connecting two pixels with those that approximate best the linear segment between them. After much work to obtain nice and symmetric results, I came to the following rationalization of the problem:
Let's have an example to brighten things up:
Some examples to get it right, for a line 10 pixels wide by 3 pixels high: The "good": The "bad" ('x' points don't comply condition about distance to ideal line):o o o · · · · · · ·· · · o o o o · · ·· · · · · · · o o o o · · · · · · · · ·· x · · · · · · · ·· · x x x x o o o o And the "ugly" (may comply conditions but following a non-centered line, I have seen things like this): o o o o · · · · · ·· · · · o o o · · ·· · · · · · · o o o The algorithmThis algorithm provides a solution for the line between points P1 and P2 with coordinates (x1,y1) and (x2,y2). For the sake of simplicity we'll assume the case where dx = x2 - x1 > 0, dy = y2 - y1 > 0, and dx > dy.
The main idea is to "divide by repeated substraction", that is: For each x-step substract dy from an accumulator (a := a - dy) initialized as (a := dx). Then whenever the accumulator comes to 0 or below "we walked enough in x-direction to have one step in y-direction". At that point, we take that y-step, and 'reload' the accumulator (a := a + dx)...
Result for the 10x3 case is the "good" one, as shown above.
Notes:
Similarity with Bresenham's
Though this algorithm was developed 'from scratch', I recently online a very similar one as an optimized version of Bresenham's algorithm (http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm#Optimisation). This is because of the idea of optimizing Bresenham's by scalating the real numbers and operations in order to work with integers, which on a mathematical plane translates to roughly the same as my 'original' idea. Thus said, I can hardly claim that my algorithm is 'new' or 'original'. I seem to have "reinvented the wheel" :-).
PerformanceThis is NOT the fastest line algorithm I know about, but I still think it is the most elegant. When implemented in C ( Here are some results for the number avid, obtained after adding my code to the mentioned benchmark (
Line algorithms cost comparison:
Raw data:
Note: Though I have used and thoroughly tested this algorithm, this particular C implementation was written almost on the fly and I haven't had the time to unit test it. Thanks a lot to Stuart Fischer for discovering the nasty error that made me think my algorithm was the fastest!
Circle Drawing by Integer OperationsWell, I was all about to explain my 'original' idea about iteratively obtaining square numbers, and the circle algorithm I developed from it in my youth days... Then I found it perfectly explained as a circle variant of Bresenham's line algorithm: http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm#Circle_Variant This makes me see (again) how terribly useful it would have been to have Wikipedia some ten years ago, and the allways-recurrent question: Can one come up with anything new anymore?
Back to gatopeich's front page
|
Comments (0)
You don't have permission to comment on this page.