hedgewars/uFloat.pas
changeset 357 165a040e4cfa
parent 355 40c68869899e
child 358 236bbd12d4d9
equal deleted inserted replaced
356:ca3a5dfcae75 357:165a040e4cfa
    51 
    51 
    52 function cstr(z: hwFloat): string;
    52 function cstr(z: hwFloat): string;
    53 function hwRound(t: hwFloat): integer;
    53 function hwRound(t: hwFloat): integer;
    54 function hwAbs(t: hwFloat): hwFloat;
    54 function hwAbs(t: hwFloat): hwFloat;
    55 function hwSqr(t: hwFloat): hwFloat;
    55 function hwSqr(t: hwFloat): hwFloat;
       
    56 function hwSqrt(t: hwFloat): hwFloat;
    56 function Distance(dx, dy: hwFloat): hwFloat;
    57 function Distance(dx, dy: hwFloat): hwFloat;
    57 function AngleSin(angle: Longword): hwFloat;
    58 function AngleSin(Angle: Longword): hwFloat;
    58 function AngleCos(angle: Longword): hwFloat;
    59 function AngleCos(Angle: Longword): hwFloat;
    59 
    60 
    60 const  _1div1024: hwFloat = (isNegative: false; QWordValue:     4194304);
    61 const  _1div1024: hwFloat = (isNegative: false; QWordValue:     4194304);
    61       _1div10000: hwFloat = (isNegative: false; QWordValue:      429496);
    62       _1div10000: hwFloat = (isNegative: false; QWordValue:      429496);
    62       _1div50000: hwFloat = (isNegative: false; QWordValue:       85899);
    63       _1div50000: hwFloat = (isNegative: false; QWordValue:       85899);
    63      _1div100000: hwFloat = (isNegative: false; QWordValue:       42950);
    64      _1div100000: hwFloat = (isNegative: false; QWordValue:       42950);
   244 function hwSqr(t: hwFloat): hwFloat;
   245 function hwSqr(t: hwFloat): hwFloat;
   245 begin
   246 begin
   246 hwSqr:= t * t
   247 hwSqr:= t * t
   247 end;
   248 end;
   248 
   249 
       
   250 function hwSqrt(t: hwFloat): hwFloat;
       
   251 begin
       
   252 hwSqrt.isNegative:= false;
       
   253 hwSqrt.QWordValue:= Round(sqrt(1.0 / $100000000 * (t.QWordValue)) * $100000000)
       
   254 end;
       
   255 
   249 function Distance(dx, dy: hwFloat): hwFloat;
   256 function Distance(dx, dy: hwFloat): hwFloat;
   250 var x, y: hwFloat;
   257 var x, y: hwFloat;
   251     Result: hwFloat;
   258     Result: hwFloat;
   252 begin
   259 begin
   253 x:= dx * dx;
   260 x:= dx * dx;
   254 y:= dy * dy;
   261 y:= dy * dy;
   255 Result:= x + y;
   262 Result:= x + y;
   256 Result.QWordValue:= Round(sqrt(1.0 / 4294967296 * (Result.QWordValue)) * 4294967296);
   263 Result.QWordValue:= Round(sqrt(1.0 / $100000000 * (Result.QWordValue)) * $100000000);
   257 Distance:= Result
   264 Distance:= Result
   258 end;
   265 end;
   259 
   266 
   260 function AngleSin(angle: Longword): hwFloat;
   267 {$INCLUDE SinTable.inc}
       
   268 
       
   269 function AngleSin(Angle: Longword): hwFloat;
   261 begin
   270 begin
   262 AngleSin.isNegative:= false;
   271 AngleSin.isNegative:= false;
   263 AngleSin.QWordValue:= Round(Sin(Angle * pi / cMaxAngle) * 4294967296)
   272 if Angle < 1024 then AngleSin.QWordValue:= SinTable[Angle]
   264 end;
   273                 else AngleSin.QWordValue:= SinTable[2048 - Angle]
   265 
   274 end;
   266 function AngleCos(angle: Longword): hwFloat;
   275 
       
   276 function AngleCos(Angle: Longword): hwFloat;
   267 var CosVal: Extended;
   277 var CosVal: Extended;
   268 begin
   278 begin
   269 CosVal:= Cos(Angle * pi / cMaxAngle);
   279 AngleCos.isNegative:= Angle > 1024;
   270 AngleCos.isNegative:= CosVal < 0;
   280 if Angle < 1024 then AngleCos.QWordValue:= SinTable[1024 - Angle]
   271 AngleCos.QWordValue:= Round(Abs(Cosval) * 4294967296)
   281                 else AngleCos.QWordValue:= SinTable[Angle - 1024]
   272 end;
   282 end;
   273 
   283 
   274 {$ENDIF}
   284 {$ENDIF}
   275 
   285 
   276 end.
   286 end.