Last active
December 12, 2015 08:29
-
-
Save Chase-san/4744590 to your computer and use it in GitHub Desktop.
A set of utilities for FightCode.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* I prefer the singleton class creation method. */ | |
var CSUtil = { | |
TWO_PI: Math.PI * 2, | |
TO_DEG: 180/Math.PI, | |
/** | |
* Returns the angle from x1y1 to x2y2. This function will accept | |
* robot.position, scannedRobot.position, and wrap. | |
*/ | |
angleTo: function(p,q) { | |
return Math.atan2(p.y-q.y,p.x-q.x); | |
}, | |
/** | |
* Returns the distance from p to q. This function will accept | |
* robot.position, scannedRobot.position, and wrap. | |
*/ | |
distance: function(p,q) { | |
var tmp = 0; | |
return Math.sqrt((tmp=(p.x-q.x))*tmp + (tmp=(p.y-q.y))*tmp); | |
}, | |
/** | |
* Returns the square of the distance from p to q. This function will | |
* accept robot.position, scannedRobot.position, and wrap. | |
*/ | |
distanceSq: function(p,q) { | |
var tmp = 0; | |
return (tmp=(p.x-q.x))*tmp + (tmp=(p.y-q.y))*tmp; | |
}, | |
/** | |
* Normalizes an angle to an absolute angle. The normalized angle will | |
* be in the range from 0 to 2*PI, where 2*PI itself is not included. | |
*/ | |
normalAbsoluteAngle: function(angle) { | |
return (angle %= this.TWO_PI) >= 0 ? angle : (angle + this.TWO_PI); | |
}, | |
/** | |
* Normalizes an angle to an absolute angle. The normalized angle will | |
* be in the range from 0 to 360, where 360 itself is not included. | |
*/ | |
normalAbsoluteAngleDegrees: function(angle) { | |
return (angle %= 360) >= 0 ? angle : (angle + 360); | |
}, | |
/** | |
* Normalizes an angle to a relative angle. The normalized angle will | |
* be in the range from -PI to PI, where PI itself is not included. | |
*/ | |
normalRelativeAngle: function(angle) { | |
return (angle %= this.TWO_PI) >= 0 ? (angle < Math.PI) ? angle | |
: angle - this.TWO_PI : (angle >= -Math.PI) ? angle : angle | |
+ this.TWO_PI; | |
}, | |
/** | |
* Normalizes an angle to a relative angle. | |
* The normalized angle will be in the range from -180 to 180, where 180 | |
* itself is not included. | |
*/ | |
normalRelativeAngleDegrees: function(angle) { | |
return (angle %= 360) >= 0 ? (angle < 180) ? angle : angle - 360 | |
: (angle >= -180) ? angle : angle + 360; | |
}, | |
/** | |
* Calculates a location given a start location and an angle and distance | |
* from that location. This function will accept robot.position, | |
* scannedRobot.position, and wrap. | |
*/ | |
project: function(start,angle,length) { | |
return { | |
x: start.x + Math.cos(angle) * length, | |
y: start.y + Math.sin(angle) * length | |
}; | |
}, | |
/** | |
* Returns 1 if the value is greater than 0, | |
* Returns -1 if the value is less than 0, | |
* Returns 0 if the value is equal to 0. | |
*/ | |
signum: function(value) { | |
if(value > 0) return 1; | |
if(value < 0) return -1; | |
return 0; | |
}, | |
/** | |
* Coverts a angle in radians to an angle in degrees. | |
*/ | |
toDegrees: function(rad) { | |
return rad*this.TO_DEG; | |
}, | |
/** | |
* Coverts a angle in degrees to an angle in radians. | |
*/ | |
toRadians: function(deg) { | |
return deg/this.TO_DEG; | |
}, | |
/** | |
* Wraps a set of coordinates in a javascript object. | |
*/ | |
wrap: function(x,y) { | |
return { | |
x: x, | |
y: y | |
}; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment