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 205 2003-11-22 12:45:25Z 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 | void caca_draw_triangle(int x1, int y1, int x2, int y2, int x3, int y3, char c) |
---|
38 | { |
---|
39 | caca_draw_line(x1, y1, x2, y2, c); |
---|
40 | caca_draw_line(x2, y2, x3, y3, c); |
---|
41 | caca_draw_line(x3, y3, x1, y1, c); |
---|
42 | } |
---|
43 | |
---|
44 | void caca_draw_thin_triangle(int x1, int y1, int x2, int y2, int x3, int y3) |
---|
45 | { |
---|
46 | caca_draw_thin_line(x1, y1, x2, y2); |
---|
47 | caca_draw_thin_line(x2, y2, x3, y3); |
---|
48 | caca_draw_thin_line(x3, y3, x1, y1); |
---|
49 | } |
---|
50 | |
---|
51 | void caca_fill_triangle(int x1, int y1, int x2, int y2, int x3, int y3, char c) |
---|
52 | { |
---|
53 | int x, y, xa, xb, xmax, ymax; |
---|
54 | |
---|
55 | /* Bubble-sort y1 <= y2 <= y3 */ |
---|
56 | if(y1 > y2) |
---|
57 | { |
---|
58 | caca_fill_triangle(x2, y2, x1, y1, x3, y3, c); |
---|
59 | return; |
---|
60 | } |
---|
61 | |
---|
62 | if(y2 > y3) |
---|
63 | { |
---|
64 | caca_fill_triangle(x1, y1, x3, y3, x2, y2, c); |
---|
65 | return; |
---|
66 | } |
---|
67 | |
---|
68 | /* Promote precision */ |
---|
69 | x1 *= 4; |
---|
70 | x2 *= 4; |
---|
71 | x3 *= 4; |
---|
72 | |
---|
73 | xmax = caca_get_width() - 1; |
---|
74 | ymax = caca_get_height() - 1; |
---|
75 | |
---|
76 | /* Rasterize our triangle */ |
---|
77 | for(y = y1 < 0 ? 0 : y1; y <= y3 && y <= ymax; y++) |
---|
78 | { |
---|
79 | if(y <= y2) |
---|
80 | { |
---|
81 | xa = (y1 == y2) ? x2 : x1 + (x2 - x1) * (y - y1) / (y2 - y1); |
---|
82 | xb = (y1 == y3) ? x3 : x1 + (x3 - x1) * (y - y1) / (y3 - y1); |
---|
83 | } |
---|
84 | else |
---|
85 | { |
---|
86 | xa = (y3 == y2) ? x2 : x3 + (x2 - x3) * (y - y3) / (y2 - y3); |
---|
87 | xb = (y3 == y1) ? x1 : x3 + (x1 - x3) * (y - y3) / (y1 - y3); |
---|
88 | } |
---|
89 | |
---|
90 | if(xb < xa) |
---|
91 | { |
---|
92 | int tmp = xb; |
---|
93 | xb = xa; xa = tmp; |
---|
94 | } |
---|
95 | |
---|
96 | /* Rescale xa and xb, slightly cropping */ |
---|
97 | xa = (xa + 2) / 4; |
---|
98 | xb = (xb - 2) / 4; |
---|
99 | |
---|
100 | if(xb < 0) continue; |
---|
101 | if(xa > xmax) continue; |
---|
102 | if(xa < 0) xa = 0; |
---|
103 | if(xb > xmax) xb = xmax; |
---|
104 | |
---|
105 | for(x = xa; x <= xb; x++) |
---|
106 | caca_putchar(x, y, c); |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|