张世豪
6 天以前 9d171a3c3a57ea54454d7e9d64dec213aa885a2c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package zhuye;
 
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
 
/**
 * 割草机拖尾绘制工具类。
 */
public final class tuowei {
    private static final Color TRAIL_COLOR = new Color(30, 144, 255, 220);
 
    private tuowei() {
        // 工具类无需实例化
    }
 
    /**
     * 绘制拖尾路径。
     *
     * @param g2d     画笔对象
     * @param samples 拖尾采样点集合
     * @param scale   当前缩放比例
     */
    public static void draw(Graphics2D g2d, Iterable<TrailSample> samples, double scale) {
        if (g2d == null || samples == null) {
            return;
        }
 
        Path2D.Double path = new Path2D.Double();
        boolean started = false;
        for (TrailSample sample : samples) {
            if (sample == null) {
                continue;
            }
            Point2D.Double point = sample.getPoint();
            if (point == null || !Double.isFinite(point.x) || !Double.isFinite(point.y)) {
                continue;
            }
            if (!started) {
                path.moveTo(point.x, point.y);
                started = true;
            } else {
                path.lineTo(point.x, point.y);
            }
        }
 
        if (!started) {
            return;
        }
 
        Stroke originalStroke = g2d.getStroke();
        Color originalColor = g2d.getColor();
 
        float strokeWidth = (float) Math.max(0.08f, 1.4f / (float) Math.max(scale, 0.2));
        g2d.setStroke(new BasicStroke(strokeWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
        g2d.setColor(TRAIL_COLOR);
        g2d.draw(path);
 
        g2d.setStroke(originalStroke);
        g2d.setColor(originalColor);
    }
 
    /**
     * 拖尾采样点。
     */
    public static final class TrailSample {
        private final long timestamp;
        private final Point2D.Double point;
 
        public TrailSample(long timestamp, Point2D.Double point) {
            this.timestamp = timestamp;
            this.point = point;
        }
 
        public long getTimestamp() {
            return timestamp;
        }
 
        public Point2D.Double getPoint() {
            return point;
        }
    }
}