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; namespace アフィン変換サンプル___ { public partial class Form1 : Form { static PointF[] Arrow = new PointF[] { new PointF(0, 2), new PointF(1.5f, 1), new PointF(1, 1), new PointF(1, -1), new PointF(-1, -1), new PointF(-1, 1), new PointF(-1.5f, 1) }; PointF[] ArrowD = new PointF[Arrow.Count()]; double Direction = 0; float Sc = 32;//Scale. コントロールに同名の何かが会ったから短くした。 uint Mode = 3; public Form1() { InitializeComponent(); Application.Idle += new EventHandler(OnIdle); } void OnIdle(object sender, EventArgs e) { PointF Move= new PointF(this.ClientSize.Width/2.0f,this.ClientSize.Height/2.0f); for (int i = 0; i < Arrow.Count(); i++) { ArrowD[i] = TransForm(Arrow[i], Direction, new SizeF(Sc, Sc), Move);//大変換!! } this.Invalidate(false); System.Threading.Thread.Sleep(15); } private void Form1_Paint(object sender, PaintEventArgs e)//event { Graphics g = e.Graphics; g.FillPolygon(Brushes.Green, ArrowD);//多角形塗りつぶし。 float X = this.ClientSize.Width / 2; float Y = this.ClientSize.Height / 2; float L = 8; g.DrawLine(Pens.Black, X - L, Y, X + L, Y); g.DrawLine(Pens.Black, X, Y - L, X, Y + L); } private void Form1_MouseMove(object sender, MouseEventArgs e)//event { float X = this.ClientSize.Width/2.0f-e.X; float Y = this.ClientSize.Height/2.0f-e.Y; if ((Mode&1) !=0) Direction = Math.Atan2(Y, X) + ToRadian(90); if ((Mode&2) !=0) Sc = (float)Math.Sqrt(X * X + Y * Y); } private void Form1_MouseDown(object sender, MouseEventArgs e)//event { Mode %= 3; Mode++; } double SingleRadian = Math.PI / 180.0; public double ToRadian(double Degree) { return SingleRadian * (Degree % 360.0); } public double ToDegree(double Radian) { return (Radian / SingleRadian) % 360.0; } public PointF TransForm(PointF Pos, double RotRadian, SizeF Scale, PointF Move) { PointF Ret = new PointF(); double Sin = Math.Sin(RotRadian); double Cos = Math.Cos(RotRadian); Ret.X = (float)(((Pos.X * Scale.Width) * Cos) - ((Pos.Y * Scale.Height) * Sin) + Move.X); Ret.Y = (float)(((Pos.X * Scale.Width) * Sin) + ((Pos.Y * Scale.Height) * Cos) + Move.Y); return Ret; } } }