function Max takes real a, real b returns real if ( a > b ) then return a endif return b endfunction function Min takes real a, real b returns real if ( a < b ) then return a endif return b endfunction function Absolute takes real a returns real if ( a < 0 ) then return -a endif return a endfunction function DistanceByCoordinates takes real x1, real y1, real x2, real y2 returns real return SquareRoot( ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ) endfunction function DistanceByCoordinatesWithZ takes real x1, real y1, real z1, real x2, real y2, real z2 returns real return SquareRoot( ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) + ( z1 - z2 ) * ( z1 - z2 ) ) endfunction function Sign takes real a returns integer if ( a < 0 ) then return -1 elseif ( a == 0 ) then return 0 endif return 1 endfunction function PowInteger takes integer base, integer exponent returns integer return R2I( Pow( base, exponent ) ) endfunction function AngleBetweenVectors takes real a_x, real a_y, real b_x, real b_y returns real return Acos( ( a_x * b_x + a_y * b_y ) / ( SquareRoot( a_x * a_x + a_y * a_y ) * SquareRoot( b_x * b_x + b_y * b_y ) ) ) endfunction function AreCoordinatesInTriangle_Child takes real A_x, real A_y, real B_x, real B_y, real C_x, real C_y returns integer return Sign( A_x * ( B_y - C_y ) + A_y * ( C_x - B_x ) + B_x * C_y - B_y * C_x ) endfunction function AreCoordinatesInTriangle takes real A_x, real A_y, real B_x, real B_y, real C_x, real C_y, real x, real y returns boolean local real Average_x = ( A_x + B_x + C_x ) / 3 local real Average_y = ( A_y + B_y + C_y ) / 3 if ( AreCoordinatesInTriangle_Child( Average_x, Average_y, A_x, A_y, B_x, B_y ) == AreCoordinatesInTriangle_Child( x, y, A_x, A_y, B_x, B_y ) ) then if ( AreCoordinatesInTriangle_Child( Average_x, Average_y, A_x, A_y, C_x, C_y ) == AreCoordinatesInTriangle_Child( x, y, A_x, A_y, C_x, C_y ) ) then if ( AreCoordinatesInTriangle_Child( Average_x, Average_y, B_x, B_y, C_x, C_y ) == AreCoordinatesInTriangle_Child( x, y, B_x, B_y, C_x, C_y ) ) then return true endif endif endif return false endfunction function AreCoordinatesInQuadrilateral takes real A_x, real A_y, real B_x, real B_y, real C_x, real C_y, real D_x, real D_y, real x, real y returns boolean if ( ( AngleBetweenVectors( B_x - A_x, B_y - A_y, D_x - A_x, D_y - A_y ) + AngleBetweenVectors( B_x - C_x, B_y - C_y, D_x - C_x, D_y - C_y ) ) > ( AngleBetweenVectors( A_x - B_x, A_y - B_y, C_x - B_x, C_y - B_y ) + AngleBetweenVectors( A_x - D_x, A_y - D_y, C_x - D_x, C_y - D_y ) ) ) then if ( ( AreCoordinatesInTriangle( A_x, A_y, B_x, B_y, C_x, C_y, x, y ) ) or ( AreCoordinatesInTriangle( A_x, A_y, C_x, C_y, D_x, D_y, x, y ) ) ) then return true endif else if ( ( AreCoordinatesInTriangle( A_x, A_y, B_x, B_y, D_x, D_y, x, y ) ) or ( AreCoordinatesInTriangle( B_x, B_y, C_x, C_y, D_x, D_y, x, y ) ) ) then return true endif endif return false endfunction function CutReal takes real a returns real local string b = R2S( a ) local string hyphen = "." local integer iteration = 1 loop exitwhen hyphen == SubString( b, iteration - 1, iteration ) set iteration = iteration + 1 endloop set b = SubString( b, 0, iteration + 5 ) return S2R( b ) endfunction function ModulateReal takes real dividend, real divisor returns real local real result set dividend = CutReal( dividend ) set divisor = CutReal( divisor ) if ( divisor == 0 ) then set result = -1 else set result = dividend loop exitwhen ( result < divisor ) set result = result - divisor endloop endif return result endfunction function RoundTo takes real base, real interval returns real local real difference1 local real difference2 if ( interval == 0 ) then return interval endif set difference1 = ModulateReal( base, interval ) set difference2 = interval - difference1 if ( difference2 < difference1 ) then return ( base + difference2 ) endif return ( base - difference1 ) endfunction function AngleRadianBetweenCoords takes real x1, real y1, real x2, real y2 returns real return Atan2( y2 - y1, x2 - x1 ) endfunction function GetAngleDifferenceRadian takes real a, real b returns real local real period = bj_PI * 2 local real result set a = ModuloReal( a, period ) set b = ModuloReal( b, period ) set result = RAbsBJ( a - b ) if ( result > period / 2 ) then return ( period - result ) endif return result endfunction