2
* libcaca ASCII-Art library
3
* Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org>
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Lesser General Public
8
* License as published by the Free Software Foundation; either
9
* version 2 of the License, or (at your option) any later version.
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Lesser General Public License for more details.
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with this library; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23
* \version \$Id: triangle.c 195 2003-12-31 14:21:08Z sam $
24
* \author Sam Hocevar <sam@zoy.org>
25
* \brief Triangle drawing
27
* This file contains triangle drawing functions, both filled and outline.
35
#include "caca_internals.h"
38
* \brief Draw a triangle on the screen using the given character.
40
* \param x1 X coordinate of the first point.
41
* \param y1 Y coordinate of the first point.
42
* \param x2 X coordinate of the second point.
43
* \param y2 Y coordinate of the second point.
44
* \param x3 X coordinate of the third point.
45
* \param y3 Y coordinate of the third point.
46
* \param c Character to draw the triangle outline with.
49
void caca_draw_triangle(int x1, int y1, int x2, int y2, int x3, int y3, char c)
51
caca_draw_line(x1, y1, x2, y2, c);
52
caca_draw_line(x2, y2, x3, y3, c);
53
caca_draw_line(x3, y3, x1, y1, c);
57
* \brief Draw a thin triangle on the screen.
59
* \param x1 X coordinate of the first point.
60
* \param y1 Y coordinate of the first point.
61
* \param x2 X coordinate of the second point.
62
* \param y2 Y coordinate of the second point.
63
* \param x3 X coordinate of the third point.
64
* \param y3 Y coordinate of the third point.
67
void caca_draw_thin_triangle(int x1, int y1, int x2, int y2, int x3, int y3)
69
caca_draw_thin_line(x1, y1, x2, y2);
70
caca_draw_thin_line(x2, y2, x3, y3);
71
caca_draw_thin_line(x3, y3, x1, y1);
75
* \brief Fill a triangle on the screen using the given character.
77
* \param x1 X coordinate of the first point.
78
* \param y1 Y coordinate of the first point.
79
* \param x2 X coordinate of the second point.
80
* \param y2 Y coordinate of the second point.
81
* \param x3 X coordinate of the third point.
82
* \param y3 Y coordinate of the third point.
83
* \param c Character to fill the triangle with.
86
void caca_fill_triangle(int x1, int y1, int x2, int y2, int x3, int y3, char c)
88
int x, y, xa, xb, xmax, ymax;
90
/* Bubble-sort y1 <= y2 <= y3 */
93
caca_fill_triangle(x2, y2, x1, y1, x3, y3, c);
99
caca_fill_triangle(x1, y1, x3, y3, x2, y2, c);
103
/* Promote precision */
108
xmax = _caca_width - 1;
109
ymax = _caca_height - 1;
111
/* Rasterize our triangle */
112
for(y = y1 < 0 ? 0 : y1; y <= y3 && y <= ymax; y++)
116
xa = (y1 == y2) ? x2 : x1 + (x2 - x1) * (y - y1) / (y2 - y1);
117
xb = (y1 == y3) ? x3 : x1 + (x3 - x1) * (y - y1) / (y3 - y1);
121
xa = (y3 == y2) ? x2 : x3 + (x2 - x3) * (y - y3) / (y2 - y3);
122
xb = (y3 == y1) ? x1 : x3 + (x1 - x3) * (y - y3) / (y1 - y3);
131
/* Rescale xa and xb, slightly cropping */
136
if(xa > xmax) continue;
138
if(xb > xmax) xb = xmax;
140
for(x = xa; x <= xb; x++)
141
caca_putchar(x, y, c);