这个状态与之前关于圆的示例的状态很类似。记住,要使它作为默认状态加载。矩形本身和圆一样,是使用线循环构成的。
public class Rectangle
{
Vector BottomLeft { get; set;}
Vector TopRight { get; set; }
Color _color = new Color(1, 1, 1, 1);
public Color Color
{
get { return _color; }
set { _color = value; }
}
public Rectangle(Vector bottomLeft, Vector topRight)
{
BottomLeft = bottomLeft;
TopRight = topRight;
}
public void Render()
{
Gl.glColor3f(_color.Red, _color.Green, _color.Blue);
Gl.glBegin(Gl.GL_LINE_LOOP);
{
Gl.glVertex2d(BottomLeft.X, BottomLeft.Y);
Gl.glVertex2d(BottomLeft.X, TopRight.Y);
Gl.glVertex2d(TopRight.X, TopRight.Y);
Gl.glVertex2d(TopRight.X, BottomLeft.Y);
}
Gl.glEnd();
}
}
rectangle类可以创建和绘制矩形。唯一还缺少的函数是最重要的intersect函数。
public bool Intersects(Point point)
{
if (
point.X >= BottomLeft.X &&
point.X <= TopRight.X &&
point.Y <= TopRight.Y &&
point.Y >= BottomLeft.Y)
{
return true;
}
return false;
}
运行程序,将光标移入矩形。与圆的示例一样,矩形会变成红色,表明相交性代码起作用了。