2012-07-18 53 views
-2

我一直在寻找解决方案来解决我的问题,但没有发现任何我完全可以理解的东西,随时链接到解决方案。C - 计算两个矩形之间的面积

我要计算在x,矩形这是其它两个2D的交叉点,正常矩形的y坐标:

(x0,y0)  
    +------------+ 
    |   | 
    | (x4,y4) | 
    |  +----------+ 
    |  | |  | 
    +-------|----+  | 
      | (x2,y2) | 
      |   | 
      +----------+ 
        (x5,y5) 

基本上我只需要负责协调有关交点矩形。我将在C中实现这一点,但答案可以是伪代码。

由于

编辑:什么我期待的是找到任何两个2D,正常的矩形之间的交叉矩形的算法,而不是为单独示例的溶液上方

+1

似乎功课... – DGomez 2012-07-18 20:44:05

+1

请张贴它是什么,你发现,你不明白其他的解决方案。 – 2012-07-18 20:56:39

回答

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)则不存在交集。

+0

谢谢。应该在问题中提到这个问题,但解决方案必须适用于两个2d常规矩形之间的任何可能的交集。我想首先要做的是找出rect2中的哪些点在rect1中? – conor 2012-07-18 20:47:37

+1

@conor:我发布的解决方案有什么问题?你甚至尝试过吗?你能举一个它失败的例子吗? – 2012-07-18 20:48:56

+0

感谢我现在得到它。知道这将是简单的.. – conor 2012-07-18 20:57:45

0
+0

请阅读我的文章的第一行。我已经看到你提供的所有链接。 我知道矩形实际上相交,我已经想出了这个部分。我需要的是算出两者之间相交矩形的算法 - 这应该适用于任何两个相交的矩形,而不仅仅是示例 – conor 2012-07-18 20:52:32

+0

@conor,请在第一个链接上查看stakx的答案。 – 2012-07-18 20:54:47

0

两个坐标是已知的,因为它们属于每个交叉三角形。

其他两个可以通过使用两侧的交集算法找到。首先找到相交边的线方程,然后用它们找出两条线的交点。

0

这是一个Java程序。 这里,一个矩形由任意两个角点(p1和p2)构成。

您可以验证矩形 您可以检查他们是否有公共区域的矩形

你可以得到相交矩形,面积(JAVA)。

package com.prb.problemSolvingSkill;

import java.util。阵列;

公共类矩形{

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"); 

} 

}