
%
%

\documentclass[11pt]{article}
\usepackage{fleqn}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{epsfig}
\usepackage{makeidx}
% if you want to include PostScript figures
\usepackage{graphicx}

\parskip 12pt
\parindent 0pt

\newcommand{\lo}{\ensuremath{\mathbf{l}}}
\newcommand{\gl}{\ensuremath{\mathbf{g}}}
\def\pp{{\bf p}}
\def\qq{{\bf q}}
\def\rr{{\bf r}}
\def\vv{{\bf v}}
\def\ww{{\bf w}}
\def\uu{{\bf u}}
%\def\eee1{{\bf e}_1}
%\def\eee2{{\bf e}_2}

\def\bef{\begin{figure}[t]}
\def\eef{\end{figure}}

% ===========================================================

\title{2D Local \& Global Coordinates\\ Introduction to Computer Graphics \\ Arizona State University}

\author{G. Farin }
\date{\today}



\begin{document}

\maketitle

%\Large

\section{Motivation}

In computer graphics, we typically design geometry in a special
coordinate system, one that makes the construction easy, and then
we move this geometry to a more general environment. Font design
is a good 2D example. A designer will create a character of a font
within a unit square (local coordinates), and then this character
must be mapped to an arbitrary place and size on the printed page
(global coordinates).


\section{Local to Global and Back}

As illustrated in Figure \ref{fig:local_global}, we have a local coordinate box defined by lower-left corner $ \begin{bmatrix} 0 \\ 0\end{bmatrix}$ and an upper-right corner
 $ \begin{bmatrix} 1 \\ 1\end{bmatrix}$ that
we want to map to the global coordinate (target) box  defined by lower-left corner ${\bf g}_{min}$ and an upper-right corner ${\bf g}_{max}$.
Therefore, if we are {\em given} a point ${\bf l}$ in local coordinates, how do we {\em find} a point ${\bf g}$ in global coordinates?



\bef
  \begin{center}
  \epsfig{file=../figures/localglobal.ps,height=2in}
  \end{center}
  \caption{Local and global coordinates: notation}
  \label{fig:local_global}
\eef


We formulate an expression for the $x-$ and $y-$coordinates separately and notice that we would like to {\em preserve ratios}. This leads to
\begin{align*}
  \frac{l_x - 0}{1 - 0} &= \frac{g_x - g_{minx}}{g_{maxx} - g_{minx}}\\
  \frac{l_y - 0}{1 - 0} &= \frac{g_y - g_{miny}}{g_{maxy} - g_{miny}}\\
\end{align*}
Solving for the unknown ${\bf g}$ leads to
\begin{equation}
\begin{split}
  g_x &= g_{minx} + l_x(g_{maxx} - g_{minx}) \\
      &= g_{minx} + l_x\Delta_x \label{eq:localtoglobalx}
\end{split}
\end{equation}
\begin{equation}
\begin{split}
  g_y &= g_{miny} + l_y(g_{maxy} - g_{miny}) \\
  &= g_{miny} + l_y\Delta_y. \label{eq:localtoglobaly}
\end{split}
\end{equation}
We can re-write these equations in the form
\begin{align*}
  g_x &= (1-l_x)g_{minx} + l_x g_{maxx} \\
  g_y &= (1-l_y)g_{miny} + l_x g_{maxy},
\end{align*}
which is linear interpolation.


Notice that $\Delta_x$ and $\Delta_y$ effect the shape of the geometry in the target box.
\begin{itemize}
\item If $\Delta_x > 1$ then the geometry is stretched in $x$.
\item If $0< \Delta_x < 1$ then the geometry is squished in $x$.
\item If $\Delta_x = 1$ then there is no change.
\item If $\Delta_x < 0$ then the geometry flips.
\end{itemize}
The ratio of $\Delta_x/\Delta_y$
defines the {\em aspect ratio} of the target box. The aspect ratio of the local system is one; If the  target box has an aspect ratio different from one, then the object will be distorted.

