WisdomSoft - for your serial experiences.

点と長方形

平面上の点は Point 構造体、長方形領域は Rectangle 構造体で表されます。

2次元座標の点

2 つの float 型の要素を持つ値は Vector2 構造体で表現できますが、ほぼ同様の型で 2 次元空間の点を表す Microsoft.Xna.Framework.Point 構造体が用意されています。Vector2 構造体と同様に X 要素と Y 要素を持ちますが、float 型ではなく int 型です。

Microsoft.Xna.Framework.Point 構造体
[TypeConverterAttribute("typeof(Microsoft.Xna.Framework.Design.PointConverter)")]
[SerializableAttribute]
public struct Point : IEquatable<Point>

この構造体のコンストラクタはオーバーロードされており、各要素を初期化する値を指定できます。

Point 構造体のコンストラクタ
public Point (int x, int y)

x パラメータには X 要素の値を、y パラメータには Y 要素の値を指定します。

コード1
using System.Diagnostics;
using Microsoft.Xna.Framework;

public class TestGame : Game
{
    public static void Main(string[] args)
    {
        Point pt1 = new Point();
        Point pt2 = new Point(10, 20);

        Debug.WriteLine("pt1=" + pt1);
        Debug.WriteLine("pt2=" + pt2);
    }
}
実行結果
コード1 実行結果

コード1は Point 構造体の値を生成し、それを出力しています。

X 要素の値は X フィールドから、Y 要素の値は Y フィールドから設定または取得できます。

Point 構造体 X フィールド
public int X
Point 構造体 Y フィールド
public int Y

これらのフィールドの型が int である点が Vector2 構造体と違っています。ベクトル演算を行う必要がなく、単にピクセルに対応する点の位置を表現したい場合は Point 構造体を使うとよいでしょう。

コード2
using System.Diagnostics;
using Microsoft.Xna.Framework;

public class TestGame : Game
{
    public static void Main(string[] args)
    {
        Point pt;
        pt.X = 10;
        pt.Y = 20;
        Debug.WriteLine("X=" + pt.X + ", Y=" + pt.Y);
    }
}
実行結果
コード2 実行結果

このように、基本的な性質は Vector2 構造体と同じです。

長方形領域

平面上の長方形は、長方形の左上隅の座標と、幅と高さで範囲が表されます。位置とサイズからなる長方形領域は Microsoft.Xna.Framework.Rectangle 構造体で表されます。

Microsoft.Xna.Framework.Rectangle 構造体
[TypeConverterAttribute("typeof(Microsoft.Xna.Framework.Design.RectangleConverter)")]
[SerializableAttribute]
public struct Rectangle : IEquatable<Rectangle>

この構造体のコンストラクタはオーバーロードされています。コンストラクタで配置とサイズの初期値を指定できます。

Rectangle 構造体のコンストラクタ
public Rectangle (int x, int y, int width, int height)

x パラメータには長方形の開始点となる左上隅の X 座標を、y パラメータには Y 座標を指定します。width パラメータは開始点からの幅を、height パラメータには高さを指定します。

コード3
using System.Diagnostics;
using Microsoft.Xna.Framework;

public class TestGame : Game
{
    public static void Main(string[] args)
    {
        Rectangle rect1 = new Rectangle();
        Rectangle rect2 = new Rectangle(30, 50, 400, 300);
        Debug.WriteLine("rect1=" + rect1);
        Debug.WriteLine("rect2=" + rect2);
    }
}
実行結果
コード3 実行結果

コード3は Rectangle 構造体の値を生成し、出力しています。コンストラクタに渡した値が、正しく設定されています。

長方形の位置やサイズはフィールドで公開されています。長方形の左辺の位置は X フィールド、上辺の位置は Y フィールドで設定または取得できます。同様に、幅は Width フィールド、高さは Height フィールドで設定または取得できます。

Rectangle 構造体 X フィールド
public int X
Rectangle 構造体 Y フィールド
public int Y
Rectangle 構造体 Width フィールド
public int Width
Rectangle 構造体 Height フィールド
public int Height

これらのフィールドによって長方形の各辺の位置を計算できますが、Rectangle 構造体は各辺の位置を返すプロパティも用意しています。

長方形の左端は Left プロパティ、上端は Top プロパティ、右端は Right プロパティ、下端は Bottom プロパティから取得できます。これらのプロパティは読み取り専用です。

事実上 Left プロパティは X フィールドに、Top プロパティは Y フィールドに等しい値ですが、コードの表現として開始点の座標を取りたいのか、端の座標を取りたいのか、といった意味の違いで表記を分けることができます。

Right プロパティは長方形の X 座標に幅を足した値(X + Width)、Bottom プロパティは Y 座標に高さを足した値(Y + Height)が返されます。

コード4
using System.Diagnostics;
using Microsoft.Xna.Framework;

public class TestGame : Game
{
    public static void Main(string[] args)
    {
        Rectangle rect = new Rectangle();
        rect.X = 10;
        rect.Y = 20;
        rect.Width = 400;
        rect.Height = 300;

        Debug.WriteLine("X=" + rect.X + ", Y=" + rect.Y);
        Debug.WriteLine("Width=" + rect.Width + ", Height=" + rect.Height);

        Debug.WriteLine("Left=" + rect.Left + ", Right=" + rect.Top);
        Debug.WriteLine("Right=" + rect.Right + ", Bottom=" + rect.Bottom);
    }
}
実行結果
コード4 実行結果

コード4は Rectangle 構造体の位置とサイズを各フィールドから設定しています。その後、フィールドとプロパティの値をそれぞれ出力しています。