source: ttyvaders/trunk/libee/triangle.c @ 152

Last change on this file since 152 was 152, checked in by Sam Hocevar, 17 years ago
  • 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().
  • Property svn:keywords set to Id
File size: 2.7 KB
Line 
1/*
2 *   libee         ASCII-Art library
3 *   Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org>
4 *                 All Rights Reserved
5 *
6 *   $Id: triangle.c 152 2003-11-11 10:22:19Z sam $
7 *
8 *   This program is free software; you can redistribute it and/or modify
9 *   it under the terms of the GNU General Public License as published by
10 *   the Free Software Foundation; either version 2 of the License, or
11 *   (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include "config.h"
24
25#ifdef USE_SLANG
26#   include <slang.h>
27#elif USE_NCURSES
28#   include <curses.h>
29#endif
30
31#include <stdlib.h>
32
33#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}
48
49void ee_fill_triangle(int x1, int y1, int x2, int y2, int x3, int y3, char c)
50{
51    int x, y, xa, xb, xmax, ymax;
52
53    /* Bubble-sort y1 <= y2 <= y3 */
54    if(y1 > y2)
55    {
56        ee_fill_triangle(x2, y2, x1, y1, x3, y3, c);
57        return;
58    }
59
60    if(y2 > y3)
61    {
62        ee_fill_triangle(x1, y1, x3, y3, x2, y2, c);
63        return;
64    }
65
66    /* Promote precision */
67    x1 *= 4;
68    x2 *= 4;
69    x3 *= 4;
70
71    xmax = ee_get_width() - 1;
72    ymax = ee_get_height() - 1;
73
74    /* Rasterize our triangle */
75    for(y = y1 < 0 ? 0 : y1; y <= y3 && y <= ymax; y++)
76    {
77        if(y <= y2)
78        {
79            xa = (y1 == y2) ? x2 : x1 + (x2 - x1) * (y - y1) / (y2 - y1);
80            xb = (y1 == y3) ? x3 : x1 + (x3 - x1) * (y - y1) / (y3 - y1);
81        }
82        else
83        {
84            xa = (y3 == y2) ? x2 : x3 + (x2 - x3) * (y - y3) / (y2 - y3);
85            xb = (y3 == y1) ? x1 : x3 + (x1 - x3) * (y - y3) / (y1 - y3);
86        }
87
88        if(xb < xa)
89        {
90            int tmp = xb;
91            xb = xa; xa = tmp;
92        }
93
94        /* Rescale xa and xb, slightly cropping */
95        xa = (xa + 2) / 4;
96        xb = (xb - 2) / 4;
97
98        if(xb < 0) continue;
99        if(xa > xmax) continue;
100        if(xa < 0) xa = 0;
101        if(xb > xmax) xb = xmax;
102
103        for(x = xa; x <= xb; x++)
104            ee_putchar(x, y, c);
105    }
106}
107
Note: See TracBrowser for help on using the repository browser.