计算两个矩形之间的交点
Calculating the intersection POINTS between 2 rectangles
我遇到了一个问题,我需要找到 2 个矩形的交点。我知道这个问题已经被问过 here,但解决方案总是 returns 一个矩形,而我只需要 2 或 4 个交点
也许这种方法提供了您正在寻找的东西
public List<Point> getIntersects(Rectangle2D a, Rectangle2D b) {
if (!a.intersects(b)) return null;
List<Point> points = new ArrayList<Point>();
double ax = a.getX();
double ay = a.getY();
double aw = a.getWidth();
double ah = a.getHeight();
double bx = b.getX();
double by = b.getY();
double bw = b.getWidth();
double bh = b.getHeight();
if (ax <= bx) {
if (ay < by) {
points.add(new Point((int) (ax + aw), (int) by));
points.add(new Point((int) (bx), (int) (ay + ah)));
} else {
points.add(new Point((int) (ax + aw), (int) (by + bw)));
points.add(new Point((int) (bx), (int) (ay)));
}
} else return getIntersects(b, a);
return points;
}
我遇到了一个问题,我需要找到 2 个矩形的交点。我知道这个问题已经被问过 here,但解决方案总是 returns 一个矩形,而我只需要 2 或 4 个交点
也许这种方法提供了您正在寻找的东西
public List<Point> getIntersects(Rectangle2D a, Rectangle2D b) {
if (!a.intersects(b)) return null;
List<Point> points = new ArrayList<Point>();
double ax = a.getX();
double ay = a.getY();
double aw = a.getWidth();
double ah = a.getHeight();
double bx = b.getX();
double by = b.getY();
double bw = b.getWidth();
double bh = b.getHeight();
if (ax <= bx) {
if (ay < by) {
points.add(new Point((int) (ax + aw), (int) by));
points.add(new Point((int) (bx), (int) (ay + ah)));
} else {
points.add(new Point((int) (ax + aw), (int) (by + bw)));
points.add(new Point((int) (bx), (int) (ay)));
}
} else return getIntersects(b, a);
return points;
}