tsl*_*lid 4 c# geometry point rectangles
我正在尝试编写程序,通过其宽度和高度及其左上角点来指定矩形。我希望程序允许用户输入点 x,y,然后我的目标是让程序确定该点是否在矩形内部。
到目前为止,这是我的代码,但我不确定如何继续。谁能帮助我实施bool Rectangle.Contains(x, y)?
public struct Rectangle
{
// declare the fields
public int Width;
public int Height;
public int Top;
public int Left;
// define a constructor
public Rectangle(int Width, int Height, int Top, int Left)
{
this.Width = Width;
this.Height = Height;
this.Top = Top;
this.Left = Left;
}
public bool Contains(int x, int y) { }
}
class MainClass
{
public static void Main()
{
Console.WriteLine("Creating a Rectangle instance");
Rectangle myRectangle = new Rectangle(6, 2, 1, -1);
Console.WriteLine("myRectangle.Width = " + myRectangle.Width);
Console.WriteLine("myRectangle.Height = " + myRectangle.Height);
Console.WriteLine("myRectangle.Top = " + myRectangle.Top);
Console.WriteLine("myRectangle.Left = " + myRectangle.Left);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
我以前没有使用过这个System.Drawing.Rectangle类,但在我看来你可以使用这个Contains(Point)方法。以下是该文档的链接:http://msdn.microsoft.com/en-us/library/22t27w02 (v=vs.110).aspx
正如你所看到的,传递给的参数Contains()是类型Point。使用用户输入的 x,y 值创建该类型的变量并将其传递给该Contains()方法。这是结构的链接Point:http://msdn.microsoft.com/en-us/library/system.drawing.point (v=vs.110).aspx
当您查看 的文档时Point,请注意左侧有多个链接,指向使用点的不同方法。