Browse Source

* libee/triangle.c:

+ Implemented ee_fill_triangle().
  * test/demo.c:
    + Triangle demo.


git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/ttyvaders/trunk@106 92316355-f0b4-4df1-b90c-862c8a59935f
master
sam 21 years ago
parent
commit
3d9085be01
5 changed files with 152 additions and 5 deletions
  1. +3
    -2
      libee/Makefile.am
  2. +2
    -1
      libee/ee.h
  3. +90
    -0
      libee/triangle.c
  4. +1
    -1
      test/Makefile.am
  5. +56
    -1
      test/demo.c

+ 3
- 2
libee/Makefile.am View File

@@ -19,9 +19,10 @@ libee_a_SOURCES = \
ee.c \
ee.h \
io.c \
circle.c \
line.c \
math.c \
line.c \
circle.c \
triangle.c \
$(NULL)
libee_a_CPPFLAGS = $(CPPFLAGS_slang) $(CPPFLAGS_ncurses)


+ 2
- 1
libee/ee.h View File

@@ -73,9 +73,10 @@ void ee_end(void);

char ee_get_key(void);

void ee_draw_circle(int, int, int, char);
void ee_draw_line(int, int, int, int, char);
void ee_draw_thin_line(int, int, int, int);
void ee_draw_circle(int, int, int, char);
void ee_fill_triangle(int, int, int, int, int, int, char);

int ee_rand(int, int);
int ee_sqrt(int);


+ 90
- 0
libee/triangle.c View File

@@ -0,0 +1,90 @@
/*
* libee ASCII-Art library
* Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org>
* All Rights Reserved
*
* $Id$
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "config.h"

#include <stdlib.h>

#include "ee.h"

void ee_fill_triangle(int x1, int y1, int x2, int y2, int x3, int y3, char c)
{
int x, y, xa, xb, xmax, ymax;

/* Bubble-sort y1 <= y2 <= y3 */
if(y1 > y2)
{
ee_fill_triangle(x2, y2, x1, y1, x3, y3, c);
return;
}

if(y2 > y3)
{
ee_fill_triangle(x1, y1, x3, y3, x2, y2, c);
return;
}

/* Promote precision */
x1 *= 4;
x2 *= 4;
x3 *= 4;

xmax = ee_get_width() - 1;
ymax = ee_get_height() - 1;

/* Rasterize our triangle */
for(y = y1 < 0 ? 0 : y1; y <= y3 && y <= ymax; y++)
{
if(y <= y2)
{
xa = (y1 == y2) ? x2 : x1 + (x2 - x1) * (y - y1) / (y2 - y1);
xb = (y1 == y3) ? x3 : x1 + (x3 - x1) * (y - y1) / (y3 - y1);
}
else
{
xa = (y3 == y2) ? x2 : x3 + (x2 - x3) * (y - y3) / (y2 - y3);
xb = (y3 == y1) ? x1 : x3 + (x1 - x3) * (y - y3) / (y1 - y3);
}

if(xb < xa)
{
int tmp = xb;
xb = xa; xa = tmp;
}

/* Rescale xa and xb, slightly cropping */
xa = (xa + 2) / 4;
xb = (xb - 2) / 4;

if(xb < 0) continue;
if(xa > xmax) continue;
if(xa < 0) xa = 0;
if(xb > xmax) xb = xmax;

for(x = xa; x <= xb; x++)
{
ee_goto(x, y);
ee_putchar(c);
}
}
}


+ 1
- 1
test/Makefile.am View File

@@ -20,5 +20,5 @@ bin_PROGRAMS = demo
demo_SOURCES = demo.c

demo_CPPFLAGS = -I../libee $(CPPFLAGS_slang) $(CPPFLAGS_ncurses)
demo_LDADD = ../libee/libee.a $(LDFLAGS_slang) $(LDFLAGS_ncurses)
demo_LDADD = ../libee/libee.a $(LDFLAGS_slang) $(LDFLAGS_ncurses) -lm


+ 56
- 1
test/demo.c View File

@@ -20,6 +20,8 @@
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "config.h"

#include <math.h>
#include <string.h>

@@ -31,6 +33,7 @@ static void demo_dots(void);
static void demo_lines(void);
static void demo_thin_lines(void);
static void demo_circles(void);
static void demo_triangle(void);
static void demo_radar(void);

int clipping = 0;
@@ -81,6 +84,10 @@ int main(int argc, char **argv)
demo = demo_circles;
break;
case '5':
ee_clear();
demo = demo_triangle;
break;
case '6':
ee_clear();
demo = demo_radar;
break;
@@ -123,7 +130,9 @@ static void display_menu(void)
ee_goto(4, 9);
ee_putstr("4: circles demo");
ee_goto(4, 10);
ee_putstr("5: radar demo");
ee_putstr("5: triangle demo");
ee_goto(4, 11);
ee_putstr("6: radar demo");

ee_goto(4, yo - 2);
ee_putstr("q: quit");
@@ -220,6 +229,52 @@ static void demo_circles(void)
ee_refresh();
}

static void demo_triangle(void)
{
static int i = 0;

int xo, yo, x1, y1, x2, y2, x3, y3;

i++;

xo = ee_get_width() * 5 / 8;
yo = 2;

x1 = ee_get_width() / 8 + sin(0.03*i) * 5;
y1 = ee_get_height() / 2 + cos(0.03*i) * 5;

x2 = ee_get_width() - 10 - cos(0.02*i) * 10;
y2 = ee_get_height() - 5 + sin(0.02*i) * 5;

x3 = ee_get_width() / 4 - sin(0.02*i) * 5;
y3 = ee_get_height() + cos(0.02*i) * 5;

ee_clear();

ee_color(EE_GREEN);
ee_fill_triangle(xo, yo, x2, y2, x1, y1, '%');
ee_color(EE_YELLOW);
ee_draw_thin_line(xo, yo, x2, y2);
ee_draw_thin_line(x2, y2, x1, y1);
ee_draw_thin_line(x1, y1, xo, yo);

ee_color(EE_RED);
ee_fill_triangle(x1, y1, x2, y2, x3, y3, '#');
ee_color(EE_YELLOW);
ee_draw_thin_line(x1, y1, x2, y2);
ee_draw_thin_line(x2, y2, x3, y3);
ee_draw_thin_line(x3, y3, x1, y1);

ee_color(EE_BLUE);
ee_fill_triangle(xo, yo, x2, y2, x3, y3, '%');
ee_color(EE_YELLOW);
ee_draw_thin_line(xo, yo, x2, y2);
ee_draw_thin_line(x2, y2, x3, y3);
ee_draw_thin_line(x3, y3, xo, yo);

ee_refresh();
}

static void demo_radar(void)
{
static int i = 0;


Loading…
Cancel
Save