图形变换

缩放、旋转和平移是图形的基本变化,从几何算法的角度看是统一的:都是借助变换向量实现。
本文针对这三种基本变换操作封装三个接口。值得注意的是,这三个接口都是针对实体本身,即使实体不存在数据库中也可以使用。

旋转

1
2
3
4
5
6
7
8
public static Entity Rotate(this Entity entity, Point3d basePoint, double ratation)
{
if (entity == null) return null;
Matrix3d mt = Matrix3d.Rotation(ratation, Vector3d.ZAxis, basePoint);
Entity tempEntity = entity.Clone() as Entity;
tempEntity.TransformBy(mt);
return tempEntity;
}

缩放

1
2
3
4
5
6
7
8
public static Entity Scale(this Entity entity, Point3d basePoint, double scaleFactor)
{
if (entity == null) return null;
Matrix3d mt = Matrix3d.Scaling(scaleFactor, basePoint);
Entity tempEntity = entity.Clone() as Entity;
tempEntity.TransformBy(mt);
return tempEntity;
}

平移

1
2
3
4
5
6
7
8
9
public static Entity Move(this Entity entity, Point3d startPoint, Point3d endPoint)
{
if (entity == null) return null;
Vector3d vector = startPoint.GetVectorTo(endPoint);
Matrix3d mt = Matrix3d.Displacement(vector);
Entity tempEntity = entity.Clone() as Entity;
tempEntity.TransformBy(mt);
return tempEntity;
}

评论