clamp method

Vec2 clamp(
  1. VectorBase other
)

Clamps the given vector so that it falls within the boundaries formed by a rectangle between the origin (0, 0) and this vector.

Implementation

Vec2 clamp(VectorBase other) {
  int newX, newY;

  if (x < 0) {
    newX = other.x.clamp(x, 0).toInt();
  } else {
    newX = other.x.clamp(0, x).toInt();
  }

  if (y < 0) {
    newY = other.y.clamp(y, 0).toInt();
  } else {
    newY = other.y.clamp(0, y).toInt();
  }

  return Vec2(newX, newY);
}