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

Last change on this file since 568 was 568, checked in by Sam Hocevar, 15 years ago
  • Added glue code to compile libcaca without a libc and build applications as multiboot kernels.
  • Property svn:keywords set to Id
File size: 3.7 KB
Line 
1/*
2 *  libcucul      Unicode canvas library
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 568 2006-03-09 12:47:37Z 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, int x3, int y3, char c)
42{
43    cucul_draw_line(qq, x1, y1, x2, y2, c);
44    cucul_draw_line(qq, x2, y2, x3, y3, c);
45    cucul_draw_line(qq, x3, y3, x1, y1, c);
46}
47
48/**
49 * \brief Draw a thin triangle on the screen.
50 *
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 */
59void cucul_draw_thin_triangle(cucul_t *qq, int x1, int y1, int x2, int y2, int x3, int y3)
60{
61    cucul_draw_thin_line(qq, x1, y1, x2, y2);
62    cucul_draw_thin_line(qq, x2, y2, x3, y3);
63    cucul_draw_thin_line(qq, x3, y3, x1, y1);
64}
65
66/**
67 * \brief Fill a triangle on the screen using the given character.
68 *
69 * \param x1 X coordinate of the first point.
70 * \param y1 Y coordinate of the first point.
71 * \param x2 X coordinate of the second point.
72 * \param y2 Y coordinate of the second point.
73 * \param x3 X coordinate of the third point.
74 * \param y3 Y coordinate of the third point.
75 * \param c Character to fill the triangle with.
76 * \return void
77 */
78void cucul_fill_triangle(cucul_t *qq, int x1, int y1, int x2, int y2, int x3, int y3, char c)
79{
80    int x, y, xa, xb, xmax, ymax;
81
82    /* Bubble-sort y1 <= y2 <= y3 */
83    if(y1 > y2)
84    {
85        cucul_fill_triangle(qq, x2, y2, x1, y1, x3, y3, c);
86        return;
87    }
88
89    if(y2 > y3)
90    {
91        cucul_fill_triangle(qq, x1, y1, x3, y3, x2, y2, c);
92        return;
93    }
94
95    /* Promote precision */
96    x1 *= 4;
97    x2 *= 4;
98    x3 *= 4;
99
100    xmax = qq->width - 1;
101    ymax = qq->height - 1;
102
103    /* Rasterize our triangle */
104    for(y = y1 < 0 ? 0 : y1; y <= y3 && y <= ymax; y++)
105    {
106        if(y <= y2)
107        {
108            xa = (y1 == y2) ? x2 : x1 + (x2 - x1) * (y - y1) / (y2 - y1);
109            xb = (y1 == y3) ? x3 : x1 + (x3 - x1) * (y - y1) / (y3 - y1);
110        }
111        else
112        {
113            xa = (y3 == y2) ? x2 : x3 + (x2 - x3) * (y - y3) / (y2 - y3);
114            xb = (y3 == y1) ? x1 : x3 + (x1 - x3) * (y - y3) / (y1 - y3);
115        }
116
117        if(xb < xa)
118        {
119            int tmp = xb;
120            xb = xa; xa = tmp;
121        }
122
123        /* Rescale xa and xb, slightly cropping */
124        xa = (xa + 2) / 4;
125        xb = (xb - 2) / 4;
126
127        if(xb < 0) continue;
128        if(xa > xmax) continue;
129        if(xa < 0) xa = 0;
130        if(xb > xmax) xb = xmax;
131
132        for(x = xa; x <= xb; x++)
133            cucul_putchar(qq, x, y, c);
134    }
135}
136
Note: See TracBrowser for help on using the repository browser.