From bf28d19f208db02fa79061de5ee4f1478dff32af Mon Sep 17 00:00:00 2001 From: Yves Rizoud Date: Tue, 14 Jun 2011 21:52:20 +0000 Subject: [PATCH] Lua: Fixed drawdisk which was drawing a square instead of a circle (doh!). Radius can now be a multiple of 0.5 to draw circles of even sizes (r=0 -> 1x1, r=0.5 -> 2x2, r=1 -> 3x3 etc.) git-svn-id: svn://pulkomandy.tk/GrafX2/trunk@1798 416bcca6-2ee7-4201-b75f-2eb2f807beb1 --- src/factory.c | 26 ++++++++++++++++++++------ src/graph.c | 14 ++++++++++++++ src/graph.h | 2 ++ src/special.c | 15 --------------- 4 files changed, 36 insertions(+), 21 deletions(-) diff --git a/src/factory.c b/src/factory.c index 796dd910..156f9248 100644 --- a/src/factory.c +++ b/src/factory.c @@ -457,30 +457,44 @@ int L_DrawCircle(lua_State* L) int L_DrawDisk(lua_State* L) { - int center_x, center_y, r, c; + int center_x, center_y, diameter, c, r, even; long circle_limit; short x_pos,y_pos; short min_x,max_x,min_y,max_y; + double r_float; int nb_args = lua_gettop(L); LUA_ARG_LIMIT(4, "drawdisk"); LUA_ARG_NUMBER(1, "drawdisk", center_x, INT_MIN, INT_MAX); LUA_ARG_NUMBER(2, "drawdisk", center_y, INT_MIN, INT_MAX); - LUA_ARG_NUMBER(3, "drawdisk", r, INT_MIN, INT_MAX); + LUA_ARG_NUMBER(3, "drawdisk", r_float, INT_MIN, INT_MAX); LUA_ARG_NUMBER(4, "drawdisk", c, INT_MIN, INT_MAX); - circle_limit = r*r; + if (r_float<0.0) + return 0; + diameter=(int)(floor(r_float*2.0+1.0)); + r=diameter/2; + even=!(diameter&1); + + circle_limit = Circle_squared_diameter(diameter); // Compute clipping limits - min_x=center_x-r<0 ? 0 : center_x-r; + min_x=center_x-r+even<0 ? 0 : center_x-r+even; max_x=center_x+r>=Main_image_width? Main_image_width-1 : center_x+r; - min_y=center_y-r<0 ? 0 : center_y-r; + min_y=center_y-r+even<0 ? 0 : center_y-r+even; max_y=center_y+r>=Main_image_height? Main_image_height-1 : center_y+r; for (y_pos=min_y;y_pos<=max_y;y_pos++) + { + short y=(y_pos-center_y)*2-even; for (x_pos=min_x;x_pos<=max_x;x_pos++) - Pixel_in_current_screen(x_pos,y_pos,c,0); + { + short x=(x_pos-center_x)*2-even; + if (x*x+y*y <= circle_limit) + Pixel_in_current_screen(x_pos,y_pos,c,0); + } + } return 0; } diff --git a/src/graph.c b/src/graph.c index 06c99716..1bd0d9fb 100644 --- a/src/graph.c +++ b/src/graph.c @@ -1340,6 +1340,20 @@ void Draw_filled_circle(short center_x,short center_y,short radius,byte color) Update_part_of_screen(start_x,start_y,end_x+1-start_x,end_y+1-start_y); } +int Circle_squared_diameter(int diameter) +{ + int result = diameter*diameter; + // Trick to make some circles rounder, even though + // mathematically incorrect. + if (diameter==3 || diameter==9) + return result-2; + if (diameter==11) + return result-6; + if (diameter==14) + return result-4; + + return result; +} // -- Tracer général d'une ellipse vide ----------------------------------- diff --git a/src/graph.h b/src/graph.h index 70c49d3c..5347a2d6 100644 --- a/src/graph.h +++ b/src/graph.h @@ -66,6 +66,8 @@ void Hide_empty_circle_preview (short center_x,short center_y,short radius); void Draw_empty_circle_general(short center_x,short center_y,short radius,byte color); void Draw_filled_circle (short center_x,short center_y,short radius,byte color); +int Circle_squared_diameter(int diameter); + void Draw_empty_ellipse_permanent(short center_x,short center_y,short horizontal_radius,short vertical_radius,byte color); void Draw_empty_ellipse_preview (short center_x,short center_y,short horizontal_radius,short vertical_radius,byte color); void Hide_empty_ellipse_preview (short center_x,short center_y,short horizontal_radius,short vertical_radius); diff --git a/src/special.c b/src/special.c index 6e3dc579..db8e14c0 100644 --- a/src/special.c +++ b/src/special.c @@ -37,21 +37,6 @@ //---------------------- Modifier le pinceau spécial ------------------------- -int Circle_squared_diameter(int diameter) -{ - int result = diameter*diameter; - // Trick to make some circles rounder, even though - // mathematically incorrect. - if (diameter==3 || diameter==9) - return result-2; - if (diameter==11) - return result-6; - if (diameter==14) - return result-4; - - return result; -} - void Set_paintbrush_size(int width, int height) { int x_pos,y_pos;