| 1 |
/* Libvisual-plugins - Standard plugins for libvisual |
| 2 |
* |
| 3 |
* Copyright (C) 2002, 2003, 2004, 2005, 2006 Dennis Smit <[email protected]> |
| 4 |
* |
| 5 |
* Authors: Dennis Smit <[email protected]> |
| 6 |
* |
| 7 |
* $Id: misc.c,v 1.5 2006/01/22 13:25:26 synap Exp $ |
| 8 |
* |
| 9 |
* This program is free software; you can redistribute it and/or modify |
| 10 |
* it under the terms of the GNU General Public License as |
| 11 |
* published by the Free Software Foundation; either version 2 |
| 12 |
* of the License, or (at your option) any later version. |
| 13 |
* |
| 14 |
* This program is distributed in the hope that it will be useful, |
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
* GNU General Public License for more details. |
| 18 |
* |
| 19 |
* You should have received a copy of the GNU General Public License |
| 20 |
* along with this program; if not, write to the Free Software |
| 21 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 |
*/ |
| 23 |
|
| 24 |
#include <math.h> |
| 25 |
#include <time.h> |
| 26 |
#include <sys/stat.h> |
| 27 |
#include <sys/types.h> |
| 28 |
#include <sys/time.h> |
| 29 |
#include <fcntl.h> |
| 30 |
|
| 31 |
#include <common.h> |
| 32 |
#include <table.h> |
| 33 |
#include <misc.h> |
| 34 |
|
| 35 |
/* FIXME when using tables check radian sanity (!>= 2 PI | % TAB SIZE DUS ?); */ |
| 36 |
int _oink_line_x_next_rad (int x, int length, float rad) |
| 37 |
{ |
| 38 |
/* return (_oink_table_sinlarge[(int) (rad * (OINK_TABLE_LARGE_SIZE / (PI * 2)))] * length) + x; */ |
| 39 |
return (sin (rad) * length) + x; |
| 40 |
} |
| 41 |
|
| 42 |
int _oink_line_y_next_rad (int y, int length, float rad) |
| 43 |
{ |
| 44 |
/* return (_oink_table_coslarge[(int) (rad * (OINK_TABLE_LARGE_SIZE / (PI * 2)))] * length) + y; */ |
| 45 |
return (cos (rad + PI) * length) + y; |
| 46 |
} |
| 47 |
|
| 48 |
int _oink_line_length (int x0, int y0, int x1, int y1) |
| 49 |
{ |
| 50 |
return sqrt (pow (y0 - y1, 2) + pow (x0 - x1, 2)); |
| 51 |
} |
| 52 |
|
| 53 |
/* FIXME .. this.. could.. be.. a.. smaller.. and especially.. faster.. function :) */ |
| 54 |
int _oink_line_xory_next_xy (int xory, int length, int x0, int y0, int x1, int y1) |
| 55 |
{ |
| 56 |
int dy = y1 - y0; |
| 57 |
int dx = x1 - x0; |
| 58 |
int stepx, stepy; |
| 59 |
int fraction; |
| 60 |
int pixdrawn = 0; |
| 61 |
|
| 62 |
if(dy < 0) |
| 63 |
{ |
| 64 |
dy = -dy; |
| 65 |
stepy = -1; |
| 66 |
} |
| 67 |
else |
| 68 |
{ |
| 69 |
stepy = 1; |
| 70 |
} |
| 71 |
|
| 72 |
if (dx < 0) |
| 73 |
{ |
| 74 |
dx = -dx; |
| 75 |
stepx = -1; |
| 76 |
} |
| 77 |
else |
| 78 |
{ |
| 79 |
stepx = 1; |
| 80 |
} |
| 81 |
|
| 82 |
dy <<= 1; |
| 83 |
dx <<= 1; |
| 84 |
|
| 85 |
if (length == 0 && xory == 0) |
| 86 |
return x0; |
| 87 |
|
| 88 |
if (length == 0 && xory == 1) |
| 89 |
return y0; |
| 90 |
|
| 91 |
if (dx > dy) |
| 92 |
{ |
| 93 |
fraction = dy - (dx >> 1); |
| 94 |
|
| 95 |
while (x0 != x1) |
| 96 |
{ |
| 97 |
if (fraction >= 0) |
| 98 |
{ |
| 99 |
y0 += stepy; |
| 100 |
fraction -= dx; |
| 101 |
} |
| 102 |
x0 += stepx; |
| 103 |
fraction += dy; |
| 104 |
|
| 105 |
pixdrawn++; |
| 106 |
|
| 107 |
if (pixdrawn >= length) |
| 108 |
{ |
| 109 |
if (xory == 0) |
| 110 |
return x0; |
| 111 |
else |
| 112 |
return y0; |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
else |
| 117 |
{ |
| 118 |
fraction = dx - (dy >> 1); |
| 119 |
while (y0 != y1) |
| 120 |
{ |
| 121 |
if (fraction >= 0) |
| 122 |
{ |
| 123 |
x0 += stepx; |
| 124 |
fraction -= dy; |
| 125 |
} |
| 126 |
y0 += stepy; |
| 127 |
fraction += dx; |
| 128 |
|
| 129 |
pixdrawn++; |
| 130 |
|
| 131 |
if (pixdrawn >= length) |
| 132 |
{ |
| 133 |
if (xory == 0) |
| 134 |
return x0; |
| 135 |
else |
| 136 |
return y0; |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
if (xory == 0) |
| 142 |
return x0; |
| 143 |
else |
| 144 |
return y0; |
| 145 |
} |
| 146 |
|
| 147 |
void _oink_pixel_rotate (int *x, int *y, int rot) |
| 148 |
{ |
| 149 |
int tx = *x; |
| 150 |
|
| 151 |
*x = (tx * _oink_table_cos[rot % OINK_TABLE_NORMAL_SIZE]) + (*y * _oink_table_sin[rot % OINK_TABLE_NORMAL_SIZE]); |
| 152 |
*y = (tx * _oink_table_sin[rot % OINK_TABLE_NORMAL_SIZE]) - (*y * _oink_table_cos[rot % OINK_TABLE_NORMAL_SIZE]); |
| 153 |
} |
| 154 |
|