1 | /* |
---|
2 | * libcaca ASCII-Art library |
---|
3 | * Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
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. |
---|
10 | * |
---|
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. |
---|
15 | * |
---|
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 |
---|
19 | * 02111-1307 USA |
---|
20 | */ |
---|
21 | |
---|
22 | /** \file triangle.c |
---|
23 | * \version \$Id: triangle.c 257 2003-12-18 00:11:52Z sam $ |
---|
24 | * \author Sam Hocevar <sam@zoy.org> |
---|
25 | * \brief Triangle drawing functions |
---|
26 | * |
---|
27 | * This file contains triangle drawing functions, both filled and outline. |
---|
28 | */ |
---|
29 | |
---|
30 | #include "config.h" |
---|
31 | |
---|
32 | #include <stdlib.h> |
---|
33 | |
---|
34 | #include "caca.h" |
---|
35 | #include "caca_internals.h" |
---|
36 | |
---|
37 | /** |
---|
38 | * \brief Draw a triangle on the screen using the given character. |
---|
39 | * |
---|
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. |
---|
47 | * \return void |
---|
48 | */ |
---|
49 | void caca_draw_triangle(int x1, int y1, int x2, int y2, int x3, int y3, char c) |
---|
50 | { |
---|
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); |
---|
54 | } |
---|
55 | |
---|
56 | /** |
---|
57 | * \brief Draw a thin triangle on the screen. |
---|
58 | * |
---|
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. |
---|
65 | * \return void |
---|
66 | */ |
---|
67 | void caca_draw_thin_triangle(int x1, int y1, int x2, int y2, int x3, int y3) |
---|
68 | { |
---|
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); |
---|
72 | } |
---|
73 | |
---|
74 | /** |
---|
75 | * \brief Fill a triangle on the screen using the given character. |
---|
76 | * |
---|
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. |
---|
84 | * \return void |
---|
85 | */ |
---|
86 | void caca_fill_triangle(int x1, int y1, int x2, int y2, int x3, int y3, char c) |
---|
87 | { |
---|
88 | int x, y, xa, xb, xmax, ymax; |
---|
89 | |
---|
90 | /* Bubble-sort y1 <= y2 <= y3 */ |
---|
91 | if(y1 > y2) |
---|
92 | { |
---|
93 | caca_fill_triangle(x2, y2, x1, y1, x3, y3, c); |
---|
94 | return; |
---|
95 | } |
---|
96 | |
---|
97 | if(y2 > y3) |
---|
98 | { |
---|
99 | caca_fill_triangle(x1, y1, x3, y3, x2, y2, c); |
---|
100 | return; |
---|
101 | } |
---|
102 | |
---|
103 | /* Promote precision */ |
---|
104 | x1 *= 4; |
---|
105 | x2 *= 4; |
---|
106 | x3 *= 4; |
---|
107 | |
---|
108 | xmax = _caca_width - 1; |
---|
109 | ymax = _caca_height - 1; |
---|
110 | |
---|
111 | /* Rasterize our triangle */ |
---|
112 | for(y = y1 < 0 ? 0 : y1; y <= y3 && y <= ymax; y++) |
---|
113 | { |
---|
114 | if(y <= y2) |
---|
115 | { |
---|
116 | xa = (y1 == y2) ? x2 : x1 + (x2 - x1) * (y - y1) / (y2 - y1); |
---|
117 | xb = (y1 == y3) ? x3 : x1 + (x3 - x1) * (y - y1) / (y3 - y1); |
---|
118 | } |
---|
119 | else |
---|
120 | { |
---|
121 | xa = (y3 == y2) ? x2 : x3 + (x2 - x3) * (y - y3) / (y2 - y3); |
---|
122 | xb = (y3 == y1) ? x1 : x3 + (x1 - x3) * (y - y3) / (y1 - y3); |
---|
123 | } |
---|
124 | |
---|
125 | if(xb < xa) |
---|
126 | { |
---|
127 | int tmp = xb; |
---|
128 | xb = xa; xa = tmp; |
---|
129 | } |
---|
130 | |
---|
131 | /* Rescale xa and xb, slightly cropping */ |
---|
132 | xa = (xa + 2) / 4; |
---|
133 | xb = (xb - 2) / 4; |
---|
134 | |
---|
135 | if(xb < 0) continue; |
---|
136 | if(xa > xmax) continue; |
---|
137 | if(xa < 0) xa = 0; |
---|
138 | if(xb > xmax) xb = xmax; |
---|
139 | |
---|
140 | for(x = xa; x <= xb; x++) |
---|
141 | caca_putchar(x, y, c); |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|