getTangent method

Vector getTangent(
  1. double t, [
  2. Vector? optionalTarget
])

t - A position on the curve. Must be in the range 0, 1 .

optionalTarget — (optional) If specified, the result will be copied into this Vector, otherwise a new Vector will be created.

Returns a unit vector tangent at t. If the derived curve does not implement its tangent derivation, two points a small delta apart will be used to find its gradient which seems to give a reasonable approximation.

Implementation

Vector getTangent(double t, [Vector? optionalTarget]) {
  const delta = 0.0001;
  double t1 = t - delta;
  double t2 = t + delta;

  // Capping in case of danger

  if (t1 < 0) t1 = 0;
  if (t2 > 1) t2 = 1;

  final pt1 = getPoint(t1);
  final pt2 = getPoint(t2);

  final tangent = optionalTarget ??
    ((pt1.runtimeType == Vector2)?Vector2(): Vector3());

  if(pt2 != null && pt1 != null){
    tangent.setFrom(pt2).sub(pt1).normalize();
  }

  return tangent;
}