using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing.Drawing2D; using System.Drawing.Imaging; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); this.Load += new EventHandler(Form1_Load); } const int cw = 400; const int ch = 400; Bitmap[] bmp = new Bitmap[2]; int screen = 0; PictureBox pictureBox1 = new PictureBox(); Timer timer1 = new Timer(); GraphicsPath gp = new GraphicsPath(); private void Form1_Load(object sender, EventArgs e) { this.ClientSize = new Size(cw, ch); this.Controls.Add(pictureBox1); pictureBox1.Dock = DockStyle.Fill; bmp[0] = new Bitmap(cw, ch, PixelFormat.Format32bppArgb); bmp[1] = new Bitmap(cw, ch, PixelFormat.Format32bppArgb); PointF[] tp = new PointF[] { new PointF(0.0f,10.0f), new PointF(10.0f,0.0f), new PointF(0.0f,-10.0f), }; gp.AddLine(0.0f, 0.0f, 20.0f, 0.0f); gp.AddPolygon(tp); timer1.Interval = 1000 / 60; timer1.Tick += new EventHandler(timer1_Tick); timer1.Enabled = true; } const float vec = 5.0f; float vx; float vy; int phase = 0; Random rand = new Random(); double baseangle; double angle = 0.0; int count = 0; PointF p = new PointF(200.0f, 200.0f); private void timer1_Tick(object sender, EventArgs e) { switch (phase) { //思考フェーズ case 0: baseangle = rand.Next(0, 90)-45; phase = 1; break; //回転フェーズ case 1: if (baseangle != 0) { if (baseangle > 0) { angle++; baseangle--; } else { angle--; baseangle++; } } else { double subangle = angle / (180 / Math.PI); vx = vec * (float)Math.Cos(subangle); vy = vec * (float)Math.Sin(subangle); phase = 2; } break; //移動フェーズ case 2: if (count < 20) { p.X += vx; p.Y += vy; count++; if (p.X < 20 || p.X >= cw-20 || p.Y < 20 || p.Y >= ch-20) { count = 0; baseangle = 180; phase = 1; } } else { count = 0; phase = 0; } break; default: break; } this.Text = phase.ToString(); //double angle = 45.0;// / (180.0 / Math.PI); using (Graphics g = Graphics.FromImage(bmp[screen])) { g.SmoothingMode = SmoothingMode.AntiAlias; g.FillRectangle(Brushes.White, 0, 0, cw, ch); Matrix m = new Matrix(); m.Translate(p.X, p.Y); m.Rotate((float)angle); g.Transform = m; g.DrawPath(Pens.Blue, gp); } pictureBox1.Image = bmp[screen]; screen ^= 1; } } }