source: libcaca/trunk/cucul/triangle.c @ 1172

Last change on this file since 1172 was 1172, checked in by Sam Hocevar, 16 years ago
  • Optimised triangle rasteriser. Thanks to Jylam for the hints.
  • Property svn:keywords set to Id
File size: 4.4 KB
Line 
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 1172 2006-10-01 10:47:41Z 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 */
43int 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 */
66int 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 */
91int 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, xmax, ymax;
95    long int xa, xb, sl21, sl31, sl32;
96    uint32_t ch;
97
98    /* Bubble-sort y1 <= y2 <= y3 */
99    if(y1 > y2)
100        return cucul_fill_triangle(cv, x2, y2, x1, y1, x3, y3, str);
101
102    if(y2 > y3)
103        return cucul_fill_triangle(cv, x1, y1, x3, y3, x2, y2, str);
104
105    ch = cucul_utf8_to_utf32(str, NULL);
106
107    /* Compute slopes and promote precision */
108    sl21 = (y2 == y1) ? 0 : (x2 - x1) * 0x1000 / (y2 - y1);
109    sl31 = (y3 == y1) ? 0 : (x3 - x1) * 0x1000 / (y3 - y1);
110    sl32 = (y3 == y2) ? 0 : (x3 - x2) * 0x1000 / (y3 - y2);
111
112    x1 *= 0x1000;
113    x2 *= 0x1000;
114    x3 *= 0x1000;
115
116    xmax = cv->width - 1;
117    ymax = cv->height - 1;
118
119    /* Rasterize our triangle */
120    for(y = y1 < 0 ? 0 : y1; y <= y3 && y <= ymax; y++)
121    {
122        if(y < y2)
123        {
124            xa = x1 + sl21 * (y - y1);
125            xb = x1 + sl31 * (y - y1);
126        }
127        else if(y == y2)
128        {
129            xa = x2;
130            xb = (y1 == y3) ? x3 : x1 + sl31 * (y - y1);
131        }
132        else /* (y > y2) */
133        {
134            xa = x3 + sl32 * (y - y3);
135            xb = x3 + sl31 * (y - y3);
136        }
137
138        if(xb < xa)
139        {
140            long int tmp = xb; xb = xa; xa = tmp;
141        }
142
143        /* Rescale xa and xb, recentering the division */
144        xa = (xa + 0x800) / 0x1000;
145        xb = (xb + 0x801) / 0x1000;
146
147        for(x = xa < 0 ? 0 : xa; x <= xb && x <= xmax; x++)
148            cucul_putchar(cv, x, y, ch);
149    }
150
151    return 0;
152}
153
Note: See TracBrowser for help on using the repository browser.