放大后如何提高翻译速度?

时间:2012-10-21 06:55:58

标签: c# winforms graphics user-controls

我有一个放大和缩小的控件,以及平移。通过单击两个按钮的选择,用户可以分别放大或缩小。我的问题是放大或缩小后的翻译:如果我缩小到大约.2F,我必须多次单击以移动我在基数0处可以达到的相同距离。我似乎通过划分缩放来解决这个问题通过它的自平方:z /(z * z),但这似乎改变了整个矩阵 - 我不是在任何一行上用矩阵翻译矩阵!

这是我的代码:

Matrix mtrx = new Matrix();
mtrx.Translate(pan.X, pan.Y);
mtrx.Scale(zoom, zoom);
e.Graphics.Transform = mtrx;

// To-Do drawing code here

基本笔记:

  • 按钮点击事件
  • 上的缩放增加和减少.12F
  • 正在绘制的控件是继承的UserControl类
  • 开发环境是C#2010
  • 绘图代码处理两个位置为{0,0}
  • 的矩形
  • 当我放大或缩小某种方式时,我只希望能够以0(不放大或缩小)的速度以相同的速度平移。 〜没有动作延迟的感觉
编辑:我已将上面的代码更新为我以前处理过的改进版本。这可以处理缩放后的平移速度,但新问题在于缩放发生的位置:就好像正在翻译缩放点一样......

编辑:我将如何在3D(XNA)中执行此操作:

public class Camera
{
    private float timeLapse = 0.0f;
    private Vector3 position, view, up;

    public Camera(Vector2 windowSize)
    {
        viewMatrix = Matrix.CreateLookAt(this.position, this.view, this.up);
        projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.Pi / 4.0f,
                           (float)windowSize.X / (float)windowSize.Y, 0.005f, 1000.0f);
    }
    //Sets time lapse between frames
    public void SetFrameInterval(GameTime gameTime)
    {
        timeLapse = (float)gameTime.ElapsedGameTime.Milliseconds;
    }

    //Move camera and view position according to gamer's move and strafe events.
    private void zoomHelper(Vector3 direction, float speed)
    {
        speed *= (float)timeLapse; // scale rate of change
        direction *= speed;
        position.Y += direction.Y;
        position.X += direction.X;      // adjust position 
        position.Z += direction.Z;
        view.Y += direction.Y;
        view.X += direction.X;      // adjust view change
        view.Z += direction.Z;
    }
    //Allows the camera to move forward or backward in the direction it is looking.
    public void Zoom(float amount)
    {
        Vector3 look = new Vector3(0.0f, 0.0f, 0.0f);
        Vector3 unitLook;
        // scale rate of change in movement
        const float SCALE = 0.005f;
        amount *= SCALE;
        // update forward direction
        look = view - position;
        // get new camera direction vector 
        unitLook = Vector3.Normalize(look);
        // update camera position and view position
        zoomHelper(unitLook, amount);
        viewMatrix = Matrix.CreateLookAt(position, view, up);
    }

    //Allows the camera to pan on a 2D plane in 3D space.
    public void Pan(Vector2 mouseCoords)
    {
        // The 2D pan translation vector in screen space
        Vector2 scaledMouse = (mouseCoords * 0.005f) * (float)timeLapse;
        // The camera's look vector
        Vector3 look = view - position;
        // The pan coordinate system basis vectors
        Vector3 Right = Vector3.Normalize(Vector3.Cross(look, up));
        Vector3 Up = Vector3.Normalize(Vector3.Cross(Right, look));
        // The 3D pan translation vector in world space
        Vector3 Pan = scaledMouse.X * Right + -scaledMouse.Y * Up;
        // Translate the camera and the target by the pan vector
        position += Pan;
        view += Pan;
        viewMatrix = Matrix.CreateLookAt(position, view, up);
    }
}

像这样使用:

Camera cam; // object
effect.Transform = cam.viewMatrix * cam.projectionMatrix;
// ToDo: Drawing Code Here

1 个答案:

答案 0 :(得分:0)

试试这个:

Matrix mtrx = new Matrix();  
mtrx.Scale(zoom, zoom);  
mtrx.Translate(pan.X/zoom, pan.Y/zoom, MatrixOrder.Append);   
e.Graphics.Transform = mtrx;  
// To-Do drawing code here