【问题标题】:C - Calculate area between two rectanglesC - 计算两个矩形之间的面积
【发布时间】:2012-07-18 20:40:53
【问题描述】:

我一直在寻找我的问题的解决方案,但没有找到任何我能完全理解的解决方案,请随时链接到解决方案。

我想计算一个矩形的 x,y 坐标,该矩形是另外两个 2D 普通矩形的交集:

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

基本上我只需要相交矩形的坐标。我将在 C 中实现这一点,但答案可以是伪代码。

谢谢

编辑:我正在寻找的是一种算法来找到任何两个二维正常矩形之间的相交矩形,而不是仅针对上述示例的解决方案

【问题讨论】:

  • 请发表您发现不理解的其他解决方案。

标签: c graphics


【解决方案1】:

左上角的坐标为:(max(x4, x0), max(y4, y0))。

右下角的坐标为:(min(x2, x5), min(y2, y5))。

如果max(x4, x0) > min(x2, x5) 或max(y4,y0) > min(y2, y5) 则没有交集。

【讨论】:

  • 谢谢。应该在问题中提到这一点,但解决方案必须适用于两个二维正常矩形之间的任何可能的交集。我想首先要做的是找出 rect2 中的哪些点在 rect1 中?
  • @conor:我发布的解决方案有什么问题?你试过了吗?你能举一个失败的例子吗?
【解决方案2】:

【讨论】:

  • 请阅读我帖子的第一行。我已经看到了您提供的所有链接。我确实知道矩形实际上相交,那部分我已经弄清楚了。我需要的是一种算法来确定两者之间的相交矩形 - 这应该适用于任何两个相交的矩形,而不仅仅是我展示的示例
  • @conor,在第一个链接上查看 stakx 的答案。
【解决方案3】:

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

另外两个可以通过两侧的交集算法找到。先求相交边的直线方程,然后用它们求两条直线的交点。

【讨论】:

    【解决方案4】:

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

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

    您可以获得相交矩形及其区域(Java)。

    包 com.prb.problemSolvingSkill;

    导入 java.util.Arrays;

    公共类矩形{

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

    }

    【讨论】:

      猜你喜欢
      • 2015-01-25
      • 2018-09-28
      • 2021-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-16
      • 1970-01-01
      相关资源
      最近更新 更多