Changeset 151


Ignore:
Timestamp:
Nov 11, 2003, 11:22:19 AM (20 years ago)
Author:
Sam Hocevar
Message:
  • libee/triangle.c: + Wrote ee_draw_triangle() and ee_draw_thin_triangle().
  • libee/box.c: + Wrote ee_draw_box(), ee_draw_thin_box() and ee_fill_box().
  • libee/conic.c: + Wrote ee_fill_ellipse(). + First attempt at ee_draw_thin_ellipse(), to be reworked.
  • test/demo.c: + Merged demo_lines() and demo_thin_lines(). + Merged demo_triangles() and demo_outlined_triangles(). + Wrote demo_box(). + Use ee_fill_ellipse() in demo_all().
Location:
libcaca/trunk
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/libee/Makefile.am

    r147 r151  
    1111        math.c \
    1212        line.c \
     13        box.c \
    1314        conic.c \
    1415        triangle.c \
  • libcaca/trunk/libee/conic.c

    r147 r151  
    5050}
    5151
    52 void ee_draw_ellipse(int xo, int yo, int a, int b, char c)
     52void ee_fill_ellipse(int xo, int yo, int a, int b, char c)
    5353{
    5454    int d2;
     
    5757    int d1 = b*b - (a*a*b) + (a*a/4);
    5858
     59    while( a*a*y - a*a/2 > b*b*(x+1))
     60    {
     61        if(d1 < 0)
     62        {
     63            d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
     64        }
     65        else
     66        {
     67            d1 += b*b*(2*x*1) + a*a*(-2*y+2);
     68            ee_draw_line(xo - x, yo - y, xo + x, yo - y, c);
     69            ee_draw_line(xo - x, yo + y, xo + x, yo + y, c);
     70            y--;
     71        }
     72        x++;
     73    }
     74
     75    ee_draw_line(xo - x, yo - y, xo + x, yo - y, c);
     76    ee_draw_line(xo - x, yo + y, xo + x, yo + y, c);
     77
     78    d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
     79    while(y > 0)
     80    {
     81        if(d2 < 0)
     82        {
     83            d2 += b*b*(2*x+2) + a*a*(-2*y+3);
     84            x++;
     85        }
     86        else
     87        {
     88            d2 += a*a*(-2*y+3);
     89        }
     90
     91        y--;
     92        ee_draw_line(xo - x, yo - y, xo + x, yo - y, c);
     93        ee_draw_line(xo - x, yo + y, xo + x, yo + y, c);
     94    }
     95}
     96
     97void ee_draw_ellipse(int xo, int yo, int a, int b, char c)
     98{
     99    int d2;
     100    int x = 0;
     101    int y = b;
     102    int d1 = b*b - (a*a*b) + (a*a/4);
     103
    59104    ellipsepoints(xo, yo, x, y, c);
    60105
     
    89134        y--;
    90135        ellipsepoints(xo, yo, x, y, c);
     136    }
     137}
     138
     139void ee_draw_thin_ellipse(int xo, int yo, int a, int b)
     140{
     141    /* FIXME: this is not correct */
     142    int d2;
     143    int x = 0;
     144    int y = b;
     145    int d1 = b*b - (a*a*b) + (a*a/4);
     146
     147    ellipsepoints(xo, yo, x, y, '-');
     148
     149    while( a*a*y - a*a/2 > b*b*(x+1))
     150    {
     151        if(d1 < 0)
     152        {
     153            d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
     154        }
     155        else
     156        {
     157            d1 += b*b*(2*x*1) + a*a*(-2*y+2);
     158            y--;
     159        }
     160        x++;
     161        ellipsepoints(xo, yo, x, y, '-');
     162    }
     163
     164    d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
     165    while(y > 0)
     166    {
     167        if(d2 < 0)
     168        {
     169            d2 += b*b*(2*x+2) + a*a*(-2*y+3);
     170            x++;
     171        }
     172        else
     173        {
     174            d2 += a*a*(-2*y+3);
     175        }
     176
     177        y--;
     178        ellipsepoints(xo, yo, x, y, '|');
    91179    }
    92180}
  • libcaca/trunk/libee/ee.h

    r147 r151  
    5959void ee_draw_line(int, int, int, int, char);
    6060void ee_draw_thin_line(int, int, int, int);
     61
    6162void ee_draw_circle(int, int, int, char);
    6263void ee_draw_ellipse(int, int, int, int, char);
     64void ee_draw_thin_ellipse(int, int, int, int);
     65void ee_fill_ellipse(int, int, int, int, char);
     66
     67void ee_draw_box(int, int, int, int, char);
     68void ee_draw_thin_box(int, int, int, int);
     69void ee_fill_box(int, int, int, int, char);
     70
     71void ee_draw_triangle(int, int, int, int, int, int, char);
     72void ee_draw_thin_triangle(int, int, int, int, int, int);
    6373void ee_fill_triangle(int, int, int, int, int, int, char);
    6474
  • libcaca/trunk/libee/triangle.c

    r147 r151  
    3232
    3333#include "ee.h"
     34
     35void ee_draw_triangle(int x1, int y1, int x2, int y2, int x3, int y3, char c)
     36{
     37    ee_draw_line(x1, y1, x2, y2, c);
     38    ee_draw_line(x2, y2, x3, y3, c);
     39    ee_draw_line(x3, y3, x1, y1, c);
     40}
     41
     42void ee_draw_thin_triangle(int x1, int y1, int x2, int y2, int x3, int y3)
     43{
     44    ee_draw_thin_line(x1, y1, x2, y2);
     45    ee_draw_thin_line(x2, y2, x3, y3);
     46    ee_draw_thin_line(x3, y3, x1, y1);
     47}
    3448
    3549void ee_fill_triangle(int x1, int y1, int x2, int y2, int x3, int y3, char c)
  • libcaca/trunk/test/demo.c

    r147 r151  
    3434static void demo_dots(void);
    3535static void demo_lines(void);
    36 static void demo_thin_lines(void);
    37 static void demo_circles(void);
     36static void demo_boxes(void);
    3837static void demo_ellipses(void);
    3938static void demo_triangles(void);
    40 static void demo_outlined_triangles(void);
    4139static void demo_sprites(void);
    4240
    4341int force_clipping = 0;
     42int outline = 0;
     43int thin = 0;
    4444struct ee_sprite *sprite = NULL;
    4545
     
    8888                ee_clear();
    8989                demo = demo_lines;
     90                thin = 0;
    9091                break;
    9192            case '3':
    9293                ee_clear();
    93                 demo = demo_thin_lines;
     94                demo = demo_lines;
     95                thin = 1;
    9496                break;
    9597            case '4':
    9698                ee_clear();
    97                 demo = demo_circles;
     99                demo = demo_boxes;
     100                outline = 0;
    98101                break;
    99102            case '5':
    100103                ee_clear();
     104                demo = demo_boxes;
     105                outline = 1;
     106                break;
     107            case '6':
     108                ee_clear();
    101109                demo = demo_ellipses;
    102110                break;
    103             case '6':
     111            case '7':
    104112                ee_clear();
    105113                demo = demo_triangles;
    106                 break;
    107             case '7':
    108                 ee_clear();
    109                 demo = demo_outlined_triangles;
     114                outline = 0;
    110115                break;
    111116            case '8':
     117                ee_clear();
     118                demo = demo_triangles;
     119                outline = 1;
     120                break;
     121            case '9':
    112122                ee_clear();
    113123                demo = demo_sprites;
     
    146156    ee_putstr(4, 8, "2: lines demo");
    147157    ee_putstr(4, 9, "3: thin lines demo");
    148     ee_putstr(4, 10, "4: circles demo");
    149     ee_putstr(4, 11, "5: ellipses demo");
    150     ee_putstr(4, 12, "6: triangles demo");
    151     ee_putstr(4, 13, "7: outlined triangles demo");
    152     ee_putstr(4, 14, "8: sprites demo");
     158    ee_putstr(4, 10, "4: boxes demo");
     159    ee_putstr(4, 11, "5: outlined boxes demo");
     160    ee_putstr(4, 12, "6: ellipses demo");
     161    ee_putstr(4, 13, "7: triangles demo");
     162    ee_putstr(4, 14, "8: outlined triangles demo");
     163    ee_putstr(4, 15, "9: sprites demo");
    153164
    154165    ee_putstr(4, yo - 2, "q: quit");
     
    179190    }
    180191
     192    j = 15 + sin(0.03*i) * 8;
    181193    ee_color(EE_WHITE);
    182     for(j = 15 + sin(0.03*i) * 8; j--;)
    183     {
    184         ee_draw_circle(xo, yo, j, '#');
    185     }
    186 
     194    ee_fill_ellipse(xo, yo, j, j / 2, '#');
    187195    ee_color(EE_YELLOW);
    188     ee_draw_circle(xo, yo, 15 + sin(0.03*i) * 8, '#');
     196    ee_draw_ellipse(xo, yo, j, j / 2, '#');
    189197
    190198    /* Draw the pyramid */
     
    204212    ee_fill_triangle(xo, yo, x2, y2, x1, y1, '%');
    205213    ee_color(EE_YELLOW);
    206     ee_draw_thin_line(xo, yo, x2, y2);
    207     ee_draw_thin_line(x2, y2, x1, y1);
    208     ee_draw_thin_line(x1, y1, xo, yo);
     214    ee_draw_thin_triangle(xo, yo, x2, y2, x1, y1);
    209215
    210216    ee_color(EE_RED);
    211217    ee_fill_triangle(x1, y1, x2, y2, x3, y3, '#');
    212218    ee_color(EE_YELLOW);
    213     ee_draw_thin_line(x1, y1, x2, y2);
    214     ee_draw_thin_line(x2, y2, x3, y3);
    215     ee_draw_thin_line(x3, y3, x1, y1);
     219    ee_draw_thin_triangle(x1, y1, x2, y2, x3, y3);
    216220
    217221    ee_color(EE_BLUE);
    218222    ee_fill_triangle(xo, yo, x2, y2, x3, y3, '%');
    219223    ee_color(EE_YELLOW);
    220     ee_draw_thin_line(xo, yo, x2, y2);
    221     ee_draw_thin_line(x2, y2, x3, y3);
    222     ee_draw_thin_line(x3, y3, xo, yo);
     224    ee_draw_thin_triangle(xo, yo, x2, y2, x3, y3);
    223225
    224226    /* Draw a background triangle */
     
    232234    y3 = ee_get_height() - 3;
    233235
    234     ee_color(EE_BLUE);
    235 //    ee_fill_triangle(x1, y1, x2, y2, x3, y3, '.');
    236236    ee_color(EE_CYAN);
    237     ee_draw_thin_line(x1, y1, x3, y3);
    238     ee_draw_thin_line(x2, y2, x1, y1);
    239     ee_draw_thin_line(x3, y3, x2, y2);
     237    ee_draw_thin_triangle(x1, y1, x2, y2, x3, y3);
    240238
    241239    xo = ee_get_width() / 2 + cos(0.027*i) * ee_get_width() / 3;
     
    246244    ee_draw_thin_line(x3, y3, xo, yo);
    247245
    248     /* Draw a sprite behind the pyramid */
     246    /* Draw a sprite on the pyramid */
    249247    ee_draw_sprite(xo, yo, sprite);
    250248
     
    288286    int w = ee_get_width();
    289287    int h = ee_get_height();
    290 
    291     /* Draw lines */
     288    int x1, y1, x2, y2;
     289
     290    if(force_clipping)
     291    {
     292        x1 = ee_rand(- w, 2 * w); y1 = ee_rand(- h, 2 * h);
     293        x2 = ee_rand(- w, 2 * w); y2 = ee_rand(- h, 2 * h);
     294    }
     295    else
     296    {
     297        x1 = ee_rand(0, w - 1); y1 = ee_rand(0, h - 1);
     298        x2 = ee_rand(0, w - 1); y2 = ee_rand(0, h - 1);
     299    }
     300
    292301    ee_color(ee_rand(1, 10));
    293     if(force_clipping)
    294     {
    295         ee_draw_line(ee_rand(- w, 2 * w), ee_rand(- h, 2 * h),
    296                      ee_rand(- w, 2 * w), ee_rand(- h, 2 * h), '#');
    297     }
     302    if(thin)
     303        ee_draw_thin_line(x1, y1, x2, y2);
    298304    else
    299     {
    300         ee_draw_line(ee_rand(0, w - 1), ee_rand(0, h - 1),
    301                      ee_rand(0, w - 1), ee_rand(0, h - 1), '#');
    302     }
    303     ee_refresh();
    304 }
    305 
    306 static void demo_thin_lines(void)
     305        ee_draw_line(x1, y1, x2, y2, '#');
     306
     307    ee_refresh();
     308}
     309
     310static void demo_boxes(void)
    307311{
    308312    int w = ee_get_width();
    309313    int h = ee_get_height();
    310 
    311     /* Draw lines */
     314    int x1, y1, x2, y2;
     315
     316    if(force_clipping)
     317    {
     318        x1 = ee_rand(- w, 2 * w); y1 = ee_rand(- h, 2 * h);
     319        x2 = ee_rand(- w, 2 * w); y2 = ee_rand(- h, 2 * h);
     320    }
     321    else
     322    {
     323        x1 = ee_rand(0, w - 1); y1 = ee_rand(0, h - 1);
     324        x2 = ee_rand(0, w - 1); y2 = ee_rand(0, h - 1);
     325    }
     326
    312327    ee_color(ee_rand(1, 10));
    313     if(force_clipping)
    314     {
    315         ee_draw_thin_line(ee_rand(- w, 2 * w), ee_rand(- h, 2 * h),
    316                           ee_rand(- w, 2 * w), ee_rand(- h, 2 * h));
    317     }
    318     else
    319     {
    320         ee_draw_thin_line(ee_rand(0, w), ee_rand(0, h),
    321                           ee_rand(0, w), ee_rand(0, h));
    322     }
    323     ee_refresh();
    324 }
    325 
    326 static void demo_circles(void)
     328    ee_fill_box(x1, y1, x2, y2, '#');
     329
     330    if(outline)
     331    {
     332        ee_color(ee_rand(1, 10));
     333        ee_draw_thin_box(x1, y1, x2, y2);
     334    }
     335
     336    ee_refresh();
     337}
     338
     339static void demo_ellipses(void)
    327340{
    328341    int w = ee_get_width();
    329342    int h = ee_get_height();
    330 
    331     /* Draw circles */
     343    int x, y, a, b;
     344
    332345    if(force_clipping)
    333346    {
     347        x = ee_rand(- w, 2 * w); y = ee_rand(- h, 2 * h);
     348        a = ee_rand(0, w); b = ee_rand(0, h);
     349    }
     350    else
     351    {
     352        do
     353        {
     354            x = ee_rand(0, w); y = ee_rand(0, h);
     355            a = ee_rand(0, w); b = ee_rand(0, h);
     356
     357        } while(x - a < 0 || x + a >= w || y - b < 0 || y + b >= h);
     358    }
     359
     360    ee_color(ee_rand(1, 10));
     361    ee_fill_ellipse(x, y, a, b, '#');
     362
     363    if(outline)
     364    {
    334365        ee_color(ee_rand(1, 10));
    335         ee_draw_circle(ee_rand(- w, 2 * w),
    336                        ee_rand(- h, 2 * h),
    337                        ee_rand(0, (w + h) / 2),
    338                        '#');
    339     }
    340     else
    341     {
    342         int x = ee_rand(0, w);
    343         int y = ee_rand(0, h);
    344         int r = ee_rand(0, (w + h) / 2);
    345 
    346         if(x - r < 0 || x + r >= w || y - r < 0 || y + r >= h)
    347         {
    348             demo_circles();
    349             return;
    350         }
    351 
    352         ee_color(ee_rand(1, 10));
    353         ee_draw_circle(x, y, r, '#');
    354     }
    355 
    356     ee_refresh();
    357 }
    358 
    359 static void demo_ellipses(void)
    360 {
    361     int w = ee_get_width();
    362     int h = ee_get_height();
    363 
    364     /* Draw circles */
    365     if(force_clipping)
    366     {
    367         ee_color(ee_rand(1, 10));
    368         ee_draw_ellipse(ee_rand(- w, 2 * w),
    369                         ee_rand(- h, 2 * h),
    370                         ee_rand(0, w),
    371                         ee_rand(0, h),
    372                         '#');
    373     }
    374     else
    375     {
    376         int x = ee_rand(0, w);
    377         int y = ee_rand(0, h);
    378         int a = ee_rand(0, w);
    379         int b = ee_rand(0, h);
    380 
    381         if(x - a < 0 || x + a >= w || y - b < 0 || y + b >= h)
    382         {
    383             demo_ellipses();
    384             return;
    385         }
    386 
    387         ee_color(ee_rand(1, 10));
    388         ee_draw_ellipse(x, y, a, b, '#');
     366        ee_draw_thin_ellipse(x, y, a, b);
    389367    }
    390368
     
    393371
    394372static void demo_triangles(void)
    395 {
    396     int w = ee_get_width();
    397     int h = ee_get_height();
    398 
    399     /* Draw lines */
    400     ee_color(ee_rand(1, 10));
    401     if(force_clipping)
    402     {
    403         ee_fill_triangle(ee_rand(- w, 2 * w), ee_rand(- h, 2 * h),
    404                          ee_rand(- w, 2 * w), ee_rand(- h, 2 * h),
    405                          ee_rand(- w, 2 * w), ee_rand(- h, 2 * h), '#');
    406     }
    407     else
    408     {
    409         ee_fill_triangle(ee_rand(0, w - 1), ee_rand(0, h - 1),
    410                          ee_rand(0, w - 1), ee_rand(0, h - 1),
    411                          ee_rand(0, w - 1), ee_rand(0, h - 1), '#');
    412     }
    413     ee_refresh();
    414 }
    415 
    416 static void demo_outlined_triangles(void)
    417373{
    418374    int w = ee_get_width();
     
    420376    int x1, y1, x2, y2, x3, y3;
    421377
    422     /* Draw lines */
     378    if(force_clipping)
     379    {
     380        x1 = ee_rand(- w, 2 * w); y1 = ee_rand(- h, 2 * h);
     381        x2 = ee_rand(- w, 2 * w); y2 = ee_rand(- h, 2 * h);
     382        x3 = ee_rand(- w, 2 * w); y3 = ee_rand(- h, 2 * h);
     383    }
     384    else
     385    {
     386
     387        x1 = ee_rand(0, w - 1); y1 = ee_rand(0, h - 1);
     388        x2 = ee_rand(0, w - 1); y2 = ee_rand(0, h - 1);
     389        x3 = ee_rand(0, w - 1); y3 = ee_rand(0, h - 1);
     390    }
     391
    423392    ee_color(ee_rand(1, 10));
    424     if(force_clipping)
    425     {
    426         x1 = ee_rand(- w, 2 * w);
    427         y1 = ee_rand(- h, 2 * h);
    428         x2 = ee_rand(- w, 2 * w);
    429         y2 = ee_rand(- h, 2 * h);
    430         x3 = ee_rand(- w, 2 * w);
    431         y3 = ee_rand(- h, 2 * h);
    432     }
    433     else
    434     {
    435 
    436         x1 = ee_rand(0, w - 1);
    437         y1 = ee_rand(0, h - 1);
    438         x2 = ee_rand(0, w - 1);
    439         y2 = ee_rand(0, h - 1);
    440         x3 = ee_rand(0, w - 1);
    441         y3 = ee_rand(0, h - 1);
    442     }
    443 
    444393    ee_fill_triangle(x1, y1, x2, y2, x3, y3, '#');
    445394
    446     ee_color(ee_rand(1, 10));
    447     ee_draw_thin_line(x1, y1, x2, y2);
    448     ee_draw_thin_line(x2, y2, x3, y3);
    449     ee_draw_thin_line(x3, y3, x1, y1);
     395    if(outline)
     396    {
     397        ee_color(ee_rand(1, 10));
     398        ee_draw_thin_triangle(x1, y1, x2, y2, x3, y3);
     399    }
    450400
    451401    ee_refresh();
     
    459409}
    460410
    461 static void demo_pyramid(void)
    462 {
    463     static int i = 0;
    464 
    465     int xo, yo, x1, y1, x2, y2, x3, y3;
    466 
    467     i++;
    468 
    469     xo = ee_get_width() * 5 / 8;
    470     yo = 2;
    471 
    472     x1 = ee_get_width() / 8 + sin(0.03*i) * 5;
    473     y1 = ee_get_height() / 2 + cos(0.03*i) * 5;
    474 
    475     x2 = ee_get_width() - 10 - cos(0.02*i) * 10;
    476     y2 = ee_get_height() - 5 + sin(0.02*i) * 5;
    477 
    478     x3 = ee_get_width() / 4 - sin(0.02*i) * 5;
    479     y3 = ee_get_height() + cos(0.02*i) * 5;
    480 
    481     ee_clear();
    482 
    483     ee_color(EE_GREEN);
    484     ee_fill_triangle(xo, yo, x2, y2, x1, y1, '%');
    485     ee_color(EE_YELLOW);
    486     ee_draw_thin_line(xo, yo, x2, y2);
    487     ee_draw_thin_line(x2, y2, x1, y1);
    488     ee_draw_thin_line(x1, y1, xo, yo);
    489 
    490     ee_color(EE_RED);
    491     ee_fill_triangle(x1, y1, x2, y2, x3, y3, '#');
    492     ee_color(EE_YELLOW);
    493     ee_draw_thin_line(x1, y1, x2, y2);
    494     ee_draw_thin_line(x2, y2, x3, y3);
    495     ee_draw_thin_line(x3, y3, x1, y1);
    496 
    497     ee_color(EE_BLUE);
    498     ee_fill_triangle(xo, yo, x2, y2, x3, y3, '%');
    499     ee_color(EE_YELLOW);
    500     ee_draw_thin_line(xo, yo, x2, y2);
    501     ee_draw_thin_line(x2, y2, x3, y3);
    502     ee_draw_thin_line(x3, y3, xo, yo);
    503 
    504     ee_refresh();
    505 }
    506 
Note: See TracChangeset for help on using the changeset viewer.