我是靠谱客的博主 可爱小土豆,这篇文章主要介绍二维坐标工具API前言工具方法和类,现在分享给大家,希望可以做个参考。

前言

处理平面的二维坐标系相关的计算,例如象棋的棋盘就是典型的二维坐标。

工具方法和类

坐标分类

复制代码
1
2
3
4
5
6
7
8
9
10
/** * 坐标线x或者y轴 * * @author leng * */ public enum E_XYConst { X, Y; }

坐标点

复制代码
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
/** * 坐标点 * * @author leng * */ public class Point { /** * x值 */ private int x; /** * y值 */ private int y; public Point() { super(); } public Point(int x, int y) { super(); this.x = x; this.y = y; } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } /** * 坐标点相等 * * @param p * @return */ public boolean equals(Point p) { return this.getX() == p.getX() && this.getY() == p.getY(); } /** * 两个点不相等 * * @return */ public boolean noEquals(Point p) { return this.getX() != p.getX() || this.getY() != p.getY(); } }

线段

复制代码
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
/** * 线段,用于描述一段连续的点<br> * 示例解释start=2,end=5,xy=X,axis=3;代表y值=3,x值大于等于2并且小于等于5的整数点<br> * 即(2,3),(3,3),(4,3),(5, 3) * * @author leng * */ public class Line { /** * 起点值 */ protected int start; /** * 终点值 */ protected int end; /** * 是x轴还是y轴的起点/终点 */ protected E_XYConst xy; /** * 与xy属性对立的轴的值 */ protected int axis; public int getStart() { return start; } public void setStart(int start) { this.start = start; } public int getEnd() { return end; } public void setEnd(int end) { this.end = end; } public E_XYConst getXy() { return xy; } public void setXy(E_XYConst xy) { this.xy = xy; } public int getAxis() { return axis; } public void setAxis(int axis) { this.axis = axis; } }

坐标系的工具方法

复制代码
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import java.util.ArrayList; import java.util.Collections; import java.util.List; /** * 二维坐标工具类,指针对二维坐标有效 * * @author leng * */ public class CoordsUtil { /** * 获取两点之间的坐标,按照到起点的距离排序,距离越近,排序越靠前 * * @param from * 起点 * @param to * 终点 * @param includeFrom * 是否包含起点 * @param includeTo * 是否包含终点 * @return */ public static List<Point> getBetweenPoint(Point from, Point to, boolean includeFrom, boolean includeTo) { List<Point> points = new ArrayList<>(); int minX = Math.min(from.getX(), to.getX()); int maxX = Math.max(from.getX(), to.getX()); int minY = Math.min(from.getY(), to.getY()); int maxY = Math.max(from.getY(), to.getY()); for (int i = minX; i < maxX + 1; i++) { for (int j = minY; j < maxY + 1; j++) { Point p = new Point(i, j); if (p.equals(from) && !includeFrom) { continue; } if (p.equals(to) && !includeTo) { continue; } points.add(p); } } int startX = from.getX(); int startY = from.getY(); Collections.sort(points, (p1, p2) -> { double acreage = Math.pow((p1.getX() - startX), 2) + Math.pow((p1.getY() - startY), 2); double acreage2 = Math.pow((p2.getX() - startX), 2) + Math.pow((p2.getY() - startY), 2); return (int) (acreage - acreage2); }); return points; } /** * 获取两点之间的坐标,按照到起点的距离排序,距离越近,排序越靠前,不包含起点和终点 * * @param from * 起点 * @param to * 终点 * @return */ public static List<Point> getBetweenPoint(Point from, Point to) { return getBetweenPoint(from, to, false, false); } /** * 获取中间的线段,有向线段从起点到终点 * * @param from * 起点 * @param to * 终点 * @return */ public static Line getBetweenLine(Point from, Point to, boolean includeFrom, boolean includeTo) { if (isInLine(from, to)) { Line line = new Line(); if (from.getX() == to.getX()) { line.setXy(E_XYConst.Y); line.setAxis(from.getX()); line.setStart(from.getY()); line.setEnd(to.getY()); } else { line.setXy(E_XYConst.X); line.setAxis(from.getY()); line.setStart(from.getX()); line.setEnd(to.getX()); } int min = Math.min(line.getStart(), line.getEnd()); int max = Math.max(line.getStart(), line.getEnd()); if (!includeFrom) { line.setStart(min == line.getStart() ? min + 1 : max - 1); } if (!includeTo) { line.setEnd(min == line.getEnd() ? min + 1 : max - 1); } return line; } return null; } /** * 集合是否包含该坐标点 * * @param points * 坐标点集合 * @param point * 坐标点 * @return */ public static boolean includePoint(List<Point> points, Point point) { for (Point p : points) { if (p.equals(point)) { return true; } } return false; } /** * 两个坐标点在同一条直线上并且不在同一个点 * * @param point1 * 坐标点1 * @param point2 * 坐标点2 * @return */ public static boolean isInLine(Point point1, Point point2) { return (point1.getX() == point2.getX() && point1.getY() != point2.getY()) || (point1.getY() == point2.getY() && point1.getX() != point2.getX()); } /** * 获取两个点之间距离的平方值 * * @param point1 * 坐标点1 * @param point2 * 坐标点2 * @return */ public static double getDistanceSquare(Point point1, Point point2) { return Math.pow(point1.getX() - point2.getX(), 2) + Math.pow(point1.getY() - point2.getY(), 2); } /** * 是同一个点 * * @param point1 * 坐标点1 * @param point2 * 坐标点2 * @return */ public static boolean isSamePoint(Point point1, Point point2) { return point1.equals(point2); } public static void main(String[] args) { Point p1 = new Point(3, 5); Point p2 = new Point(1, 2); getBetweenPoint(p1, p2, true, true).forEach((p) -> System.out.println("(" + p.getX() + "," + p.getY() + ")")); System.out.println(getDistanceSquare(p1, p2)); System.out.println(5D); Point from = new Point(3, 7); Point to = new Point(3, 2); Line line = getBetweenLine(from, to, true, true); System.out.println(line); } }

最后

以上就是可爱小土豆最近收集整理的关于二维坐标工具API前言工具方法和类的全部内容,更多相关二维坐标工具API前言工具方法和类内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(89)

评论列表共有 0 条评论

立即
投稿
返回
顶部