getPickerGeometry method Null safety

PickerGeometry getPickerGeometry(
  1. double lightness
)

Implementation

static PickerGeometry getPickerGeometry(double lightness) {
// Array of lines
  var lines = Hsluv.getBounds(lightness);
  var numLines = lines.length;
  var outerCircleRadius = 0.0;

// Find the line closest to origin
  int? closestIndex2;
  double? closestLineDistance;

  for (int i = 0; i < numLines; i += 1) {
    var d = Geometry.distanceLineFromOrigin(lines[i]);
    if (closestLineDistance == null || d < closestLineDistance) {
      closestLineDistance = d;
      closestIndex2 = i;
    }
  }

  var closestLine = lines[closestIndex2!];
  var perpendicularLine =
      Line(slope: 0 - (1 / closestLine.slope), intercept: 0.0);
  var intersectionPoint =
      Geometry.intersectLineLine(closestLine, perpendicularLine);
  var startingAngle = Geometry.angleFromOrigin(intersectionPoint);

  var intersections = [];
  var intersectionPointAngle;

  for (int i1 = 0; i1 < numLines - 1; i1 += 1) {
    for (int i2 = 0; i1 + 1 < numLines; i2 += 1) {
      intersectionPoint = Geometry.intersectLineLine(lines[i1], lines[i2]);
      intersectionPointAngle = Geometry.angleFromOrigin(intersectionPoint);
      intersections.add({
        "line1": i1,
        "line2": i2,
        "intersectionPoint": intersectionPoint,
        "intersectionPointAngle": intersectionPointAngle,
        "relativeAngle":
            Geometry.normalizeAngle(intersectionPointAngle - startingAngle)
      });
    }
  }

  intersections.sort((a, b) {
    if (a["relativeAngle"] > b["relativeAngle"]) {
      return 1;
    } else {
      return -1;
    }
  });

  final List<Line> orderedLines = [];
  final List<Point> orderedVertices = [];
  final List<double> orderedAngles = [];

  var nextIndex2;
  var currentIntersection;
  var intersectionPointDistance;

  int? currentIndex2 = closestIndex2;
  var d = [];

  for (int j = 0; j < intersections.length; j += 1) {
    currentIntersection = intersections[j];
    nextIndex2 = null;
    if (currentIntersection["line1"] == currentIndex2) {
      nextIndex2 = currentIntersection["line2"];
    } else if (currentIntersection["line2"] == currentIndex2) {
      nextIndex2 = currentIntersection["line1"];
    }
    if (nextIndex2 != null) {
      currentIndex2 = nextIndex2;

      d.add(currentIndex2);
      orderedLines.add(lines[nextIndex2]);
      orderedVertices.add(currentIntersection["intersectionPoint"]);
      orderedAngles.add(currentIntersection["intersectionPointAngle"]);

      intersectionPointDistance = Geometry.distanceFromOrigin(
          currentIntersection["intersectionPoint"]);
      if (intersectionPointDistance > outerCircleRadius) {
        outerCircleRadius = intersectionPointDistance;
      }
    }
  }

  return PickerGeometry(
    lines: orderedLines,
    vertices: orderedVertices,
    angles: orderedAngles,
    outerCircleRadius: outerCircleRadius,
    innerCircleRadius: closestLineDistance,
  );
}