在直角坐标系中有两个矩形型,其左下角与右上角坐标分别为(X1,X2)(X3,X4),另一矩形的左下角与右上角坐标分别为(X5,X6)(X7,X8),以这8个数为参数,实现一个函数,如果两矩形相交,返回相交的面积,否则返回0。我的方法特笨,就是用6个if判断是否相交,我假设矩形为A和B,那么相交有4种情况,包含有2种情况,请问有什么好的方法吗?
解决方案 »
- 求用try-catch写个输入一定要是数字的程序
- ((Shape)shape).Position = p; 这句话是什么意思,() 在这里起什么作用
- 打印问题
- web service不能被win2003 server版访问
- 水晶报表的入门问题,抢分哈
- 如何将自己写的Windows服务在我的电脑->管理->服务和应用程序下面显示?
- 我建立了一个WINFORM项目,在使用System.Management时,为什么编译器找不到???
- 问一个关于vb 6.0到vc#的一个字符串声明(在线等.急.)
- 嵌套repeater控件 查找 内层repeater控件里面的服务端控件为空
- C#写的入库解压程序CPU占用率很低
- C#从数据库读取图片
- winform datagridview中,单元焦点问题
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 发送反馈意见。
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; } }
}
就是求交集而已。不用我再说了吧
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 );
这样应该就可以了吧....
完全是数学问题,自己出那几种情况吧,坐标多有了用if就if吧