If we are given a point in the global system, how do we find a point in the local system? Easy -- simply solve (\ref{eq:localtoglobalx}) and (\ref{eq:localtoglobaly}) for $l_x$ and $l_y$, respectively, namely
\begin{align*}
  l_x &= \frac{g_x - g_{minx}}{\Delta_x}\\
   l_y &= \frac{g_y - g_{miny}}{\Delta_y}
\end{align*}



\section{More General Coordinate Transformations}

Often times we will want to map an arbitrary local coordinate box to a global target box. Suppose the local box is defined by the corners $\lo_{min}$ and $\lo_{max}$, and
we wish to map to the global target box with corners $\gl_{min}$ and $\gl_{max}$, as illustrated in Figure \ref{fig:local_global_general}.

Given $\lo$, find $\gl$: Set up the ratios that will be preserved.
\begin{align*}
  \frac{l_x - l_{minx}}{l_{maxx} - l_{minx}} &= \frac{g_x - g_{minx}}{g_{maxx} - g_{minx}}\\
  \frac{l_y - l_{miny}}{l_{maxy} - l_{miny}} &= \frac{g_y - g_{miny}}{g_{maxy} - g_{miny}}\\
\end{align*}
And solve for $\gl$:
\begin{align*}
  g_x &= g_{minx} + [\frac{l_x - l_{minx}}{l_{maxx} - l_{minx}}](g_{maxx} - g_{minx})\\
  g_y &= g_{miny} + [\frac{l_y - l_{miny}}{l_{maxy} - l_{miny}}](g_{maxy} - g_{miny}).
\end{align*}
Notice the $[l_{max},l_{min}] \rightarrow [0,1]$ mapping embedded in this equation!

Now it is easy to find $\lo$ given $\gl$. Try it for yourself.


\bef
  \begin{center}
  \epsfig{file=../figures/localglobal2.ps,height=2in}
  \end{center}
  \caption{Local and global coordinates: notation}
  \label{fig:local_global_general}
\eef

\section{World to Window Transformations}

It is a common practice to define a {\em minmax box} around the geometry that
you want to display. Another name for this box is a {\em world coordinate system}.
This minmax box is then the input to a command such as OpenGL's
gluOrtho2D(), which in turn, maps this minmax box to the window.
Let's suppose that
our minmax box is a square of width four, centered at the origin. In other words,
the lower-left corner is at $\begin{bmatrix} -2\\ -2\end{bmatrix}$. The OpenGL
command takes the form
$$
  \textrm{gluOrtho2D}(-2, 2, -2, 2).
$$

As long as the window remains square, the aspect ratio of the world system and the
window are the same, and your geometry is not distorted. However, if the window
is allowed to be reshaped, and if we want our geometry to be displayed without
distortion, then we need to compensate for such changes in aspect ratio.

Suppose the world system has width $w_0$ and height $h_0$, and therefore,
aspect ratio $w_0/h_0$. In our example here, the aspect ratio of the world system
is one. Suppose the window has been reshaped to have width $w_1$ and $h_1$, and
further, suppose that $w_1 < h_1$.

We need to stretch the  minmax box to match the shape of the window's shape. In
other words, we need
$$
  \frac{w_0}{h_0} = \frac{w_1}{h_1}.
$$
We will leave $w_0$ as it is, and stretch the height:
$$
  h_0 = w_0\frac{h_1}{w_1}.
$$
Because our minmax box is centered at the origin, we simply center this new
height around the origin, and the new OpenGL command becomes
$$
  \textrm{gluOrtho2D}(-2, 2, -2\frac{h_1}{w_1}, 2\frac{h_1}{w_1}).
$$
Our initial assumption was that $w_1 < h_1$, therefore $h_1/w_1 > 1$ and
we have achieved the stretching we needed.


Now determine the gluOrtho() parameters if $w_1 > h_1$ for yourself!



For more information, examples, and exercises, see {\bf Practical
Linear Algebra}.


\end{document}
