在直角坐标系中有两个矩形型,其左下角与右上角坐标分别为(X1,X2)(X3,X4),另一矩形的左下角与右上角坐标分别为(X5,X6)(X7,X8),以这8个数为参数,实现一个函数,如果两矩形相交,返回相交的面积,否则返回0。我的方法特笨,就是用6个if判断是否相交,我假设矩形为A和B,那么相交有4种情况,包含有2种情况,请问有什么好的方法吗?

解决方案 »

  1.   

    我学会看MSDN           全部折叠全部展开        代码:全部 代码:多个 代码:Visual Basic 代码:C# 代码:Visual C++ 代码:J# 代码:JScript   
     Visual Basic 
     C# 
     Visual C++ 
     J# 
     JScript 
    .NET Framework 类库  
    Rect..::.Intersect 方法 (Rect)  
    Rect 结构  示例  请参见  发送反馈意见 
     查找当前矩形和指定矩形的交集,并将结果存储为当前矩形。命名空间:  System.Windows
    程序集:  WindowsBase(在 WindowsBase.dll 中)语法
    Visual Basic(声明) 
    Public Sub Intersect ( _
    rect As Rect _
    )
     
    Visual Basic(用法) 
    Dim instance As Rect
    Dim rect As Rectinstance.Intersect(rect)
     
    C# 
    public void Intersect(
    Rect rect
    )
     
    Visual C++ 
    public:
    void Intersect(
    Rect rect
    )
     
    J# 
    public void Intersect(
    Rect rect
    )
     
    JScript 
    public function Intersect(
    rect : Rect
    )
     
    XAML 
    不能在 XAML 中使用方法。
     参数
    rect
    类型:System.Windows..::.Rect要与当前矩形相交的矩形。备注
    如果不存在任何交集,则当前矩形将变为 Rect..::.Empty。示例
    下面的示例演示如何使用 Intersect(Rect) 方法查找两个矩形的交集并将结果存储为矩形。C#  复制代码 
    private Rect intersectExample1()
    {
        // Initialize new rectangle.
        Rect myRectangle = new Rect();    // The Location property specifies the coordinates of the upper left-hand 
        // corner of the rectangle. 
        myRectangle.Location = new Point(10, 5);    // Set the Size property of the rectangle with a width of 200
        // and a height of 50.
        myRectangle.Size = new Size(200, 50);    // Create second rectangle to compare to the first.
        Rect myRectangle2 = new Rect();
        myRectangle2.Location = new Point(0, 0);
        myRectangle2.Size = new Size(200, 50);    // Intersect method finds the intersection between the current rectangle and the 
        // specified rectangle, and stores the result as the current rectangle. If no 
        // intersection exists, the current rectangle becomes the Empty rectangle. 
        // myRectangle now has a size of 190,45 and location of 10,5. 
        myRectangle.Intersect(myRectangle2);    // myRectangle has been changed into the intersection area between the old myRectangle
        // and myRectangle2 (new size of 190,45 and new location of 10,5).
        return myRectangle;}
     平台
    Windows Vista
    .NET Framework 和 .NET Compact Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。 版本信息
    .NET Framework
    受以下版本支持:3.5、3.0 SP1、3.0请参见
    参考
    Rect 结构
    Rect 成员
    Intersect 重载
    System.Windows 命名空间
    IntersectsWith
     发送反馈意见,就此主题向 Microsoft 发送反馈意见。
      

  2.   

    LZ认真看一下MSDN吧using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Forms;
    using System.Drawing;
    namespace ConsoleApplication39
    {
        class Program
        {
            static void Main(string[] args)
            {  Program ps=new Program();
                Rectangle rect = ps.intersectExample1();
                Console.WriteLine("{0}-{1}-{2}-{3}", rect.X, rect.Y, rect.Width, rect.Height);
            }
            private Rectangle  intersectExample1()
            {  
               
                // Initialize new rectangle.
                Rectangle myRectangle = new Rectangle();
                
                // The Location property specifies the coordinates of the upper left-hand 
                // corner of the rectangle. 
                myRectangle.Location = new Point(10, 5);            // Set the Size property of the rectangle with a width of 200
                // and a height of 50.
                myRectangle.Size = new Size(200, 50);            // Create second rectangle to compare to the first.
                Rectangle myRectangle2 = new Rectangle();
                myRectangle2.Location = new Point(0, 0);
                myRectangle2.Size = new Size(200, 50);            // Intersect method finds the intersection between the current rectangle and the 
                // specified rectangle, and stores the result as the current rectangle. If no 
                // intersection exists, the current rectangle becomes the Empty rectangle. 
                // myRectangle now has a size of 190,45 and location of 10,5. 
                myRectangle.Intersect(myRectangle2);            // myRectangle has been changed into the intersection area between the old myRectangle
                // and myRectangle2 (new size of 190,45 and new location of 10,5).
                return myRectangle;        }    }
    }
    就是求交集而已。不用我再说了吧
      

  3.   

    谢谢hhc123兄,用Rectangle 类能实现功能,我想问问的是用普通的算法怎么实现,因为是一道笔试题,呵呵
      

  4.   

    Rect A = new Rect(PointStart1,PointEnd1);
    Rect B = new Rect(PointStart2,PointEnd2);Point PointUnionStart = new Point(Math.Max(PointStart1.X,PointStart2.X),Math.Max(PointStart1.Y,PointStart2.Y);Point PointUnionEnd = new Point(Math.Min(PointEnd1.X,PointEnd2.X),Math.Min(PointEnd1.Y,PointEnd2.Y);Rect RectUnion = new Rect(PointUnionStart ,PointUnionEnd );
    这样应该就可以了吧....
      

  5.   

    adrianEvin兄的情况只适用与这一种情况啊,如果两个矩形只是各各角相交成一个矩形,那就不行了啊,比如坐标为(0,0)(2,1)的矩形和(1,0)(4,2)这个矩形相交就不行了
      

  6.   

    我习惯用Rectangle
    完全是数学问题,自己出那几种情况吧,坐标多有了用if就if吧
      

  7.   

    因为是平衡坐标系统的,所以可以先算线段 X1-X3,X5-X7 的重合部分,再算X2-X4,X6-X8的重合部分,如果都有重合,那相乘就是相交的面积了,否则返回0
      

  8.   

    Rectangle.Intersect(rect1, rect2);