getDashedPath method

Path getDashedPath({
  1. required Point<double> a,
  2. required Point<double> b,
  3. required double gap,
})

Implementation

Path getDashedPath(
    {required math.Point<double> a,
    required math.Point<double> b,
    required double gap}) {
  final Size size = Size(b.x - a.x, b.y - a.y);
  final Path path = Path();
  path.moveTo(a.x, a.y);
  bool shouldDraw = true;
  math.Point<double> currentPoint = math.Point<double>(a.x, a.y);
  final num radians = math.atan(size.height / size.width);
  final num dx = math.cos(radians) * gap < 0
      ? math.cos(radians) * gap * -1
      : math.cos(radians) * gap;
  final num dy = math.sin(radians) * gap < 0
      ? math.sin(radians) * gap * -1
      : math.sin(radians) * gap;
  while (currentPoint.x <= b.x && currentPoint.y <= b.y) {
    shouldDraw
        ? path.lineTo(currentPoint.x.toDouble(), currentPoint.y.toDouble())
        : path.moveTo(currentPoint.x.toDouble(), currentPoint.y.toDouble());
    shouldDraw = !shouldDraw;
    currentPoint = math.Point<double>(
        currentPoint.x + dx.toDouble(), currentPoint.y + dy.toDouble());
  }
  return path;
}