00001 #ifndef _PA_Math
00002 #define _PA_Math
00003
00004 #ifdef __cplusplus
00005 extern "C" {
00006 #endif
00007
00008 #include "PA9.h"
00009
00010 #include "PA_Interrupt.h"
00011
00012
00013
00024 #define PA_Cos(angle) PA_SIN[((angle) + 128)&511]
00025
00026
00032 #define PA_Sin(angle) PA_SIN[((angle))&511]
00033
00034
00035
00036
00037 extern u16 RandomValue;
00038
00044 extern inline u32 PA_Rand(void) {
00045 int i;
00046 RandomValue+=0x9248;
00047
00048 for(i=0;i<3;i++) {
00049 if(RandomValue&0x1) {
00050 RandomValue>>=1;
00051 RandomValue|=0x8000;
00052 }
00053 else RandomValue>>=1;
00054 }
00055 return(RandomValue);
00056 }
00057
00058
00064
00065 extern inline void PA_InitRand(void) {
00066 RandomValue = PA_RTC.Minutes*60 + PA_RTC.Seconds + PA_RTC.Hour*3600 + PA_RTC.Day*3600*24+PA_RTC.Month*3600*24*30;
00067 PA_Rand(); PA_Rand(); PA_Rand();
00068 }
00069
00079
00080 extern inline void PA_SRand(s32 r) {
00081 RandomValue = r;
00082 }
00083
00084
00085
00086
00087
00096 extern inline u32 PA_RandMax(u32 max)
00097 {
00098 return PA_Rand()%(max + 1);
00099 }
00100
00101
00102
00114 extern inline u32 PA_RandMinMax(u32 min,u32 max)
00115 {
00116 return ((PA_Rand()%((max + 1)-min)) + min);
00117 }
00118
00119
00137 extern inline u64 PA_Distance(s32 x1, s32 y1, s32 x2, s32 y2) {
00138 s64 h = x1 - x2;
00139 s64 v = y1 - y2;
00140 return(h*h + v*v);
00141 }
00142
00160 extern inline u64 PA_TrueDistance(s32 x1, s32 y1, s32 x2, s32 y2) {
00161 s64 h = x1 - x2;
00162 s64 v = y1 - y2;
00163 return(swiSqrt(h*h + v*v));
00164 }
00165
00166
00167
00192 u16 PA_AdjustAngle(u16 angle, s16 anglerot, s32 startx, s32 starty, s32 targetx, s32 targety);
00193
00194
00195
00213 extern inline u16 PA_GetAngle(s32 startx, s32 starty, s32 targetx, s32 targety) {
00214 u16 angle = 0;
00215 u16 anglerot = 180;
00216
00217
00218 while(anglerot > 5) {
00219 angle = PA_AdjustAngle(angle, anglerot, startx, starty, targetx, targety);
00220 anglerot = (anglerot - ((3 * anglerot) >> 3));
00221 }
00222
00223
00224 anglerot = 4;
00225 angle = PA_AdjustAngle(angle, anglerot, startx, starty, targetx, targety);
00226 anglerot = 2;
00227 angle = PA_AdjustAngle(angle, anglerot, startx, starty, targetx, targety);
00228 anglerot = 1;
00229 angle = PA_AdjustAngle(angle, anglerot, startx, starty, targetx, targety);
00230
00231 return angle;
00232 }
00233
00235
00236
00237 extern inline s32 PA_Modulo(s32 var, s32 modulo){
00238 while(var < 0) var += modulo;
00239 return (var%modulo);
00240 }
00241
00242 #ifdef __cplusplus
00243 }
00244 #endif
00245
00246 #endif
00247
00248