我一直在寻找解决问题的方法,但没有发现任何我能完全理解的内容,请随时链接到解决方案。
我想计算一个矩形的x,y坐标,它是另外两个2D正常矩形的交点:
(x0,y0)
+------------+
| |
| (x4,y4) |
| +----------+
| | | |
+-------|----+ |
| (x2,y2) |
| |
+----------+
(x5,y5)
基本上我只需要交叉矩形的坐标。我将在C中实现它,但答案可能是伪代码。
由于
编辑:我正在寻找的是一种算法,可以找到任意两个2d正常矩形之间的交点矩形,而不仅仅是上述示例的解决方案
答案 0 :(得分:4)
左上角的坐标由:(max(x4, x0), max(y4, y0))
给出。
右下角的坐标由:(min(x2, x5), min(y2, y5))
给出。
如果max(x4, x0) > min(x2, x5)
或max(y4,y0) > min(y2, y5)
则没有交集。
答案 1 :(得分:0)
感觉这很长,以发表评论:你试过使用谷歌吗?有很多可用的链接(很多来自stackoverflow):
How to get overlapping rectangle coordinates
What is an Efficient algorithm to find Area of Overlapping Rectangles
Calculate overlap between two rectangles on x/y grid?
http://www.leetcode.com/2011/05/determine-if-two-rectangles-overlap.html
如果你有糟糕的google-fu:
http://www.google.com/search?q=find+coordinates+for+overlapping+rectangle
答案 2 :(得分:0)
两个坐标已知,因为它们属于每个相交的三角形。
通过对双方使用交叉算法可以找到另外两个。首先找到相交边的线方程,然后用它们找到两条线的交点。
答案 3 :(得分:0)
这是一个Java程序。 这里,矩形由任意两个角点(p1和p2)构成。
您可以验证矩形 您可以检查它们是否具有公共区域矩形
package com.prb.problemSolvingSkill;
import java.util.Arrays;
public class Rectangle {
public class Point {
/*
* This is a 2D point with coordinate (x,y)
*/
double x;
double y;
Point() {
this.x = 0;
this.y = 0;
}
Point(double x, double y) {
this.x = x;
this.y = y;
}
public String show() {
return "( " + x + " , " + y + " )";
}
public boolean isEqual(Point p) {
return this.x == p.x && this.y == p.y;
}
}
/**
* Rectangle is constructed by any two corner points p1 and p2
*/
Point p1, p2;
public Rectangle() {
this.p1 = new Point();
this.p2 = new Point();
}
public Rectangle(double x1, double y1, double x2, double y2) {
this.p1 = new Point(x1, y1);
this.p2 = new Point(x2, y2);
}
public Rectangle(Point p1, Point p2) {
this.p1 = p1;
this.p2 = p2;
}
public void show() {
System.out.println("---------- " + this + " ------------");
System.out.println("Point p1 is : " + p1.show());
System.out.println("Point p2 is : " + p2.show());
}
public boolean validate() {
if (this.p1.x != this.p2.x && this.p1.y != this.p2.y)
return true;
else
return false;
}
public double getArea() {
double height = Math.abs(p1.y - p2.y);
double width = Math.abs(p1.x - p2.x);
return height * width;
}
/**
* This is like a utility method
*
* @param rect1
* @param rect2
* @return
*/
public static Rectangle getIntersectedRectangle(Rectangle rect1,
Rectangle rect2) {
if (!hasCommonArea(rect1, rect2))
return null;
/*
* If Common area exists then find Rectangle
*
* Two x-coordinate of intersected rectangle will be middle two
* x-coordinate of four x-coordinates
*/
double[] dXArr = new double[] { rect1.p1.x, rect1.p2.x, rect2.p1.x,
rect2.p2.x };
double[] dYArr = new double[] { rect1.p1.y, rect1.p2.y, rect2.p1.y,
rect2.p2.y };
Arrays.sort(dXArr);
Arrays.sort(dYArr);
Rectangle inRect = new Rectangle(dXArr[1], dYArr[1], dXArr[2], dYArr[2]);
inRect.show();
return inRect;
}
/**
* This is like a utility method
*
* @param rect1
* @param rect2
* @return
*/
public static boolean hasCommonArea(Rectangle rect1, Rectangle rect2) {
boolean flag1 = true, flag2 = true;
if ((Math.min(rect1.p1.x, rect1.p2.x) >= Math.max(rect2.p1.x,
rect2.p2.x))
|| (Math.max(rect1.p2.x, rect1.p2.x) <= Math.min(rect2.p1.x,
rect2.p2.x))) {
flag1 = false;
}
if ((Math.min(rect1.p1.y, rect1.p2.y) >= Math.max(rect2.p1.y,
rect2.p2.y))
|| (Math.max(rect1.p2.y, rect1.p2.y) <= Math.min(rect2.p1.y,
rect2.p2.y))) {
flag2 = false;
}
if (!(flag1 && flag2))
System.out.println("Common Area doesnot exist");
// System.out.println("flag1 :x " + flag1 + " flag2 :y " + flag2);
return flag1 && flag2;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Rectangle rect1 = new Rectangle(1, 1, 6, 6);
Rectangle rect2 = new Rectangle(1, 16, 6, 20);
if (null != getIntersectedRectangle(rect1, rect2))
System.out.println("Area is : "
+ getIntersectedRectangle(rect1, rect2).getArea()
+ " sq unit");
}
}