1 | /* |
---|
2 | * libcucul Canvas for ultrafast compositing of Unicode letters |
---|
3 | * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: triangle.c 769 2006-04-14 07:30:53Z sam $ |
---|
7 | * |
---|
8 | * This library is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the Do What The Fuck You Want To |
---|
10 | * Public License, Version 2, as published by Sam Hocevar. See |
---|
11 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
12 | */ |
---|
13 | |
---|
14 | /* |
---|
15 | * This file contains triangle drawing functions, both filled and outline. |
---|
16 | */ |
---|
17 | |
---|
18 | #include "config.h" |
---|
19 | |
---|
20 | #if !defined(__KERNEL__) |
---|
21 | # include <stdlib.h> |
---|
22 | #endif |
---|
23 | |
---|
24 | #include "cucul.h" |
---|
25 | #include "cucul_internals.h" |
---|
26 | |
---|
27 | /** |
---|
28 | * \brief Draw a triangle on the screen using the given character. |
---|
29 | * |
---|
30 | * \param x1 X coordinate of the first point. |
---|
31 | * \param y1 Y coordinate of the first point. |
---|
32 | * \param x2 X coordinate of the second point. |
---|
33 | * \param y2 Y coordinate of the second point. |
---|
34 | * \param x3 X coordinate of the third point. |
---|
35 | * \param y3 Y coordinate of the third point. |
---|
36 | * \param c Character to draw the triangle outline with. |
---|
37 | * \return void |
---|
38 | */ |
---|
39 | void cucul_draw_triangle(cucul_t *qq, int x1, int y1, int x2, int y2, |
---|
40 | int x3, int y3, char const *str) |
---|
41 | { |
---|
42 | cucul_draw_line(qq, x1, y1, x2, y2, str); |
---|
43 | cucul_draw_line(qq, x2, y2, x3, y3, str); |
---|
44 | cucul_draw_line(qq, x3, y3, x1, y1, str); |
---|
45 | } |
---|
46 | |
---|
47 | /** |
---|
48 | * \brief Draw a thin triangle on the screen. |
---|
49 | * |
---|
50 | * \param x1 X coordinate of the first point. |
---|
51 | * \param y1 Y coordinate of the first point. |
---|
52 | * \param x2 X coordinate of the second point. |
---|
53 | * \param y2 Y coordinate of the second point. |
---|
54 | * \param x3 X coordinate of the third point. |
---|
55 | * \param y3 Y coordinate of the third point. |
---|
56 | * \return void |
---|
57 | */ |
---|
58 | void cucul_draw_thin_triangle(cucul_t *qq, int x1, int y1, int x2, int y2, |
---|
59 | int x3, int y3) |
---|
60 | { |
---|
61 | cucul_draw_thin_line(qq, x1, y1, x2, y2); |
---|
62 | cucul_draw_thin_line(qq, x2, y2, x3, y3); |
---|
63 | cucul_draw_thin_line(qq, x3, y3, x1, y1); |
---|
64 | } |
---|
65 | |
---|
66 | /** |
---|
67 | * \brief Fill a triangle on the screen using the given character. |
---|
68 | * |
---|
69 | * \param x1 X coordinate of the first point. |
---|
70 | * \param y1 Y coordinate of the first point. |
---|
71 | * \param x2 X coordinate of the second point. |
---|
72 | * \param y2 Y coordinate of the second point. |
---|
73 | * \param x3 X coordinate of the third point. |
---|
74 | * \param y3 Y coordinate of the third point. |
---|
75 | * \param c Character to fill the triangle with. |
---|
76 | * \return void |
---|
77 | */ |
---|
78 | void cucul_fill_triangle(cucul_t *qq, int x1, int y1, int x2, int y2, |
---|
79 | int x3, int y3, char const *str) |
---|
80 | { |
---|
81 | int x, y, xa, xb, xmax, ymax; |
---|
82 | uint32_t c; |
---|
83 | |
---|
84 | /* Bubble-sort y1 <= y2 <= y3 */ |
---|
85 | if(y1 > y2) |
---|
86 | { |
---|
87 | cucul_fill_triangle(qq, x2, y2, x1, y1, x3, y3, str); |
---|
88 | return; |
---|
89 | } |
---|
90 | |
---|
91 | if(y2 > y3) |
---|
92 | { |
---|
93 | cucul_fill_triangle(qq, x1, y1, x3, y3, x2, y2, str); |
---|
94 | return; |
---|
95 | } |
---|
96 | |
---|
97 | /* Promote precision */ |
---|
98 | x1 *= 4; |
---|
99 | x2 *= 4; |
---|
100 | x3 *= 4; |
---|
101 | |
---|
102 | xmax = qq->width - 1; |
---|
103 | ymax = qq->height - 1; |
---|
104 | |
---|
105 | c = _cucul_utf8_to_utf32(str); |
---|
106 | |
---|
107 | /* Rasterize our triangle */ |
---|
108 | for(y = y1 < 0 ? 0 : y1; y <= y3 && y <= ymax; y++) |
---|
109 | { |
---|
110 | if(y <= y2) |
---|
111 | { |
---|
112 | xa = (y1 == y2) ? x2 : x1 + (x2 - x1) * (y - y1) / (y2 - y1); |
---|
113 | xb = (y1 == y3) ? x3 : x1 + (x3 - x1) * (y - y1) / (y3 - y1); |
---|
114 | } |
---|
115 | else |
---|
116 | { |
---|
117 | xa = (y3 == y2) ? x2 : x3 + (x2 - x3) * (y - y3) / (y2 - y3); |
---|
118 | xb = (y3 == y1) ? x1 : x3 + (x1 - x3) * (y - y3) / (y1 - y3); |
---|
119 | } |
---|
120 | |
---|
121 | if(xb < xa) |
---|
122 | { |
---|
123 | int tmp = xb; |
---|
124 | xb = xa; xa = tmp; |
---|
125 | } |
---|
126 | |
---|
127 | /* Rescale xa and xb, slightly cropping */ |
---|
128 | xa = (xa + 2) / 4; |
---|
129 | xb = (xb - 2) / 4; |
---|
130 | |
---|
131 | if(xb < 0) continue; |
---|
132 | if(xa > xmax) continue; |
---|
133 | if(xa < 0) xa = 0; |
---|
134 | if(xb > xmax) xb = xmax; |
---|
135 | |
---|
136 | for(x = xa; x <= xb; x++) |
---|
137 | _cucul_putchar32(qq, x, y, c); |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|