- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写程序,通过矩形的宽度和高度以及它的上角点和左角点指定一个矩形。我希望程序允许用户输入点 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/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 4 年前。
正如您在 this travis.yml 中看到的那样文件,我的代码依赖于一些第三方库,我在构建项目之前将它们安装在远程系统上。 Travis 每次推送提交时都会下载并构建这些库,这可以避免吗?我的意
我是一名优秀的程序员,十分优秀!