1 | /* |
---|
2 | * libcaca ASCII-Art library |
---|
3 | * Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: triangle.c 192 2003-11-16 12:28:29Z sam $ |
---|
7 | * |
---|
8 | * This library is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU Lesser General Public |
---|
10 | * License as published by the Free Software Foundation; either |
---|
11 | * version 2 of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This library 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 GNU |
---|
16 | * Lesser General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU Lesser General Public |
---|
19 | * License along with this library; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
---|
21 | * 02111-1307 USA |
---|
22 | */ |
---|
23 | |
---|
24 | #include "config.h" |
---|
25 | |
---|
26 | #include <stdlib.h> |
---|
27 | |
---|
28 | #include "caca.h" |
---|
29 | #include "caca_internals.h" |
---|
30 | |
---|
31 | void caca_draw_triangle(int x1, int y1, int x2, int y2, int x3, int y3, char c) |
---|
32 | { |
---|
33 | caca_draw_line(x1, y1, x2, y2, c); |
---|
34 | caca_draw_line(x2, y2, x3, y3, c); |
---|
35 | caca_draw_line(x3, y3, x1, y1, c); |
---|
36 | } |
---|
37 | |
---|
38 | void caca_draw_thin_triangle(int x1, int y1, int x2, int y2, int x3, int y3) |
---|
39 | { |
---|
40 | caca_draw_thin_line(x1, y1, x2, y2); |
---|
41 | caca_draw_thin_line(x2, y2, x3, y3); |
---|
42 | caca_draw_thin_line(x3, y3, x1, y1); |
---|
43 | } |
---|
44 | |
---|
45 | void caca_fill_triangle(int x1, int y1, int x2, int y2, int x3, int y3, char c) |
---|
46 | { |
---|
47 | int x, y, xa, xb, xmax, ymax; |
---|
48 | |
---|
49 | /* Bubble-sort y1 <= y2 <= y3 */ |
---|
50 | if(y1 > y2) |
---|
51 | { |
---|
52 | caca_fill_triangle(x2, y2, x1, y1, x3, y3, c); |
---|
53 | return; |
---|
54 | } |
---|
55 | |
---|
56 | if(y2 > y3) |
---|
57 | { |
---|
58 | caca_fill_triangle(x1, y1, x3, y3, x2, y2, c); |
---|
59 | return; |
---|
60 | } |
---|
61 | |
---|
62 | /* Promote precision */ |
---|
63 | x1 *= 4; |
---|
64 | x2 *= 4; |
---|
65 | x3 *= 4; |
---|
66 | |
---|
67 | xmax = caca_get_width() - 1; |
---|
68 | ymax = caca_get_height() - 1; |
---|
69 | |
---|
70 | /* Rasterize our triangle */ |
---|
71 | for(y = y1 < 0 ? 0 : y1; y <= y3 && y <= ymax; y++) |
---|
72 | { |
---|
73 | if(y <= y2) |
---|
74 | { |
---|
75 | xa = (y1 == y2) ? x2 : x1 + (x2 - x1) * (y - y1) / (y2 - y1); |
---|
76 | xb = (y1 == y3) ? x3 : x1 + (x3 - x1) * (y - y1) / (y3 - y1); |
---|
77 | } |
---|
78 | else |
---|
79 | { |
---|
80 | xa = (y3 == y2) ? x2 : x3 + (x2 - x3) * (y - y3) / (y2 - y3); |
---|
81 | xb = (y3 == y1) ? x1 : x3 + (x1 - x3) * (y - y3) / (y1 - y3); |
---|
82 | } |
---|
83 | |
---|
84 | if(xb < xa) |
---|
85 | { |
---|
86 | int tmp = xb; |
---|
87 | xb = xa; xa = tmp; |
---|
88 | } |
---|
89 | |
---|
90 | /* Rescale xa and xb, slightly cropping */ |
---|
91 | xa = (xa + 2) / 4; |
---|
92 | xb = (xb - 2) / 4; |
---|
93 | |
---|
94 | if(xb < 0) continue; |
---|
95 | if(xa > xmax) continue; |
---|
96 | if(xa < 0) xa = 0; |
---|
97 | if(xb > xmax) xb = xmax; |
---|
98 | |
---|
99 | for(x = xa; x <= xb; x++) |
---|
100 | caca_putchar(x, y, c); |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|