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 870 2006-04-25 09:59:58Z 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 | #include "common.h" |
---|
20 | |
---|
21 | #if !defined(__KERNEL__) |
---|
22 | # include <stdlib.h> |
---|
23 | #endif |
---|
24 | |
---|
25 | #include "cucul.h" |
---|
26 | #include "cucul_internals.h" |
---|
27 | |
---|
28 | /** \brief Draw a triangle on the canvas using the given character. |
---|
29 | * |
---|
30 | * This function never fails. |
---|
31 | * |
---|
32 | * \param cv The handle to the libcucul canvas. |
---|
33 | * \param x1 X coordinate of the first point. |
---|
34 | * \param y1 Y coordinate of the first point. |
---|
35 | * \param x2 X coordinate of the second point. |
---|
36 | * \param y2 Y coordinate of the second point. |
---|
37 | * \param x3 X coordinate of the third point. |
---|
38 | * \param y3 Y coordinate of the third point. |
---|
39 | * \param str UTF-8 string representing the character that should be used |
---|
40 | * to draw the triangle outline. |
---|
41 | * \return This function always returns 0. |
---|
42 | */ |
---|
43 | int cucul_draw_triangle(cucul_canvas_t *cv, int x1, int y1, int x2, int y2, |
---|
44 | int x3, int y3, char const *str) |
---|
45 | { |
---|
46 | cucul_draw_line(cv, x1, y1, x2, y2, str); |
---|
47 | cucul_draw_line(cv, x2, y2, x3, y3, str); |
---|
48 | cucul_draw_line(cv, x3, y3, x1, y1, str); |
---|
49 | |
---|
50 | return 0; |
---|
51 | } |
---|
52 | |
---|
53 | /** \brief Draw a thin triangle on the canvas. |
---|
54 | * |
---|
55 | * This function never fails. |
---|
56 | * |
---|
57 | * \param cv The handle to the libcucul canvas. |
---|
58 | * \param x1 X coordinate of the first point. |
---|
59 | * \param y1 Y coordinate of the first point. |
---|
60 | * \param x2 X coordinate of the second point. |
---|
61 | * \param y2 Y coordinate of the second point. |
---|
62 | * \param x3 X coordinate of the third point. |
---|
63 | * \param y3 Y coordinate of the third point. |
---|
64 | * \return This function always returns 0. |
---|
65 | */ |
---|
66 | int cucul_draw_thin_triangle(cucul_canvas_t *cv, int x1, int y1, |
---|
67 | int x2, int y2, int x3, int y3) |
---|
68 | { |
---|
69 | cucul_draw_thin_line(cv, x1, y1, x2, y2); |
---|
70 | cucul_draw_thin_line(cv, x2, y2, x3, y3); |
---|
71 | cucul_draw_thin_line(cv, x3, y3, x1, y1); |
---|
72 | |
---|
73 | return 0; |
---|
74 | } |
---|
75 | |
---|
76 | /** \brief Fill a triangle on the canvas using the given character. |
---|
77 | * |
---|
78 | * This function never fails. |
---|
79 | * |
---|
80 | * \param cv The handle to the libcucul canvas. |
---|
81 | * \param x1 X coordinate of the first point. |
---|
82 | * \param y1 Y coordinate of the first point. |
---|
83 | * \param x2 X coordinate of the second point. |
---|
84 | * \param y2 Y coordinate of the second point. |
---|
85 | * \param x3 X coordinate of the third point. |
---|
86 | * \param y3 Y coordinate of the third point. |
---|
87 | * \param str UTF-8 string representing the character that should be used |
---|
88 | * to fill the triangle. |
---|
89 | * \return This function always returns 0. |
---|
90 | */ |
---|
91 | int cucul_fill_triangle(cucul_canvas_t *cv, int x1, int y1, int x2, int y2, |
---|
92 | int x3, int y3, char const *str) |
---|
93 | { |
---|
94 | int x, y, xa, xb, xmax, ymax; |
---|
95 | uint32_t ch; |
---|
96 | |
---|
97 | /* Bubble-sort y1 <= y2 <= y3 */ |
---|
98 | if(y1 > y2) |
---|
99 | return cucul_fill_triangle(cv, x2, y2, x1, y1, x3, y3, str); |
---|
100 | |
---|
101 | if(y2 > y3) |
---|
102 | return cucul_fill_triangle(cv, x1, y1, x3, y3, x2, y2, str); |
---|
103 | |
---|
104 | /* Promote precision */ |
---|
105 | x1 *= 4; |
---|
106 | x2 *= 4; |
---|
107 | x3 *= 4; |
---|
108 | |
---|
109 | xmax = cv->width - 1; |
---|
110 | ymax = cv->height - 1; |
---|
111 | |
---|
112 | ch = _cucul_utf8_to_utf32(str); |
---|
113 | |
---|
114 | /* Rasterize our triangle */ |
---|
115 | for(y = y1 < 0 ? 0 : y1; y <= y3 && y <= ymax; y++) |
---|
116 | { |
---|
117 | if(y <= y2) |
---|
118 | { |
---|
119 | xa = (y1 == y2) ? x2 : x1 + (x2 - x1) * (y - y1) / (y2 - y1); |
---|
120 | xb = (y1 == y3) ? x3 : x1 + (x3 - x1) * (y - y1) / (y3 - y1); |
---|
121 | } |
---|
122 | else |
---|
123 | { |
---|
124 | xa = (y3 == y2) ? x2 : x3 + (x2 - x3) * (y - y3) / (y2 - y3); |
---|
125 | xb = (y3 == y1) ? x1 : x3 + (x1 - x3) * (y - y3) / (y1 - y3); |
---|
126 | } |
---|
127 | |
---|
128 | if(xb < xa) |
---|
129 | { |
---|
130 | int tmp = xb; |
---|
131 | xb = xa; xa = tmp; |
---|
132 | } |
---|
133 | |
---|
134 | /* Rescale xa and xb, slightly cropping */ |
---|
135 | xa = (xa + 2) / 4; |
---|
136 | xb = (xb - 2) / 4; |
---|
137 | |
---|
138 | if(xb < 0) continue; |
---|
139 | if(xa > xmax) continue; |
---|
140 | if(xa < 0) xa = 0; |
---|
141 | if(xb > xmax) xb = xmax; |
---|
142 | |
---|
143 | for(x = xa; x <= xb; x++) |
---|
144 | _cucul_putchar32(cv, x, y, ch); |
---|
145 | } |
---|
146 | |
---|
147 | return 0; |
---|
148 | } |
---|
149 | |
---|