summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBertrik Sikken <bertrik@sikken.nl>2008-08-01 19:33:55 +0000
committerBertrik Sikken <bertrik@sikken.nl>2008-08-01 19:33:55 +0000
commit7c3f98f58c69a0b382b69ca546a273d20ebf2d5f (patch)
tree3d04f79148736141b17eb833fa0edd55505109dd
parent84d8d83c50cab91962a1bf683156f4c7c5b30f64 (diff)
downloadrockbox-7c3f98f58c69a0b382b69ca546a273d20ebf2d5f.tar.gz
rockbox-7c3f98f58c69a0b382b69ca546a273d20ebf2d5f.zip
Apply FS#9217 - Rockpaint line-drawing function, replace line-drawing algorithm by better looking bresenham algorithm
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@18176 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--apps/plugins/rockpaint.c100
1 files changed, 47 insertions, 53 deletions
diff --git a/apps/plugins/rockpaint.c b/apps/plugins/rockpaint.c
index 82fb2ea29b..c4e6328923 100644
--- a/apps/plugins/rockpaint.c
+++ b/apps/plugins/rockpaint.c
@@ -1610,64 +1610,58 @@ static void draw_brush( int x, int y )
}
}
+/* This is an implementation of Bresenham's line algorithm.
+ * See http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm.
+ */
static void draw_line( int x1, int y1, int x2, int y2 )
{
- x1 = x1<<1;
- y1 = y1<<1;
- x2 = x2<<1;
- y2 = y2<<1;
- int w = x1 - x2;
- int h = y1 - y2;
-
- int x, y;
-
- if( w == 0 && h == 0 )
- {
- draw_pixel( x1>>1, y1>>1 );
- return;
- }
-
- if( w < 0 ) w *= -1;
- if( h < 0 ) h *= -1;
-
- if( w > h )
- {
- if( x1 > x2 )
- {
- x = x2;
- y = y2;
- x2 = x1;
- y2 = y1;
- x1 = x;
- y1 = y;
- }
- w = x1 - x2;
- h = y1 - y2;
- while( x1 <= x2 )
- {
- draw_pixel( (x1+1)>>1, (y1+1)>>1 );
- x1+=2;
- y1 = y2 - ( x2 - x1 ) * h / w;
+ int x = x1;
+ int y = y1;
+ int deltax = x2 - x1;
+ int deltay = y2 - y1;
+ int i;
+
+ int xerr = abs(deltax);
+ int yerr = abs(deltay);
+ int xstep = deltax > 0 ? 1 : -1;
+ int ystep = deltay > 0 ? 1 : -1;
+ int err;
+
+ if (yerr > xerr)
+ {
+ /* more vertical */
+ err = yerr;
+ xerr <<= 1;
+ yerr <<= 1;
+
+ /* to leave off the last pixel of the line, leave off the "+ 1" */
+ for (i = abs(deltay) + 1; i; --i)
+ {
+ draw_pixel(x, y);
+ y += ystep;
+ err -= xerr;
+ if (err < 0) {
+ x += xstep;
+ err += yerr;
+ }
}
}
- else /* h > w */
+ else
{
- if( y1 > y2 )
- {
- x = x2;
- y = y2;
- x2 = x1;
- y2 = y1;
- x1 = x;
- y1 = y;
- }
- w = x1 - x2;
- h = y1 - y2;
- while( y1 <= y2 )
- {
- draw_pixel( (x1+1)>>1, (y1+1)>>1 );
- y1+=2;
- x1 = x2 - ( y2 - y1 ) * w / h;
+ /* more horizontal */
+ err = xerr;
+ xerr <<= 1;
+ yerr <<= 1;
+
+ for (i = abs(deltax) + 1; i; --i)
+ {
+ draw_pixel(x, y);
+ x += xstep;
+ err -= yerr;
+ if (err < 0) {
+ y += ystep;
+ err += xerr;
+ }
}
}
}