transformPoint static method

Offset transformPoint(
  1. Matrix4 transform,
  2. Offset point
)

Applies the given matrix as a perspective transform to the given point.

This function assumes the given point has a z-coordinate of 0.0. The z-coordinate of the result is ignored.

While not common, this method may return (NaN, NaN), iff the given point results in a "point at infinity" in homogeneous coordinates after applying the transform. For example, a RenderObject may set its transform to the zero matrix to indicate its content is currently not visible. Trying to convert an Offset to its coordinate space always results in (NaN, NaN).

Implementation

static Offset transformPoint(Matrix4 transform, Offset point) {
  final Float64List storage = transform.storage;
  final double x = point.dx;
  final double y = point.dy;

  // Directly simulate the transform of the vector (x, y, 0, 1),
  // dropping the resulting Z coordinate, and normalizing only
  // if needed.

  final double rx = storage[0] * x + storage[4] * y + storage[12];
  final double ry = storage[1] * x + storage[5] * y + storage[13];
  final double rw = storage[3] * x + storage[7] * y + storage[15];
  if (rw == 1.0) {
    return Offset(rx, ry);
  } else {
    return Offset(rx / rw, ry / rw);
  }
}