1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
|
/*
* libcaca Colour ASCII-Art library
* Copyright (c) 2002-2010 Sam Hocevar <sam@hocevar.net>
* All Rights Reserved
*
* This library is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*/
/*
* This file contains line and polyline drawing functions, with both thin
* and thick styles.
*/
#include "config.h"
#if !defined(__KERNEL__)
# include <stdlib.h>
#endif
#include "caca.h"
#include "caca_internals.h"
#if !defined(_DOXYGEN_SKIP_ME)
struct line
{
int x1, y1;
int x2, y2;
uint32_t ch;
void (*draw) (caca_canvas_t *, struct line*);
};
#endif
static void clip_line(caca_canvas_t*, struct line*);
static uint8_t clip_bits(caca_canvas_t*, int, int);
static void draw_solid_line(caca_canvas_t*, struct line*);
static void draw_thin_line(caca_canvas_t*, struct line*);
/** \brief Draw a line on the canvas using the given character.
*
* This function never fails.
*
* \param cv The handle to the libcaca canvas.
* \param x1 X coordinate of the first point.
* \param y1 Y coordinate of the first point.
* \param x2 X coordinate of the second point.
* \param y2 Y coordinate of the second point.
* \param ch UTF-32 character to be used to draw the line.
* \return This function always returns 0.
*/
int caca_draw_line(caca_canvas_t *cv, int x1, int y1, int x2, int y2,
uint32_t ch)
{
struct line s;
s.x1 = x1;
s.y1 = y1;
s.x2 = x2;
s.y2 = y2;
s.ch = ch;
s.draw = draw_solid_line;
clip_line(cv, &s);
return 0;
}
/** \brief Draw a polyline.
*
* Draw a polyline on the canvas using the given character and coordinate
* arrays. The first and last points are not connected, hence in order to
* draw a polygon you need to specify the starting point at the end of the
* list as well.
*
* This function never fails.
*
* \param cv The handle to the libcaca canvas.
* \param x Array of X coordinates. Must have \p n + 1 elements.
* \param y Array of Y coordinates. Must have \p n + 1 elements.
* \param n Number of lines to draw.
* \param ch UTF-32 character to be used to draw the lines.
* \return This function always returns 0.
*/
int caca_draw_polyline(caca_canvas_t *cv, int const x[], int const y[],
int n, uint32_t ch)
{
int i;
struct line s;
s.ch = ch;
s.draw = draw_solid_line;
for(i = 0; i < n; i++)
{
s.x1 = x[i];
s.y1 = y[i];
s.x2 = x[i+1];
s.y2 = y[i+1];
clip_line(cv, &s);
}
return 0;
}
/** \brief Draw a thin line on the canvas, using ASCII art.
*
* This function never fails.
*
* \param cv The handle to the libcaca canvas.
* \param x1 X coordinate of the first point.
* \param y1 Y coordinate of the first point.
* \param x2 X coordinate of the second point.
* \param y2 Y coordinate of the second point.
* \return This function always returns 0.
*/
int caca_draw_thin_line(caca_canvas_t *cv, int x1, int y1, int x2, int y2)
{
struct line s;
s.x1 = x1;
s.y1 = y1;
s.x2 = x2;
s.y2 = y2;
s.draw = draw_thin_line;
clip_line(cv, &s);
return 0;
}
/** \brief Draw an ASCII art thin polyline.
*
* Draw a thin polyline on the canvas using the given coordinate arrays and
* with ASCII art. The first and last points are not connected, so in order
* to draw a polygon you need to specify the starting point at the end of
* the list as well.
*
* This function never fails.
*
* \param cv The handle to the libcaca canvas.
* \param x Array of X coordinates. Must have \p n + 1 elements.
* \param y Array of Y coordinates. Must have \p n + 1 elements.
* \param n Number of lines to draw.
* \return This function always returns 0.
*/
int caca_draw_thin_polyline(caca_canvas_t *cv, int const x[], int const y[],
int n)
{
int i;
struct line s;
s.draw = draw_thin_line;
for(i = 0; i < n; i++)
{
s.x1 = x[i];
s.y1 = y[i];
s.x2 = x[i+1];
s.y2 = y[i+1];
clip_line(cv, &s);
}
return 0;
}
/*
* XXX: The following functions are local.
*/
/* Generic Cohen-Sutherland line clipping function. */
static void clip_line(caca_canvas_t *cv, struct line* s)
{
uint8_t bits1, bits2;
bits1 = clip_bits(cv, s->x1, s->y1);
bits2 = clip_bits(cv, s->x2, s->y2);
if(bits1 & bits2)
return;
if(bits1 == 0)
{
if(bits2 == 0)
s->draw(cv, s);
else
{
int tmp;
tmp = s->x1; s->x1 = s->x2; s->x2 = tmp;
tmp = s->y1; s->y1 = s->y2; s->y2 = tmp;
clip_line(cv, s);
}
return;
}
if(bits1 & (1<<0))
{
s->y1 = s->y2 - (s->x2 - 0) * (s->y2 - s->y1) / (s->x2 - s->x1);
s->x1 = 0;
}
else if(bits1 & (1<<1))
{
int xmax = cv->width - 1;
s->y1 = s->y2 - (s->x2 - xmax) * (s->y2 - s->y1) / (s->x2 - s->x1);
s->x1 = xmax;
}
else if(bits1 & (1<<2))
{
s->x1 = s->x2 - (s->y2 - 0) * (s->x2 - s->x1) / (s->y2 - s->y1);
s->y1 = 0;
}
else if(bits1 & (1<<3))
{
int ymax = cv->height - 1;
s->x1 = s->x2 - (s->y2 - ymax) * (s->x2 - s->x1) / (s->y2 - s->y1);
s->y1 = ymax;
}
clip_line(cv, s);
}
/* Helper function for clip_line(). */
static uint8_t clip_bits(caca_canvas_t *cv, int x, int y)
{
uint8_t b = 0;
if(x < 0)
b |= (1<<0);
else if(x >= (int)cv->width)
b |= (1<<1);
if(y < 0)
b |= (1<<2);
else if(y >= (int)cv->height)
b |= (1<<3);
return b;
}
/* Solid line drawing function, using Bresenham's mid-point line
* scan-conversion algorithm. */
static void draw_solid_line(caca_canvas_t *cv, struct line* s)
{
int x1, y1, x2, y2;
int dx, dy;
int xinc, yinc;
x1 = s->x1; y1 = s->y1; x2 = s->x2; y2 = s->y2;
dx = abs(x2 - x1);
dy = abs(y2 - y1);
xinc = (x1 > x2) ? -1 : 1;
yinc = (y1 > y2) ? -1 : 1;
if(dx >= dy)
{
int dpr = dy << 1;
int dpru = dpr - (dx << 1);
int delta = dpr - dx;
for(; dx>=0; dx--)
{
caca_put_char(cv, x1, y1, s->ch);
if(delta > 0)
{
x1 += xinc;
y1 += yinc;
delta += dpru;
}
else
{
x1 += xinc;
delta += dpr;
}
}
}
else
{
int dpr = dx << 1;
int dpru = dpr - (dy << 1);
int delta = dpr - dy;
for(; dy >= 0; dy--)
{
caca_put_char(cv, x1, y1, s->ch);
if(delta > 0)
{
x1 += xinc;
y1 += yinc;
delta += dpru;
}
else
{
y1 += yinc;
delta += dpr;
}
}
}
}
/* Thin line drawing function, using Bresenham's mid-point line
* scan-conversion algorithm and ASCII art graphics. */
static void draw_thin_line(caca_canvas_t *cv, struct line* s)
{
uint32_t charmapx[2], charmapy[2];
int x1, y1, x2, y2;
int dx, dy;
int yinc;
if(s->x2 >= s->x1)
{
charmapx[0] = (s->y1 > s->y2) ? ',' : '`';
charmapx[1] = (s->y1 > s->y2) ? '\'' : '.';
x1 = s->x1; y1 = s->y1; x2 = s->x2; y2 = s->y2;
}
else
{
charmapx[0] = (s->y1 > s->y2) ? '`' : '.';
charmapx[1] = (s->y1 > s->y2) ? ',' : '\'';
x2 = s->x1; y2 = s->y1; x1 = s->x2; y1 = s->y2;
}
dx = abs(x2 - x1);
dy = abs(y2 - y1);
if(y1 > y2)
{
charmapy[0] = ',';
charmapy[1] = '\'';
yinc = -1;
}
else
{
yinc = 1;
charmapy[0] = '`';
charmapy[1] = '.';
}
if(dx >= dy)
{
int dpr = dy << 1;
int dpru = dpr - (dx << 1);
int delta = dpr - dx;
int prev = 0;
for(; dx>=0; dx--)
{
if(delta > 0)
{
caca_put_char(cv, x1, y1, charmapy[1]);
x1++;
y1 += yinc;
delta += dpru;
prev = 1;
}
else
{
if(prev)
caca_put_char(cv, x1, y1, charmapy[0]);
else
caca_put_char(cv, x1, y1, '-');
x1++;
delta += dpr;
prev = 0;
}
}
}
else
{
int dpr = dx << 1;
int dpru = dpr - (dy << 1);
int delta = dpr - dy;
for(; dy >= 0; dy--)
{
if(delta > 0)
{
caca_put_char(cv, x1, y1, charmapx[0]);
caca_put_char(cv, x1 + 1, y1, charmapx[1]);
x1++;
y1 += yinc;
delta += dpru;
}
else
{
caca_put_char(cv, x1, y1, '|');
y1 += yinc;
delta += dpr;
}
}
}
}
/*
* XXX: The following functions are aliases.
*/
int cucul_draw_line(cucul_canvas_t *, int, int, int, int, uint32_t)
CACA_ALIAS(caca_draw_line);
int cucul_draw_polyline(cucul_canvas_t *, int const x[],
int const y[], int, uint32_t)
CACA_ALIAS(caca_draw_polyline);
int cucul_draw_thin_line(cucul_canvas_t *, int, int, int, int)
CACA_ALIAS(caca_draw_thin_line);
int cucul_draw_thin_polyline(cucul_canvas_t *, int const x[],
int const y[], int)
CACA_ALIAS(caca_draw_thin_polyline);
|