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

Last change on this file since 1462 was 1462, checked in by Sam Hocevar, 13 years ago
  • Bwarf, typo in the no warranty clause.
  • Property svn:keywords set to Id
File size: 4.6 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 1462 2006-12-12 01:53:54Z sam $
7 *
8 *  This library is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15/*
16 *  This file contains triangle drawing functions, both filled and outline.
17 */
18
19#include "config.h"
20#include "common.h"
21
22#if !defined(__KERNEL__)
23#   include <stdlib.h>
24#endif
25
26#include "cucul.h"
27#include "cucul_internals.h"
28
29/** \brief Draw a triangle on the canvas using the given character.
30 *
31 *  This function never fails.
32 *
33 *  \param cv The handle to the libcucul canvas.
34 *  \param x1 X coordinate of the first point.
35 *  \param y1 Y coordinate of the first point.
36 *  \param x2 X coordinate of the second point.
37 *  \param y2 Y coordinate of the second point.
38 *  \param x3 X coordinate of the third point.
39 *  \param y3 Y coordinate of the third point.
40 *  \param ch UTF-32 character to be used 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, unsigned long int ch)
45{
46    cucul_draw_line(cv, x1, y1, x2, y2, ch);
47    cucul_draw_line(cv, x2, y2, x3, y3, ch);
48    cucul_draw_line(cv, x3, y3, x1, y1, ch);
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 ch UTF-32 character to be used to fill the triangle.
88 *  \return This function always returns 0.
89 */
90int cucul_fill_triangle(cucul_canvas_t *cv, int x1, int y1, int x2, int y2,
91                        int x3, int y3, unsigned long int ch)
92{
93    int x, y, xmin, xmax, ymin, ymax;
94    long int xx1, xx2, xa, xb, sl21, sl31, sl32;
95
96    /* Bubble-sort y1 <= y2 <= y3 */
97    if(y1 > y2)
98        return cucul_fill_triangle(cv, x2, y2, x1, y1, x3, y3, ch);
99
100    if(y2 > y3)
101        return cucul_fill_triangle(cv, x1, y1, x3, y3, x2, y2, ch);
102
103    /* Compute slopes and promote precision */
104    sl21 = (y2 == y1) ? 0 : (x2 - x1) * 0x10000 / (y2 - y1);
105    sl31 = (y3 == y1) ? 0 : (x3 - x1) * 0x10000 / (y3 - y1);
106    sl32 = (y3 == y2) ? 0 : (x3 - x2) * 0x10000 / (y3 - y2);
107
108    x1 *= 0x10000;
109    x2 *= 0x10000;
110    x3 *= 0x10000;
111
112    ymin = y1 < 0 ? 0 : y1;
113    ymax = y3 + 1 < (int)cv->height ? y3 + 1 : (int)cv->height;
114
115    if(ymin < y2)
116    {
117        xa = x1 + sl21 * (ymin - y1);
118        xb = x1 + sl31 * (ymin - y1);
119    }
120    else if(ymin == y2)
121    {
122        xa = x2;
123        xb = (y1 == y3) ? x3 : x1 + sl31 * (ymin - y1);
124    }
125    else /* (ymin > y2) */
126    {
127        xa = x3 + sl32 * (ymin - y3);
128        xb = x3 + sl31 * (ymin - y3);
129    }
130
131    /* Rasterize our triangle */
132    for(y = ymin; y < ymax; y++)
133    {
134        /* Rescale xa and xb, recentering the division */
135        if(xa < xb)
136        {
137            xx1 = (xa + 0x800) / 0x10000;
138            xx2 = (xb + 0x801) / 0x10000;
139        }
140        else
141        {
142            xx1 = (xb + 0x800) / 0x10000;
143            xx2 = (xa + 0x801) / 0x10000;
144        }
145
146        xmin = xx1 < 0 ? 0 : xx1;
147        xmax = xx2 + 1 < (int)cv->width ? xx2 + 1 : (int)cv->width;
148
149        for(x = xmin; x < xmax; x++)
150            cucul_put_char(cv, x, y, ch);
151
152        xa += y < y2 ? sl21 : sl32;
153        xb += sl31;
154    }
155
156    return 0;
157}
158
Note: See TracBrowser for help on using the repository browser.