Formに塗りつぶされた多角形(FillPolygon)を描画する
Graphics.FillPolygon メソッド を使ってみます。
Pointの配列を作成し、そのPointとPointを直線で結び、多角形(ポリゴン)の内部を塗りつぶします。
1)Windowsアプリケーションから起動してForm1を用意します。
2)プロパティのイベントボタンをクリック。
3)プロパティの右側のScrollBarを下に動かして、Paintをダブルクリック。
4)下記を入力します。
int sx = 40;
int sy = 30;
int x = 35;
int y = 20;
int x0 = sx;
int x1 = sx + x;
int x2 = sx + x * 2;
int x3 = sx + x * 3;
int x4 = sx + x * 4;
int x5 = sx + x * 5;
int x6 = sx + x * 6;
int y0 = sy;
int y1 = sy + y;
int y3 = sy + y * 3;
int y4 = sy + y * 4;
int y6 = sy + y * 6;
int y7 = sy + y * 7;
int y9 = sy + y * 9;
int y10 = sy + y * 10;
// graphics
Graphics g = e.Graphics;
// pointの配列
Point[] p1 = new Point[6];
p1[0] = new Point(x2, y0);
p1[1] = new Point(x1, y1);
p1[2] = new Point(x1, y3);
p1[3] = new Point(x2, y4);
p1[4] = new Point(x3, y3);
p1[5] = new Point(x3, y1);
Point[] p2 = new Point[6];
p2[0] = new Point(x4, y0);
p2[1] = new Point(x3, y1);
p2[2] = new Point(x3, y3);
p2[3] = new Point(x4, y4);
p2[4] = new Point(x5, y3);
p2[5] = new Point(x5, y1);
Point[] p3 = new Point[6];
p3[0] = new Point(x1, y3);
p3[1] = new Point(x0, y4);
p3[2] = new Point(x0, y6);
p3[3] = new Point(x1, y7);
p3[4] = new Point(x2, y6);
p3[5] = new Point(x2, y4);
Point[] p4 = new Point[6];
p4[0] = new Point(x3, y3);
p4[1] = new Point(x2, y4);
p4[2] = new Point(x2, y6);
p4[3] = new Point(x3, y7);
p4[4] = new Point(x4, y6);
p4[5] = new Point(x4, y4);
Point[] p5 = new Point[6];
p5[0] = new Point(x5, y3);
p5[1] = new Point(x4, y4);
p5[2] = new Point(x4, y6);
p5[3] = new Point(x5, y7);
p5[4] = new Point(x6, y6);
p5[5] = new Point(x6, y4);
Point[] p6 = new Point[6];
p6[0] = new Point(x2, y6);
p6[1] = new Point(x1, y7);
p6[2] = new Point(x1, y9);
p6[3] = new Point(x2, y10);
p6[4] = new Point(x3, y9);
p6[5] = new Point(x3, y7);
Point[] p7 = new Point[6];
p7[0] = new Point(x4, y6);
p7[1] = new Point(x3, y7);
p7[2] = new Point(x3, y9);
p7[3] = new Point(x4, y10);
p7[4] = new Point(x5, y9);
p7[5] = new Point(x5, y7);
// FillPolygon(近似正6角形)
g.FillPolygon(Brushes.Violet, p1);
g.FillPolygon(Brushes.Blue, p2);
g.FillPolygon(Brushes.Green, p3);
g.FillPolygon(Brushes.Yellow, p4);
g.FillPolygon(Brushes.Orange, p5);
g.FillPolygon(Brushes.Cyan, p6);
g.FillPolygon(Brushes.Red, p7);
// GraphicをDispose
g.Dispose();
5)デバッグ開始(F5)します。
Pointの位置関係は下図の通りです。
近似正6角形については、前回を参照してください。
Pointの配列を作成し、そのPointとPointを直線で結び、多角形(ポリゴン)の内部を塗りつぶします。
1)Windowsアプリケーションから起動してForm1を用意します。
2)プロパティのイベントボタンをクリック。
3)プロパティの右側のScrollBarを下に動かして、Paintをダブルクリック。
4)下記を入力します。
int sx = 40;
int sy = 30;
int x = 35;
int y = 20;
int x0 = sx;
int x1 = sx + x;
int x2 = sx + x * 2;
int x3 = sx + x * 3;
int x4 = sx + x * 4;
int x5 = sx + x * 5;
int x6 = sx + x * 6;
int y0 = sy;
int y1 = sy + y;
int y3 = sy + y * 3;
int y4 = sy + y * 4;
int y6 = sy + y * 6;
int y7 = sy + y * 7;
int y9 = sy + y * 9;
int y10 = sy + y * 10;
// graphics
Graphics g = e.Graphics;
// pointの配列
Point[] p1 = new Point[6];
p1[0] = new Point(x2, y0);
p1[1] = new Point(x1, y1);
p1[2] = new Point(x1, y3);
p1[3] = new Point(x2, y4);
p1[4] = new Point(x3, y3);
p1[5] = new Point(x3, y1);
Point[] p2 = new Point[6];
p2[0] = new Point(x4, y0);
p2[1] = new Point(x3, y1);
p2[2] = new Point(x3, y3);
p2[3] = new Point(x4, y4);
p2[4] = new Point(x5, y3);
p2[5] = new Point(x5, y1);
Point[] p3 = new Point[6];
p3[0] = new Point(x1, y3);
p3[1] = new Point(x0, y4);
p3[2] = new Point(x0, y6);
p3[3] = new Point(x1, y7);
p3[4] = new Point(x2, y6);
p3[5] = new Point(x2, y4);
Point[] p4 = new Point[6];
p4[0] = new Point(x3, y3);
p4[1] = new Point(x2, y4);
p4[2] = new Point(x2, y6);
p4[3] = new Point(x3, y7);
p4[4] = new Point(x4, y6);
p4[5] = new Point(x4, y4);
Point[] p5 = new Point[6];
p5[0] = new Point(x5, y3);
p5[1] = new Point(x4, y4);
p5[2] = new Point(x4, y6);
p5[3] = new Point(x5, y7);
p5[4] = new Point(x6, y6);
p5[5] = new Point(x6, y4);
Point[] p6 = new Point[6];
p6[0] = new Point(x2, y6);
p6[1] = new Point(x1, y7);
p6[2] = new Point(x1, y9);
p6[3] = new Point(x2, y10);
p6[4] = new Point(x3, y9);
p6[5] = new Point(x3, y7);
Point[] p7 = new Point[6];
p7[0] = new Point(x4, y6);
p7[1] = new Point(x3, y7);
p7[2] = new Point(x3, y9);
p7[3] = new Point(x4, y10);
p7[4] = new Point(x5, y9);
p7[5] = new Point(x5, y7);
// FillPolygon(近似正6角形)
g.FillPolygon(Brushes.Violet, p1);
g.FillPolygon(Brushes.Blue, p2);
g.FillPolygon(Brushes.Green, p3);
g.FillPolygon(Brushes.Yellow, p4);
g.FillPolygon(Brushes.Orange, p5);
g.FillPolygon(Brushes.Cyan, p6);
g.FillPolygon(Brushes.Red, p7);
// GraphicをDispose
g.Dispose();
5)デバッグ開始(F5)します。
Pointの位置関係は下図の通りです。
近似正6角形については、前回を参照してください。