C#中級 コーディング例

G1)滑らかサインウェーブ
G2)単一色ペイント
   (スタック方式)
G3)ギザトゲ楕円
G4)ナミモク四角

C1)マウス移動可能パネル

この講座は中級者用です。
「using」「Form」等は、省略しています。


















C#中級 コーディング例

G3)ギザトゲ楕円



トゲトゲが付いた楕円を描く

 public void TogeOval()
{
      Bitmap imgBitmap = new Bitmap(600, 600);//pictureBoxのサイズ
      pictureBox.Image = imgBitmap;
      Graphics grf = Graphics.FromImage(pictureBox.Image);

      Pen pen = new Pen(Color.Red, 0.1f);
     //設定
      double a = 100.0;// 楕円の長軸半径
      double b = 50.0;// 楕円の短軸半径
      int numberOfProtrusions = 50;// V字型突起の数
      double protrusionHeight = 80.0;// 突起の高さ
      double angleIncrement = 2 * Math.PI / numberOfProtrusions;
      double protrusionX;
      double protrusionY;
      double angle;//分割角度
      double x;
      double y;
      Point startPoint = new Point(0, 0);
      Point referencePoint = new Point(0, 0);
      for (int i = 0; i < numberOfProtrusions + 1; i++)
    {
          angle = i * angleIncrement;
          x = a * Math.Cos(angle) + a + protrusionHeight;
          y = b * Math.Sin(angle) + b + protrusionHeight;
          startPoint.X = (int)(x - (x - referencePoint.X) / 2);
          startPoint.Y = (int)(y - (y - referencePoint.Y) / 2);
     // V字型突起の計算
          protrusionX = x + protrusionHeight * Math.Cos(angle + Math.PI /numberOfProtrusions);
          protrusionY = y + protrusionHeight * Math.Sin(angle + Math.PI / numberOfProtrusions);
          if (i > 0)
        {
              grf.DrawLine(pen, referencePoint.X, referencePoint.Y, startPoint.X, startPoint.Y);
              grf.DrawLine(pen, startPoint.X, startPoint.Y, (int)protrusionX, (int)protrusionY);
        }
          referencePoint.X = (int)protrusionX;
          referencePoint.Y = (int)protrusionY;
    }
      pictureBox.Image = imgBitmap;
      grf.Dispose();
}