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

Last change on this file since 540 was 540, checked in by Sam Hocevar, 14 years ago
  • Polished the driver split a bit (still no events, except resize events), properly credited authors and documented a few things.
  • Property svn:keywords set to Id
File size: 3.6 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 540 2006-03-07 09:17:35Z 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#include <stdlib.h>
23
24#include "cucul.h"
25#include "cucul_internals.h"
26
27/**
28 * \brief Draw a triangle on the screen using the given character.
29 *
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 c Character to draw the triangle outline with.
37 * \return void
38 */
39void cucul_draw_triangle(cucul_t *qq, int x1, int y1, int x2, int y2, int x3, int y3, char c)
40{
41    cucul_draw_line(qq, x1, y1, x2, y2, c);
42    cucul_draw_line(qq, x2, y2, x3, y3, c);
43    cucul_draw_line(qq, x3, y3, x1, y1, c);
44}
45
46/**
47 * \brief Draw a thin triangle on the screen.
48 *
49 * \param x1 X coordinate of the first point.
50 * \param y1 Y coordinate of the first point.
51 * \param x2 X coordinate of the second point.
52 * \param y2 Y coordinate of the second point.
53 * \param x3 X coordinate of the third point.
54 * \param y3 Y coordinate of the third point.
55 * \return void
56 */
57void cucul_draw_thin_triangle(cucul_t *qq, int x1, int y1, int x2, int y2, int x3, int y3)
58{
59    cucul_draw_thin_line(qq, x1, y1, x2, y2);
60    cucul_draw_thin_line(qq, x2, y2, x3, y3);
61    cucul_draw_thin_line(qq, x3, y3, x1, y1);
62}
63
64/**
65 * \brief Fill a triangle on the screen using the given character.
66 *
67 * \param x1 X coordinate of the first point.
68 * \param y1 Y coordinate of the first point.
69 * \param x2 X coordinate of the second point.
70 * \param y2 Y coordinate of the second point.
71 * \param x3 X coordinate of the third point.
72 * \param y3 Y coordinate of the third point.
73 * \param c Character to fill the triangle with.
74 * \return void
75 */
76void cucul_fill_triangle(cucul_t *qq, int x1, int y1, int x2, int y2, int x3, int y3, char c)
77{
78    int x, y, xa, xb, xmax, ymax;
79
80    /* Bubble-sort y1 <= y2 <= y3 */
81    if(y1 > y2)
82    {
83        cucul_fill_triangle(qq, x2, y2, x1, y1, x3, y3, c);
84        return;
85    }
86
87    if(y2 > y3)
88    {
89        cucul_fill_triangle(qq, x1, y1, x3, y3, x2, y2, c);
90        return;
91    }
92
93    /* Promote precision */
94    x1 *= 4;
95    x2 *= 4;
96    x3 *= 4;
97
98    xmax = qq->width - 1;
99    ymax = qq->height - 1;
100
101    /* Rasterize our triangle */
102    for(y = y1 < 0 ? 0 : y1; y <= y3 && y <= ymax; y++)
103    {
104        if(y <= y2)
105        {
106            xa = (y1 == y2) ? x2 : x1 + (x2 - x1) * (y - y1) / (y2 - y1);
107            xb = (y1 == y3) ? x3 : x1 + (x3 - x1) * (y - y1) / (y3 - y1);
108        }
109        else
110        {
111            xa = (y3 == y2) ? x2 : x3 + (x2 - x3) * (y - y3) / (y2 - y3);
112            xb = (y3 == y1) ? x1 : x3 + (x1 - x3) * (y - y3) / (y1 - y3);
113        }
114
115        if(xb < xa)
116        {
117            int tmp = xb;
118            xb = xa; xa = tmp;
119        }
120
121        /* Rescale xa and xb, slightly cropping */
122        xa = (xa + 2) / 4;
123        xb = (xb - 2) / 4;
124
125        if(xb < 0) continue;
126        if(xa > xmax) continue;
127        if(xa < 0) xa = 0;
128        if(xb > xmax) xb = xmax;
129
130        for(x = xa; x <= xb; x++)
131            cucul_putchar(qq, x, y, c);
132    }
133}
134
Note: See TracBrowser for help on using the repository browser.