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