findIncircle function

Map<String, dynamic> findIncircle(
  1. List<List<double>> points
)

Computes the incircle of a triangle.

The triangle is represented by a list of three points. Each point is a list of two doubles (x, y).

The function returns a Map with two keys:

  • 'center': a list of two doubles representing the coordinates of the center of the incircle.
  • 'radius': a double representing the radius of the incircle.

Throws ArgumentError if the input is not a list of three points.

Example:

var triangle = [
  [0.0, 0.0],
  [1.0, 0.0],
  [0.5, sqrt(3)/2]
];
var incircle = findIncircle(triangle);
print(incircle);  // prints: {'center': [0.5, 0.2886751345948129], 'radius': 0.2886751345948129}

points: A list of three points representing the triangle. Each point is a list of two doubles (x, y).

Returns a Map with the center and radius of the incircle of the triangle.

Implementation

Map<String, dynamic> findIncircle(List<List<double>> points) {
  if (points.length != 3) {
    throw ArgumentError("Input must be a list of three points.");
  }

  // Compute the side lengths of the triangle.
  double a = sqrt(pow(points[1][0] - points[0][0], 2) +
      pow(points[1][1] - points[0][1], 2));
  double b = sqrt(pow(points[2][0] - points[1][0], 2) +
      pow(points[2][1] - points[1][1], 2));
  double c = sqrt(pow(points[0][0] - points[2][0], 2) +
      pow(points[0][1] - points[2][1], 2));

  // Compute the semiperimeter of the triangle.
  double s = (a + b + c) / 2;

  // Compute the radius of the incircle.
  double radius = sqrt((s - a) * (s - b) * (s - c) / s);

  // Compute the coordinates of the incenter.
  double x =
      (a * points[2][0] + b * points[0][0] + c * points[1][0]) / (a + b + c);
  double y =
      (a * points[2][1] + b * points[0][1] + c * points[1][1]) / (a + b + c);

  return {
    'center': [x, y],
    'radius': radius
  };
}