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
|
#include "trigtable.h"
#include "defs.h"
#include <math.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
/*
double tcos[360];
double tsine[360];
double rsine[360];
double ttan[360];
*/
double tsqr( const double r ) {
return r*r;
}
long int tcube( const long int c ) {
return c*c*c;
}
double fsqr( double r ) {
return r*r;
}
double fcube( double r ) {
return r*r*r;
}
double tsqrt( const double q ) {
return sqrt( q );
if ( q <= 0 )
return 0;
long int min = 0;
while ( tsqr( min ) < q )
min += 30;
while ( tsqr( min ) > q )
min -= 10;
while ( tsqr( min ) < q )
min++;
if ( tsqr( min ) > q )
min--;
return min;
}
double dist( const int x1, const int y1, const int x2, const int y2 ) {
double a = tsqr( y1 - y2 );
double b = tsqr( x1 - x2 );
return tsqrt( a + b );
}
double zdist( int x, int y, int x1, int y1 ) {
double a = tsqr(x-x1);
double b = tsqr(y-y1);
return sqrt( a+b );
}
long int area( int x1, int y1, int x2, int y2, int x3, int y3 ) {
double d1 = zdist(x1,y1,x2,y2);
double d2 = zdist(x1,y1,x3,y3);
double d3 = zdist(x2,y2,x3,y3);
double s = ( d1 + d2 + d3 ) / 2.0;
// area = sqrt( s * (s-d1) * (s-d2) * (s-d3) )
long int atotal = (long int)(sqrt(s*(s-d1)) * sqrt((s-d2)*(s-d3)));
return atotal;
}
bool ptriangle( int tx1, int ty1, int tx2, int ty2, int tx3, int ty3, int x1, int y1 ) {
long int atotal = area( tx1, ty1, tx2, ty2, tx3, ty3 )+2;
long int a1 = area( tx1, ty1, tx2, ty2, x1, y1 );
if ( a1 > atotal ) return false;
long int a2 = area( tx1, ty1, tx3, ty3, x1, y1 );
if ( a2 > atotal ) return false;
long int a3 = area( tx2, ty2, tx3, ty3, x1, y1 );
if ( a3 > atotal ) return false;
if ( a1+a2+a3 > atotal )
return false;
return true;
}
bool prect( int px1, int py1, int x1, int y1, int x2, int y2 ) {
if ( px1 >= x1 && px1 <= x2 && py1 >= y1 && py1 <= y2 )
return true;
return false;
}
/*
int sarctan( const double tr ) {
if ( tr == 0.0 )
return 0;
int my_ang = 0;
double precision = fabs(ttan[0] - tr );
for ( int r = 1; r < 180; r++ ) {
double dif = fabs( ttan[r] - tr );
if ( dif == 0 )
return r;
if ( dif < precision ) {
precision = dif;
my_ang = r;
}
}
return my_ang;
}
*/
/*
int barctan( const double tr ) {
int my_ang = 180;
double precision = fabs(ttan[180] - tr );
for ( int r = 181; r < 360; r++ ) {
double dif = fabs( ttan[r] - tr );
if ( dif <= precision ) {
precision = dif;
my_ang = r;
}
}
return my_ang;
}
*/
int lineIntersect( int ax, int ay, int bx, int by, int cx, int cy, int dx, int dy, int *ix, int *iy ) {
float r, s;
float denom = (bx-ax)*(dy-cy)-(by-ay)*(dx-cx);
r = (float)((ay-cy)*(dx-cx)-(ax-cx)*(dy-cy))/denom;
s = (float)((ay-cy)*(bx-ax)-(ax-cx)*(by-ay))/denom;
if ( r < 0.0 || s < 0.0 || r > 1.0 || s > 1.0 )
return 0;
*ix=ax+(int)(r*(float)(bx-ax)+0.5);
*iy=ay+(int)(r*(float)(by-ay)+0.5);
return 1;
} // end lineIntersect
/* nicer name for gang */
int getAngle( int x1, int y1, int x2, int y2 ){
return gang( x1, y1, x2, y2 );
}
int gang( int x1, int y1, int x2, int y2 ) {
int tang = 0;
if ( x1 == x2 ) {
if ( y1 < y2 )
tang = 90;
else
tang = 270;
}
if ( y1 == y2 ) {
if ( x1 < x2 )
tang = 0;
else
tang = 180;
} else
tang = (int)(0.5+atan2( (double) -(y2-y1), (double) (x2 - x1) ) * 180.0 / M_PI );
while ( tang < 0 )
tang += 360;
return tang % 360;
}
int arcsine( double x ) {
int tang = (int)(0.5 + asin( x * 180.0 / M_PI) );
while ( tang < 0 )
tang += 360;
return tang % 360;
}
/*
int find_sin_pos( double q, bool side )
{
double low = 9999;
int keep;
for ( int x = side*90; x <= side*90+90; x++ )
{
if ( fabs(tsine[x]-q) < low )
{
low = fabs(tsine[x]-q);
keep = x;
}
}
return keep;
}
int find_sin_neg( double q, bool side )
{
double low = 9999;
int keep;
for ( int x = side*90+180; x < side*90+270; x++ )
{
if ( fabs(tsine[x]-q) < low )
{
low = fabs(tsine[x]-q);
keep = x;
}
}
return keep;
}
int mang( int x1, int y1, int x2, int y2 )
{
if ( x1 == x2 )return 0;
if ( y1 == y2 )return 0;
int t1 = dist( x1, y1, x2, y2 );
if ( y2 > y1 )
{
double cr = (double)t1 / (double)( y2 - y1 );
return find_sin_pos( cr, x1>x2 );
}
else
{
double cr = (double)t1 / (double)( y1 - y2 );
return find_sin_neg( cr, x1>x2 );
}
}
*/
/*
void set_trig() {
for ( int r = 0; r<360; r++ ) {
tcos[ r ] = cos( (double)(r) * AL_PI / 180.0 );
tsine[ r ] = -sin( (double)(r) * AL_PI / 180.0 );
rsine[ r ] = sin( (double)(r) * AL_PI / 180.0 );
if ( tcos[r] == 0.0 )
ttan[r] = 99999.0;
else
ttan[ r ] = tsine[r] / tcos[r];
}
}
*/
|