這個狀態(tài)與之前關(guān)于圓的示例的狀態(tài)很類似。記住,要使它作為默認(rèn)狀態(tài)加載。矩形本身和圓一樣,是使用線循環(huán)構(gòu)成的。
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類可以創(chuàng)建和繪制矩形。唯一還缺少的函數(shù)是最重要的intersect函數(shù)。
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;
}
運行程序,將光標(biāo)移入矩形。與圓的示例一樣,矩形會變成紅色,表明相交性代碼起作用了。