calculateCurvature function

double calculateCurvature(
  1. Tangent currentTangent,
  2. PathMetric pathMetric,
  3. double distance,
  4. double step,
)

Calculates the curvature at a given distance along the path.

The currentTangent parameter represents the tangent at the current distance. The pathMetric parameter represents the path metric of the path. The distance parameter specifies the distance along the path. The step parameter specifies the step size for calculating the next tangent.

Returns the curvature as a double value.

Implementation

double calculateCurvature(Tangent currentTangent, PathMetric pathMetric,
    double distance, double step) {
  // Calculate the next tangent in the path
  double nextDistance = distance + step;
  if (nextDistance >= pathMetric.length) {
    nextDistance = pathMetric.length - 1;
  }
  Tangent? nextTangent = pathMetric.getTangentForOffset(nextDistance);

  if (nextTangent == null) return 0.0;

  // Calculate the angle difference between the current and next tangent
  double currentAngle = currentTangent.angle;
  double nextAngle = nextTangent.angle;
  double angleDifference = (nextAngle - currentAngle).abs();

  // Normalize the angle difference
  if (angleDifference > pi) {
    angleDifference = 2 * pi - angleDifference;
  }

  return angleDifference;
}