当前位置:网站首页>二维坐标工具API
二维坐标工具API
2022-07-31 07:23:00 【逆水行舟没有退路】
前言
处理平面的二维坐标系相关的计算,例如象棋的棋盘就是典型的二维坐标。
工具方法和类
坐标分类
/** * 坐标线x或者y轴 * * @author leng * */
public enum E_XYConst {
X, Y;
}
坐标点
/** * 坐标点 * * @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();
}
}
线段
/** * 线段,用于描述一段连续的点<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;
}
}
坐标系的工具方法
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);
}
}
边栏推荐
猜你喜欢
随机推荐
van-uploader uploads images, and cannot preview the image using base64 echo
2022.07.13 _ a day
Titanic 预测问题
7/28-7/29 期望+思维+后缀数组+ST表
2022.07.14_Daily Question
Ceph单节点部署
mysql的建表语句_三种常用的MySQL建表语句
NK-RTU980烧写裸机程序
MySQL detailed explanation
Vscode: Project-tree plugin
2022.07.15_每日一题
2022.07.20_每日一题
Thread 类的基本用法——一网打尽
波士顿房价数据集 Boston house prices dataset
[Interview: Concurrency 37: Multithreading: Thread Pool] Custom Thread Pool
XSS详解
MySQL table creation statement_Three commonly used MySQL table creation statements
2022.07.18_每日一题
一文读懂Elephant Swap,为何为ePLATO带来如此高的溢价?
PHP中 比较 0、false、null,‘‘ “









