iforce2d Chris / Mbed 2 deprecated ubxDistanceMeter

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers u8g_circle.c Source File

u8g_circle.c

00001 /*
00002 
00003   u8g_circle.c
00004 
00005   Utility to draw empty and filled circles.
00006   
00007   Universal 8bit Graphics Library
00008   
00009   Copyright (c) 2011, bjthom@gmail.com
00010   u8g_DrawCircle & u8g_DrawDisc by olikraus@gmail.com
00011   
00012   All rights reserved.
00013 
00014   Redistribution and use in source and binary forms, with or without modification, 
00015   are permitted provided that the following conditions are met:
00016 
00017   * Redistributions of source code must retain the above copyright notice, this list 
00018     of conditions and the following disclaimer.
00019     
00020   * Redistributions in binary form must reproduce the above copyright notice, this 
00021     list of conditions and the following disclaimer in the documentation and/or other 
00022     materials provided with the distribution.
00023 
00024   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
00025   CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
00026   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
00027   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
00028   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
00029   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
00030   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
00031   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
00032   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
00033   CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
00034   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
00035   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
00036   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
00037   
00038   Addition to the U8G Library 02/25/12
00039   
00040   
00041 */
00042 
00043 #include "u8g.h"
00044 
00045 #ifdef OLD_CODE
00046 
00047 void circ_upperRight(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t x0, u8g_uint_t y0) {
00048   u8g_DrawPixel(u8g, x0 + x, y0 - y);
00049   u8g_DrawPixel(u8g, x0 + y, y0 - x);
00050 }
00051         
00052 void circ_upperLeft(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t x0, u8g_uint_t y0) {
00053   u8g_DrawPixel(u8g, x0 - x, y0 - y);
00054   u8g_DrawPixel(u8g, x0 - y, y0 - x);
00055 }
00056         
00057 void circ_lowerRight(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t x0, u8g_uint_t y0) {
00058   u8g_DrawPixel(u8g, x0 + x, y0 + y);
00059   u8g_DrawPixel(u8g, x0 + y, y0 + x);
00060 }
00061         
00062 void circ_lowerLeft(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t x0, u8g_uint_t y0) {
00063   u8g_DrawPixel(u8g, x0 - x, y0 + y);
00064   u8g_DrawPixel(u8g, x0 - y, y0 + x);
00065 }
00066             
00067 void circ_all(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t x0, u8g_uint_t y0) {
00068   circ_upperRight(u8g, x, y, x0, y0);
00069   circ_upperLeft(u8g, x, y, x0, y0);
00070   circ_lowerRight(u8g, x, y, x0, y0);
00071   circ_lowerLeft(u8g, x, y, x0, y0);
00072 }
00073 
00074 void u8g_DrawEmpCirc(u8g_t *u8g, u8g_uint_t x0, u8g_uint_t y0, u8g_uint_t rad, uint8_t option)
00075 {
00076   if ( u8g_IsBBXIntersection(u8g, x0-rad-1, y0-rad-1, 2*rad+1, 2*rad+1) == 0)
00077     return;
00078 
00079   int f = 1 - rad;
00080   int ddF_x = 1;
00081   int ddF_y = -2*rad;
00082   uint8_t x = 0;
00083   uint8_t y = rad;
00084 
00085   void ( *circ_util )(u8g_t *, u8g_uint_t, u8g_uint_t, u8g_uint_t, u8g_uint_t);
00086   
00087   switch (option)
00088   {
00089     case U8G_CIRC_UPPER_RIGHT:
00090         u8g_DrawPixel(u8g, x0, y0 - rad);
00091         u8g_DrawPixel(u8g, x0 + rad, y0);
00092         circ_util = circ_upperRight;
00093         break;
00094     case U8G_CIRC_UPPER_LEFT:
00095         u8g_DrawPixel(u8g, x0, y0 - rad);
00096         u8g_DrawPixel(u8g, x0 - rad, y0);
00097         circ_util = circ_upperLeft;
00098         break;
00099     case U8G_CIRC_LOWER_RIGHT:
00100         u8g_DrawPixel(u8g, x0, y0 + rad);
00101         u8g_DrawPixel(u8g, x0 + rad, y0);
00102         circ_util = circ_lowerRight;
00103         break;
00104     case U8G_CIRC_LOWER_LEFT:
00105         u8g_DrawPixel(u8g, x0, y0 + rad);
00106         u8g_DrawPixel(u8g, x0 - rad, y0);
00107         circ_util = circ_lowerLeft;
00108         break;
00109         default:
00110     case U8G_CIRC_ALL:
00111         u8g_DrawPixel(u8g, x0, y0 + rad);
00112         u8g_DrawPixel(u8g, x0, y0 - rad);
00113         u8g_DrawPixel(u8g, x0 + rad, y0);
00114         u8g_DrawPixel(u8g, x0 - rad, y0);
00115         circ_util = circ_all;
00116         break;
00117   }
00118   
00119   while( x < y )
00120   {
00121     if(f >= 0) 
00122     {
00123       y--;
00124       ddF_y += 2;
00125       f += ddF_y;
00126     }
00127     x++;
00128     ddF_x += 2;
00129     f += ddF_x;
00130     
00131     circ_util(u8g, x, y, x0, y0);
00132   }
00133 }
00134 
00135 
00136 void u8g_DrawFillCirc(u8g_t *u8g, u8g_uint_t x0, u8g_uint_t y0, u8g_uint_t rad, uint8_t option)
00137 {
00138   if ( u8g_IsBBXIntersection(u8g, x0-rad-1, y0-rad-1, 2*rad+1, 2*rad+1) == 0)
00139     return;
00140 
00141   int f = 1 - rad;
00142   int ddF_x = 1;
00143   int ddF_y = -2*rad;
00144   uint8_t x = 0;
00145   uint8_t y = rad;
00146   
00147   // Draw vertical diameter at the horiz. center
00148   // u8g_DrawVLine(u8g, x0, y0 - rad, 2*rad+1);
00149 
00150   if (option == U8G_CIRC_UPPER_LEFT || option == U8G_CIRC_UPPER_RIGHT) {
00151     u8g_DrawVLine(u8g, x0, y0 - rad, rad+1);
00152   }
00153   else if (option == U8G_CIRC_LOWER_LEFT || option == U8G_CIRC_LOWER_RIGHT) {
00154     u8g_DrawVLine(u8g, x0, y0, rad+1);
00155   }
00156   else {
00157     u8g_DrawVLine(u8g, x0, y0 - rad, 2*rad+1);
00158   }
00159   
00160   while( x < y )
00161   {
00162     if(f >= 0) 
00163     {
00164       y--;
00165       ddF_y += 2;
00166       f += ddF_y;
00167     }
00168     x++;
00169     ddF_x += 2;
00170     f += ddF_x;
00171     
00172     //Draw vertical lines from one point to another
00173     
00174     switch (option)
00175     {
00176         case U8G_CIRC_UPPER_RIGHT:
00177             u8g_DrawVLine(u8g, x0+x, y0-y, y+1);
00178             u8g_DrawVLine(u8g, x0+y, y0-x, x+1);
00179             break;
00180         case U8G_CIRC_UPPER_LEFT:
00181             u8g_DrawVLine(u8g, x0-x, y0-y, y+1);
00182             u8g_DrawVLine(u8g, x0-y, y0-x, x+1);
00183             break;
00184         case U8G_CIRC_LOWER_RIGHT:
00185             u8g_DrawVLine(u8g, x0+x, y0, y+1);
00186             u8g_DrawVLine(u8g, x0+y, y0, x+1);
00187             break;
00188         case U8G_CIRC_LOWER_LEFT:
00189             u8g_DrawVLine(u8g, x0-x, y0, y+1);
00190             u8g_DrawVLine(u8g, x0-y, y0, x+1);
00191             break;
00192         case U8G_CIRC_ALL:
00193             u8g_DrawVLine(u8g, x0+x, y0-y, 2*y+1);
00194             u8g_DrawVLine(u8g, x0-x, y0-y, 2*y+1);
00195             u8g_DrawVLine(u8g, x0+y, y0-x, 2*x+1);
00196             u8g_DrawVLine(u8g, x0-y, y0-x, 2*x+1);
00197             break;
00198     }
00199   }
00200 }
00201 
00202 #endif 
00203 
00204 /*=========================================================================*/
00205 
00206 static void u8g_draw_circle_section(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t x0, u8g_uint_t y0, uint8_t option) U8G_NOINLINE;
00207 
00208 static void u8g_draw_circle_section(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t x0, u8g_uint_t y0, uint8_t option)
00209 {
00210     /* upper right */
00211     if ( option & U8G_DRAW_UPPER_RIGHT )
00212     {
00213       u8g_DrawPixel(u8g, x0 + x, y0 - y);
00214       u8g_DrawPixel(u8g, x0 + y, y0 - x);
00215     }
00216     
00217     /* upper left */
00218     if ( option & U8G_DRAW_UPPER_LEFT )
00219     {
00220       u8g_DrawPixel(u8g, x0 - x, y0 - y);
00221       u8g_DrawPixel(u8g, x0 - y, y0 - x);
00222     }
00223     
00224     /* lower right */
00225     if ( option & U8G_DRAW_LOWER_RIGHT )
00226     {
00227       u8g_DrawPixel(u8g, x0 + x, y0 + y);
00228       u8g_DrawPixel(u8g, x0 + y, y0 + x);
00229     }
00230     
00231     /* lower left */
00232     if ( option & U8G_DRAW_LOWER_LEFT )
00233     {
00234       u8g_DrawPixel(u8g, x0 - x, y0 + y);
00235       u8g_DrawPixel(u8g, x0 - y, y0 + x);
00236     }
00237 }
00238 
00239 void u8g_draw_circle(u8g_t *u8g, u8g_uint_t x0, u8g_uint_t y0, u8g_uint_t rad, uint8_t option)
00240 {
00241     u8g_int_t f;
00242     u8g_int_t ddF_x;
00243     u8g_int_t ddF_y;
00244     u8g_uint_t x;
00245     u8g_uint_t y;
00246 
00247     f = 1;
00248     f -= rad;
00249     ddF_x = 1;
00250     ddF_y = 0;
00251     ddF_y -= rad;
00252     ddF_y *= 2;
00253     x = 0;
00254     y = rad;
00255 
00256     u8g_draw_circle_section(u8g, x, y, x0, y0, option);
00257     
00258     while ( x < y )
00259     {
00260       if (f >= 0) 
00261       {
00262         y--;
00263         ddF_y += 2;
00264         f += ddF_y;
00265       }
00266       x++;
00267       ddF_x += 2;
00268       f += ddF_x;
00269 
00270       u8g_draw_circle_section(u8g, x, y, x0, y0, option);    
00271     }
00272 }
00273 
00274 void u8g_DrawCircle(u8g_t *u8g, u8g_uint_t x0, u8g_uint_t y0, u8g_uint_t rad, uint8_t option)
00275 {
00276   /* check for bounding box */
00277   {
00278     u8g_uint_t radp, radp2;
00279     
00280     radp = rad;
00281     radp++;
00282     radp2 = radp;
00283     radp2 *= 2;
00284     
00285     if ( u8g_IsBBXIntersection(u8g, x0-radp, y0-radp, radp2, radp2) == 0)
00286       return;    
00287   }
00288   
00289   /* draw circle */
00290   u8g_draw_circle(u8g, x0, y0, rad, option);
00291 }
00292 
00293 static void u8g_draw_disc_section(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t x0, u8g_uint_t y0, uint8_t option) U8G_NOINLINE;
00294 
00295 static void u8g_draw_disc_section(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t x0, u8g_uint_t y0, uint8_t option)
00296 {
00297     /* upper right */
00298     if ( option & U8G_DRAW_UPPER_RIGHT )
00299     {
00300       u8g_DrawVLine(u8g, x0+x, y0-y, y+1);
00301       u8g_DrawVLine(u8g, x0+y, y0-x, x+1);
00302     }
00303     
00304     /* upper left */
00305     if ( option & U8G_DRAW_UPPER_LEFT )
00306     {
00307       u8g_DrawVLine(u8g, x0-x, y0-y, y+1);
00308       u8g_DrawVLine(u8g, x0-y, y0-x, x+1);
00309     }
00310     
00311     /* lower right */
00312     if ( option & U8G_DRAW_LOWER_RIGHT )
00313     {
00314       u8g_DrawVLine(u8g, x0+x, y0, y+1);
00315       u8g_DrawVLine(u8g, x0+y, y0, x+1);
00316     }
00317     
00318     /* lower left */
00319     if ( option & U8G_DRAW_LOWER_LEFT )
00320     {
00321       u8g_DrawVLine(u8g, x0-x, y0, y+1);
00322       u8g_DrawVLine(u8g, x0-y, y0, x+1);
00323     }
00324 }
00325 
00326 void u8g_draw_disc(u8g_t *u8g, u8g_uint_t x0, u8g_uint_t y0, u8g_uint_t rad, uint8_t option)
00327 {
00328   u8g_int_t f;
00329   u8g_int_t ddF_x;
00330   u8g_int_t ddF_y;
00331   u8g_uint_t x;
00332   u8g_uint_t y;
00333 
00334   f = 1;
00335   f -= rad;
00336   ddF_x = 1;
00337   ddF_y = 0;
00338   ddF_y -= rad;
00339   ddF_y *= 2;
00340   x = 0;
00341   y = rad;
00342 
00343   u8g_draw_disc_section(u8g, x, y, x0, y0, option);
00344   
00345   while ( x < y )
00346   {
00347     if (f >= 0) 
00348     {
00349       y--;
00350       ddF_y += 2;
00351       f += ddF_y;
00352     }
00353     x++;
00354     ddF_x += 2;
00355     f += ddF_x;
00356 
00357     u8g_draw_disc_section(u8g, x, y, x0, y0, option);    
00358   }
00359 }
00360 
00361 void u8g_DrawDisc(u8g_t *u8g, u8g_uint_t x0, u8g_uint_t y0, u8g_uint_t rad, uint8_t option)
00362 {
00363   /* check for bounding box */
00364   {
00365     u8g_uint_t radp, radp2;
00366     
00367     radp = rad;
00368     radp++;
00369     radp2 = radp;
00370     radp2 *= 2;
00371     
00372     if ( u8g_IsBBXIntersection(u8g, x0-radp, y0-radp, radp2, radp2) == 0)
00373       return;    
00374   }
00375   
00376   /* draw disc */
00377   u8g_draw_disc(u8g, x0, y0, rad, option);
00378 }
00379 
00380 
00381 
00382 
00383