左上隅の座標と、大きさを指定します。
public Rectangle (System.Drawing.Point location, System.Drawing.Size size);
public Rectangle (int x, int y, int width, int height);Rectangle コンストラクター (System.Drawing) | Microsoft Learn
Rectangle r1 = new Rectangle(1, 2, 3, 4); // {X = 1 Y = 2 Width = 3 Height = 4}
RectangleFからは、下表の静的メソッドから変換できます。
| メソッド | 機能 |
|---|---|
| Ceiling(RectangleF) | 値を切り上げる |
| Round(RectangleF) | 値を丸める |
| Truncate(RectangleF) | 値を切り捨てる |
| 型 | プロパティ | 内容 |
|---|---|---|
| int | X | 左上隅のx座標 |
| int | Y | 左上隅のy座標 |
| int | Width | 幅 |
| int | Height | 高さ |
| Point | Location | 左上隅の座標。getではnew Point(X, Y)が返される Location - Rectangle.cs |
| Size | Size | サイズ。getではnew Size(Width, Height)が返される Size - Rectangle.cs |
| int | Left | 左上隅のx座標。Xから取得するのと同じ Left - Rectangle.cs |
| int | Top | 左上隅のy座標。Yから取得するのと同じ |
| int | Right | 右上隅のx座標。X+Width |
| bool | IsEmpty | X、Y、Width、Heightのすべてがゼロならばtrue IsEmpty - Rectangle.cs |
| メソッド | 機能 |
|---|---|
| Union(Rectangle, Rectangle) | 指定のRectangleが結合したRectangleを得られる |
| メソッド | 機能 |
|---|---|
| Inflate(Size) | このRectangleの大きさを、変更できる。中心を維持しながら、XとY方向それぞれに拡大される |
| Offset(Point) | このRectangleの位置を、変更できる |
| Contains(Point) | 指定位置が、このRectangleに含まれているか判定できる |
| Contains(Rectangle) | 指定のRectangleの全体が、このRectangleに含まれているか判定できる |
| Intersect(Rectangle) | このRectangleを、指定のRectangleとの交差部分に置き換えられる |
| IntersectsWith(Rectangle) | 指定のRectangleが、このRectangleと交差するか判定できる |
Rectangle rec = new Rectangle(0, 0, 100, 200); // {X = 0 Y = 0 Width = 100 Height = 200} rec.Inflate(100, 100); // {X = -100 Y = -100 Width = 300 Height = 400} rec.Offset(10, 10); // {X = -90 Y = -90 Width = 300 Height = 400}
中心を維持しながら、XとY方向それぞれに拡大できます。左上を維持しながら拡大するには、WidthやHeightを変更します。
public void Inflate (int width, int height);Inflate(Int32, Int32) - Rectangle.Inflate メソッド (System.Drawing) | Microsoft Learn
Rectangle r1 = new Rectangle(-20, -10, 40, 20); // {X = -20 Y = -10 Width = 40 Height = 20} r1.Inflate(10, 20); // {X = -30 Y = -30 Width = 60 Height = 60} Rectangle r2 = new Rectangle(0, 0, 20, 10); // {X = 0 Y = 0 Width = 20 Height = 10} r2.Inflate(10, 20); // {X = -10 Y = -20 Width = 40 Height = 50}
このメソッドは次のように実装されています。
public void Inflate(int width, int height) {
this.X -= width;
this.Y -= height;
this.Width += 2*width;
this.Height += 2*height;
}
Inflate - Rectangle.cs
位置を変更できます。
Rectangle r1 = new Rectangle(-20, -10, 40, 20); // {X = -20 Y = -10 Width = 40 Height = 20} r1.Offset(10, 20); // {X = -10 Y = 10 Width = 40 Height = 20} Rectangle r2 = new Rectangle(0, 0, 20, 10); // {X = 0 Y = 0 Width = 20 Height = 10} r2.Offset(10, 20); // {X = 10 Y = 20 Width = 20 Height = 10}
このメソッドは次のように実装されています。
public void Offset(int x, int y) {
this.X += x;
this.Y += y;
}
Offset - Rectangle.cs
このRectangleが、指定のRectangleと交差するか確認できます。
public bool IntersectsWith (System.Drawing.Rectangle rect);Rectangle.IntersectsWith(Rectangle) メソッド (System.Drawing) | Microsoft Learn
このメソッドは次のように実装されており、指定のRectangleに内包されていたり、Rectangleを内包している場合も交差していると判定されます。
public bool IntersectsWith(Rectangle rect) {
return(rect.X < this.X + this.Width) &&
(this.X < (rect.X + rect.Width)) &&
(rect.Y < this.Y + this.Height) &&
(this.Y < rect.Y + rect.Height);
}
IntersectsWith - Rectangle.cs
Rectangle r = new Rectangle(10, 10, 20, 20); bool b1 = r.IntersectsWith(new Rectangle(15, 0, 10, 40)); // true bool b2 = r.IntersectsWith(new Rectangle(0, 0, 5, 5)); // false bool b3 = r.IntersectsWith(new Rectangle(0, 0, 40, 40)); // true bool b4 = r.IntersectsWith(new Rectangle(15, 15, 10, 10)); // true
2つのRectangleが結合 (union) したRectangleを取得できます。
public static System.Drawing.Rectangle Union (
System.Drawing.Rectangle a,
System.Drawing.Rectangle b
);
Rectangle.Union(Rectangle, Rectangle) メソッド (System.Drawing) | Microsoft Learn
Rectangle r1 = new Rectangle(10, 0, 10, 30); Rectangle r2 = new Rectangle(0, 10, 30, 10); Rectangle r3 = Rectangle.Union(r1, r2); // "{X=0,Y=0,Width=30,Height=30}" Rectangle r4 = Rectangle.Union(r1, Rectangle.Empty); // "{X=0,Y=0,Width=20,Height=30}"
このメソッドは次のように実装されています。
public static Rectangle Union(Rectangle a, Rectangle b) {
int x1 = Math.Min(a.X, b.X);
int x2 = Math.Max(a.X + a.Width, b.X + b.Width);
int y1 = Math.Min(a.Y, b.Y);
int y2 = Math.Max(a.Y + a.Height, b.Y + b.Height);
return new Rectangle(x1, y1, x2 - x1, y2 - y1);
}
Union - Rectangle.cs