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 160 2003-11-12 21:18:50Z 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 | #include <stdlib.h> |
---|
26 | |
---|
27 | #include "ee.h" |
---|
28 | #include "ee_internals.h" |
---|
29 | |
---|
30 | void ee_draw_triangle(int x1, int y1, int x2, int y2, int x3, int y3, char c) |
---|
31 | { |
---|
32 | ee_draw_line(x1, y1, x2, y2, c); |
---|
33 | ee_draw_line(x2, y2, x3, y3, c); |
---|
34 | ee_draw_line(x3, y3, x1, y1, c); |
---|
35 | } |
---|
36 | |
---|
37 | void ee_draw_thin_triangle(int x1, int y1, int x2, int y2, int x3, int y3) |
---|
38 | { |
---|
39 | ee_draw_thin_line(x1, y1, x2, y2); |
---|
40 | ee_draw_thin_line(x2, y2, x3, y3); |
---|
41 | ee_draw_thin_line(x3, y3, x1, y1); |
---|
42 | } |
---|
43 | |
---|
44 | void ee_fill_triangle(int x1, int y1, int x2, int y2, int x3, int y3, char c) |
---|
45 | { |
---|
46 | int x, y, xa, xb, xmax, ymax; |
---|
47 | |
---|
48 | /* Bubble-sort y1 <= y2 <= y3 */ |
---|
49 | if(y1 > y2) |
---|
50 | { |
---|
51 | ee_fill_triangle(x2, y2, x1, y1, x3, y3, c); |
---|
52 | return; |
---|
53 | } |
---|
54 | |
---|
55 | if(y2 > y3) |
---|
56 | { |
---|
57 | ee_fill_triangle(x1, y1, x3, y3, x2, y2, c); |
---|
58 | return; |
---|
59 | } |
---|
60 | |
---|
61 | /* Promote precision */ |
---|
62 | x1 *= 4; |
---|
63 | x2 *= 4; |
---|
64 | x3 *= 4; |
---|
65 | |
---|
66 | xmax = ee_get_width() - 1; |
---|
67 | ymax = ee_get_height() - 1; |
---|
68 | |
---|
69 | /* Rasterize our triangle */ |
---|
70 | for(y = y1 < 0 ? 0 : y1; y <= y3 && y <= ymax; y++) |
---|
71 | { |
---|
72 | if(y <= y2) |
---|
73 | { |
---|
74 | xa = (y1 == y2) ? x2 : x1 + (x2 - x1) * (y - y1) / (y2 - y1); |
---|
75 | xb = (y1 == y3) ? x3 : x1 + (x3 - x1) * (y - y1) / (y3 - y1); |
---|
76 | } |
---|
77 | else |
---|
78 | { |
---|
79 | xa = (y3 == y2) ? x2 : x3 + (x2 - x3) * (y - y3) / (y2 - y3); |
---|
80 | xb = (y3 == y1) ? x1 : x3 + (x1 - x3) * (y - y3) / (y1 - y3); |
---|
81 | } |
---|
82 | |
---|
83 | if(xb < xa) |
---|
84 | { |
---|
85 | int tmp = xb; |
---|
86 | xb = xa; xa = tmp; |
---|
87 | } |
---|
88 | |
---|
89 | /* Rescale xa and xb, slightly cropping */ |
---|
90 | xa = (xa + 2) / 4; |
---|
91 | xb = (xb - 2) / 4; |
---|
92 | |
---|
93 | if(xb < 0) continue; |
---|
94 | if(xa > xmax) continue; |
---|
95 | if(xa < 0) xa = 0; |
---|
96 | if(xb > xmax) xb = xmax; |
---|
97 | |
---|
98 | for(x = xa; x <= xb; x++) |
---|
99 | ee_putchar(x, y, c); |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|