Программа вращения фигуры вокруг заданной точки
Файл Form1.cs
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 WindowsFormsApplication8 { public partial class Form1 : Form {
ObjectCreater objectCreater = new ObjectCreater(); ObjectTransform objectTransform = new ObjectTransform(); Graphics graphics; float angle; PointF pointf; float x; float y; int offsetx; int offsety; int dx; int dy;
//Image image; public Form1() { InitializeComponent(); angle = 0; x = 150; y = 150; pointf = new PointF(x, y); offsetx = 0; offsety = 0; dx = 1; dy = 1; }
private void button1_Click(object sender, EventArgs e) { }
private void pictureBox1_Paint(object sender, PaintEventArgs e) { timer1.Enabled = true; timer1.Interval = 30; objectTransform.ObjectRotate(angle, pointf, offsetx, offsety, e); angle += 1; if (angle >= 360) { angle = 0; } offsetx += dx; offsety += dy; if (offsetx >= pictureBox1.ClientSize.Width) { dx = -1; } else if (offsetx <= 50) { dx = 1; } if (offsety >= pictureBox1.ClientSize.Height) { dy = -1; } else if (offsety <= 50) { dy = 1; } }
private void timer1_Tick(object sender, EventArgs e) { pictureBox1.Refresh(); }
} }
Файл ObjectTransform.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; using System.ComponentModel;
namespace WindowsFormsApplication8 { public class ObjectTransform { Pen pen; public ObjectTransform() { pen = new Pen(Color.Black, 1); }
public Graphics ObjectRotate(float angle,PointF pointf, int offsetX, int offsetY, PaintEventArgs e) { SolidBrush brush = new SolidBrush(Color.Blue); Matrix matrix = new Matrix(); //matrix.Translate(offsetX, offsetY); matrix.RotateAt(angle, pointf, MatrixOrder.Append); //matrix.Translate(offsetX, offsetY); e.Graphics.Transform = matrix; e.Graphics.FillRectangle(brush, 100, 100, 20, 20); return e.Graphics; } } }
|