rotate function

List<double> rotate(
  1. List<double> out,
  2. List<double> a,
  3. List<double> b,
  4. double rad,
)

Rotate a 2D vector @param {vec2} out The receiving vec2 @param {ReadonlyVec2} a The vec2 point to rotate @param {ReadonlyVec2} b The origin of the rotation @param {Number} rad The angle of rotation in radians @returns {vec2} out

Implementation

List<double> rotate(List<double> out, List<double> a, List<double> b, double rad) {
  //Translate point to the origin
  final p0 = a[0] - b[0], p1 = a[1] - b[1], sinC = math.sin(rad), cosC = math.cos(rad);

  //perform rotation and translate to correct position
  out[0] = p0 * cosC - p1 * sinC + b[0];
  out[1] = p0 * sinC + p1 * cosC + b[1];

  return out;
}