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

Last change on this file since 678 was 678, checked in by Sam Hocevar, 15 years ago
  • Made cucul_putchar32() an internal-only function.
  • Changed the line, box, ellipsis etc. prototypes so that they use an UTF-8 string instead of a single character as their last argument.
  • Property svn:keywords set to Id
File size: 3.9 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 *  This library is free software; you can redistribute it and/or
7 *  modify it under the terms of the Do What The Fuck You Want To
8 *  Public License, Version 2, as published by Sam Hocevar. See
9 *  http://sam.zoy.org/wtfpl/COPYING for more details.
10 */
11
12/** \file triangle.c
13 *  \version \$Id: triangle.c 678 2006-03-23 14:07:32Z sam $
14 *  \author Sam Hocevar <sam@zoy.org>
15 *  \brief Triangle drawing
16 *
17 *  This file contains triangle drawing functions, both filled and outline.
18 */
19
20#include "config.h"
21
22#if !defined(__KERNEL__)
23#   include <stdlib.h>
24#endif
25
26#include "cucul.h"
27#include "cucul_internals.h"
28
29/**
30 * \brief Draw a triangle on the screen using the given character.
31 *
32 * \param x1 X coordinate of the first point.
33 * \param y1 Y coordinate of the first point.
34 * \param x2 X coordinate of the second point.
35 * \param y2 Y coordinate of the second point.
36 * \param x3 X coordinate of the third point.
37 * \param y3 Y coordinate of the third point.
38 * \param c Character to draw the triangle outline with.
39 * \return void
40 */
41void cucul_draw_triangle(cucul_t *qq, int x1, int y1, int x2, int y2,
42                         int x3, int y3, char const *str)
43{
44    cucul_draw_line(qq, x1, y1, x2, y2, str);
45    cucul_draw_line(qq, x2, y2, x3, y3, str);
46    cucul_draw_line(qq, x3, y3, x1, y1, str);
47}
48
49/**
50 * \brief Draw a thin triangle on the screen.
51 *
52 * \param x1 X coordinate of the first point.
53 * \param y1 Y coordinate of the first point.
54 * \param x2 X coordinate of the second point.
55 * \param y2 Y coordinate of the second point.
56 * \param x3 X coordinate of the third point.
57 * \param y3 Y coordinate of the third point.
58 * \return void
59 */
60void cucul_draw_thin_triangle(cucul_t *qq, int x1, int y1, int x2, int y2,
61                              int x3, int y3)
62{
63    cucul_draw_thin_line(qq, x1, y1, x2, y2);
64    cucul_draw_thin_line(qq, x2, y2, x3, y3);
65    cucul_draw_thin_line(qq, x3, y3, x1, y1);
66}
67
68/**
69 * \brief Fill a triangle on the screen using the given character.
70 *
71 * \param x1 X coordinate of the first point.
72 * \param y1 Y coordinate of the first point.
73 * \param x2 X coordinate of the second point.
74 * \param y2 Y coordinate of the second point.
75 * \param x3 X coordinate of the third point.
76 * \param y3 Y coordinate of the third point.
77 * \param c Character to fill the triangle with.
78 * \return void
79 */
80void cucul_fill_triangle(cucul_t *qq, 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 c;
85
86    /* Bubble-sort y1 <= y2 <= y3 */
87    if(y1 > y2)
88    {
89        cucul_fill_triangle(qq, x2, y2, x1, y1, x3, y3, str);
90        return;
91    }
92
93    if(y2 > y3)
94    {
95        cucul_fill_triangle(qq, 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 = qq->width - 1;
105    ymax = qq->height - 1;
106
107    c = _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(qq, x, y, c);
140    }
141}
142
Note: See TracBrowser for help on using the repository browser.