gpt4 book ai didi

c# - 检查特定点 x,y 是否在矩形内

转载 作者:行者123 更新时间:2023-11-30 22:08:17 42 4
gpt4 key购买 nike

我正在尝试编写程序,通过矩形的宽度和高度以及它的上角点和左角点指定一个矩形。我希望程序允许用户输入点 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);
}
}

最佳答案

我之前没有使用过 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 的文档时,请注意左侧有几个指向不同使用点的方法的链接。

关于c# - 检查特定点 x,y 是否在矩形内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22424155/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com