# viewmtx

## Purpose

View transformation matrices.

## Synopsis

    T = viewmtx(az,el)
    T = viewmtx(az,el,phi)
    T = viewmtx(az,el,phi,xc)

## Description

`viewmtx` computes 4-by-4 orthographic or perspective transformation matrices that project four-dimensional homogeneous vectors onto a two-dimensional view surface (for example, your computer screen). A four-dimensional homogenous vector is formed by appending a 1 to the corresponding three-dimensional vector. For instance the four-dimensional vector corresponding to the three-dimensional point `[x,y,z]'` is `[x,y,z,1]'`. The `x` and `y` components of the projected vector are the desired two-dimensional components (see "Examples" below).

`T = viewmtx(az,el)` returns an orthographic transformation matrix corresponding to azimuth `az` and elevation `el`. `viewmtx` uses the same definition for azimuth and elevation as `view`, in particular, `az` and `el` are specified in degrees.`T = viewmtx(az,el)` returns the same matrix as the commands

    view(az,el)
    T = view

but doesn't change the current view.

`T = viewmtx(az,el,phi)` returns a perspective transformation matrix. `phi` is the subtended view angle of the normalized plot cube (in degrees) and controls the amount of perspective distortion:

    -------------------------------
    Phi  Description
    -------------------------------
    0    Orthographic projection.
    10   Similar to telephoto lens.
    25   Similar to normal lens.
    60   Similar to wide angle lens.
    -------------------------------

The matrix returned can be used to set the view transformation with `view(T)`. The 4-by-4 perspective transformation matrix transforms four-dimensional homogeneous vectors into unnormalized vectors of the form (x,y,z,w) where w is not equal to 1. The x- and y -components of the normalized vector (x/w, y/w, z/w, 1) are the desired two-dimensional components (see example below).

`T = viewmtx(az,el,phi,xc)` returns the perspective transformation matrix using `xc` as the target point within the normalized plot cube, (that is the camera is looking at the point `xc`). The coordinate center is specified as a three-element vector, `xc = [xc,yc,zc]`. Valid values for the components of `xc` are in the interval [0,1]. The default value is `xc = [0,0,0]`.

## Examples

Determine the projected two-dimensional vector corresponding to the three-dimensional point (.5,0,-3) using the default view direction.

    A = viewmtx(-37.5,30);
    x4d = [.5  0  -3  1]';
    x2d = A*x4d;
    x2d = x2d(1:2)

    x2d =
         0.3967
        -2.4459

Vectors that trace the edges of a unit cube are

    x = [0  1  1  0  0  0  1  1  0  0  1  1  1  1  0  0];
    y = [0  0  1  1  0  0  0  1  1  0  0  0  1  1  1  1];
    z = [0  0  0  0  0  1  1  1  1  1  1  0  0  1  1  0];

Transform the points in these vectors to the screen, and then plot the object.

    A = viewmtx(-37.5,30);
    [m,n] = size(x);
    x4d = [x(:),y(:),z(:),ones(m*n,1)]';
    x2d = A*x4d;
    x2 = zeros(m,n); y2 = zeros(m,n);
    x2(:) = x2d(1,:);
    y2(:) = x2d(2,:);
    plot(x2,y2)

``Now repeat the above, but use a perspective transformation.

    A = viewmtx(-37.5,30,25);
    x4d = [.5  0  -3  1]';
    x2d = A*x4d;
    x2d = x2d(1:2)/x2d(4);% Normalize

    x2d =
         0.1777
        -1.8858

And again transform the cube vectors to the screen, and plot the object.

    A = viewmtx(-37.5,30,2);
    [m,n] = size(x);
    x4d = [x(:),y(:),z(:),ones(m*n,1)]';
    x2d = A*x4d;
    x2 = zeros(m,n); y2 = zeros(m,n);
    x2(:) = x2d(1,:)./x2d(4,:);
    y2(:) = x2d(2,:)./x2d(4,:);
    plot(x2,y2)

## See Also

    view

(c) Copyright 1994 by The MathWorks, Inc.