一个坐标点围绕任意中心点旋转--C#实现
分类: C#/ASP.net 重用代码之C# 2011-07-19 20:23 1668人阅读 评论(0) 收藏 举报
假设对图片上任意点(x,y),绕一个坐标点(rx0,ry0)逆时针旋转RotaryAngle角度后的新的坐标设为(x', y'),有公式:
x'= (x - rx0)*cos(RotaryAngle) + (y - ry0)*sin(RotaryAngle) + rx0 ;
y'=-(x - rx0)*sin(RotaryAngle) + (y - ry0)*cos(RotaryAngle) + ry0 ;
[csharp] view plaincopy
/// <summary>
/// 对一个坐标点按照一个中心进行旋转
/// </summary>
/// <param name="center">中心点</param>
/// <param name="p1">要旋转的点</param>
/// <param name="angle">旋转角度,笛卡尔直角坐标</param>
/// <returns></returns>
private Point PointRotate(Point center, Point p1, double angle)
{
Point tmp = new Point();
double angleHude = angle * Math.PI / 180;/*角度变成弧度*/
double x1 = (p1.X - center.X) * Math.Cos(angleHude) + (p1.Y - center.Y ) * Math.Sin(angleHude) + center .X;
double y1 = -(p1.X - center.X) * Math.Sin(angleHude) + (p1.Y - center.Y) * Math.Cos(angleHude) + center.Y;
tmp.X = (int)x1;
tmp.Y = (int)y1;
return tmp;
}
1 、旋转
e.Graphics.RotateTransform(30.0F, MatrixOrder.Prepend);
2、平移
e.Graphics.TranslateTransform(100.0F, 0.0F);
3、缩放
e.Graphics.ScaleTransform(3.0F, 1.0F, MatrixOrder.Append);
4、点坐标变换
e.Graphics.TranslateTransform(40, 30);
e.Graphics.TransformPoints(CoordinateSpace.Page, CoordinateSpace.World, points);
e.Graphics.ResetTransform();
// Create array of two points.
Point[] points = { new Point(0, 0), new Point(100, 50) };
// Draw line connecting two untransformed points.
e.Graphics.DrawLine(new Pen(Color.Blue, 3), points[0], points[1]);
// Set world transformation of Graphics object to translate.
e.Graphics.TranslateTransform(40, 30);
// Transform points in array from world to page coordinates.
e.Graphics.TransformPoints(CoordinateSpace.Page, CoordinateSpace.World, points);
// Reset world transformation.
e.Graphics.ResetTransform();
// Draw line that connects transformed points.
e.Graphics.DrawLine(new Pen(Color.Red, 3), points[0], points[1]);
5、选择参数:将此变换参数放在已有的变换矩阵之后还是之前。MatrixOrder.Append//MatrixOrder.Prepend
//----------------旋转和平移的顺序不一样,得到的结果页不一样。
6、辅助功能
GraphicsState transState = e.Graphics.Save();//保存当前绘图板状态
e.Graphics.ResetTransform();//重置
e.Graphics.Restore(transState);//置为此状态