概述
前言
处理平面的二维坐标系相关的计算,例如象棋的棋盘就是典型的二维坐标。
工具方法和类
坐标分类
/**
* 坐标线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);
}
}
最后
以上就是可爱小土豆为你收集整理的二维坐标工具API前言工具方法和类的全部内容,希望文章能够帮你解决二维坐标工具API前言工具方法和类所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复