import 'dart:math'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:scence_map/controllers/controller.dart'; import 'package:scence_map/controllers/plumController.dart'; ScenceMapController mapController = Get.find(); class DrawDirection extends CustomPainter { PlumDataController controller; Offset pos; bool isDarkMode; bool isInit; DrawDirection(this.controller, this.pos, this.isDarkMode, this.isInit); @override void paint(Canvas canvas, Size size) { controller.canvasSize = size; final paint = Paint(); Path path = Path(); paint ..strokeWidth = 2 ..style = PaintingStyle.stroke ..color = isDarkMode ? Colors.white : Colors.black; if (controller.linePointOffset.value == Offset.zero || controller.centerXY.value == Offset.zero) { return; } Offset linePointOffset = mapController.xy2Screen( controller.linePointXY.value.dx, controller.linePointXY.value.dy); Offset centerOffset = controller.centerOffset.value; Offset centerXY = controller.centerXY.value; if (isInit) { linePointOffset = linePointOffset + pos; centerOffset = controller.centerOffset.value + pos; } print("-----矩阵中心$centerOffset,$centerXY"); path.moveTo(centerOffset.dx, centerOffset.dy); path.lineTo(linePointOffset.dx, linePointOffset.dy); canvas.drawPath(path, paint); paint.style = PaintingStyle.fill; canvas.drawCircle(linePointOffset, 10, paint); // canvas.translate(centerOffset.dx, centerOffset.dy); // canvas.rotate(controller.angle.value); // canvas.translate(-centerOffset.dx, -centerOffset.dy); paint // ..color = Colors.grey.withOpacity(.4) .style = PaintingStyle.stroke; if (controller.isUp.value && controller.angle.value != 0) { double unit = mapController.plottingScale.value / mapController.pixel2MeterRatio; // 沿着路径绘制圆圈 double distance = (linePointOffset - centerOffset).distance; int horizonalCircles = (distance / unit).floor(); int verticalCircles = (controller.pileWidth / unit).floor(); // controller.plumList.length = 0; List circlePoints = []; // List plumList = []; controller.plumList.length = 0; for (int i = 0; i <= horizonalCircles; i++) { double t = i * unit / distance; Offset point = Offset.lerp(centerOffset, linePointOffset, t)!; canvas.drawCircle(point, .3 / mapController.pixel2MeterRatio, paint); circlePoints.add(point); // Offset xy = mapController.screen2xy(point.dx, point.dy); // plumList.add(Offset(xy.dx, xy.dy)); controller.plumList.add(point); } // 在反方向延长线上绘制对称的圆圈 for (int i = 1; i <= horizonalCircles; i++) { double t = i * unit / distance; Offset point = Offset.lerp(centerOffset, linePointOffset, -t)!; canvas.drawCircle(point, .3 / mapController.pixel2MeterRatio, paint); circlePoints.add(point); controller.plumList.add(point); //List保存 } // 计算垂直于path的方向向量 Offset direction = (linePointOffset - centerOffset).scale(1 / distance, 1 / distance); Offset perpendicular = Offset(-direction.dy, direction.dx); // 绘制上下方的圆圈,形成正方形矩阵 for (Offset point in circlePoints) { for (int j = 0; j <= verticalCircles / 2; j++) { Offset upPoint = point + perpendicular * (j * unit); Offset downPoint = point - perpendicular * (j * unit); canvas.drawCircle( upPoint, .3 / mapController.pixel2MeterRatio, paint); canvas.drawCircle( downPoint, .3 / mapController.pixel2MeterRatio, paint); // Offset xy = mapController.screen2xy0(upPoint); // Offset xy1 = mapController.screen2xy0(downPoint); controller.plumList.add(upPoint); controller.plumList.add(downPoint); } } // if (plumList.isNotEmpty) { // controller.plumList.value = plumList; // // controller.update(); // } } } @override bool shouldRepaint(covariant CustomPainter oldDelegate) => true; }