The following code is a version of the usual function required in an analog watchface to rotate the various shapes (which all are polygons with four corners in my case). It works fine but is now one of two functions that take the most time. So I was wondering if it can be further tuned. I've tried with a lookup table for sin/cos - which doesn't make a difference - and with a 1-dimensional array for result
- which is even slower - and now I'm out of ideas.
Does anyone see any way to further tune this code to make it faster?
//! Rotate the four corner coordinates of a polygon used to draw a watch hand or a tick mark. //! 0 degrees is at the 12 o'clock position, and increases in the clockwise direction. //! @param shape Index of the shape //! @param angle Rotation angle in radians //! @return The rotated coordinates of the polygon (watch hand or tick mark) private function rotateCoords(shape as Shape, angle as Float) as Array< Array<Number> > { var sin = Math.sin(angle); var cos = Math.cos(angle); var shapeIdx = shape * 8; var result = new Array< Array<Number> >[4]; for (var i = 0; i < 4; i++) { var idx = shapeIdx + i * 2; var x = (_coords[idx] * cos - _coords[idx + 1] * sin + 0.5).toNumber(); var y = (_coords[idx] * sin + _coords[idx + 1] * cos + 0.5).toNumber(); result[i] = [_screenCenter[0] + x, _screenCenter[1] + y]; } return result; }
_coords
is a long Array<Number>
with the coordinates of all my shapes. The complete code is here.