[Tex/LaTex] Setting coordinate system for TiKZ picture

coordinatesgeometrytikz-pgf

I'd like to use TiKZ to generate stand-alone PDF diagrams programatically. I have an application that generates simple geometry of lines and arcs etc and I'd like to create (using e.g. Ruby) TikZ diagram text files from that simple geometry.

I have a bounding box (AABB) of each chunk of geometry and my question is what is the most idiomatic TiKZ and LaTeX/ConTeXt way of specifying what would in effect be a window-to-viewport transform between the application geometry bounds and the TiKZ drawing bounds?

I can see I can use:

\begin{tikzpicture}[x=2cm,y=1.5cm] 

to set distance units and:

scale=1.5 

parameter, but I suspect there's a better way.

Any advice is much appreciated,

Best Answer

I'm not sure this is what your looking for, but probably you want something like this:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows}

\begin{document}

\newcommand{\xangle}{10}
\newcommand{\yangle}{110}
\newcommand{\zangle}{45}

\newcommand{\xlength}{1}
\newcommand{\ylength}{1.7}
\newcommand{\zlength}{0.6}

\pgfmathsetmacro{\xx}{\xlength*cos(\xangle)}
\pgfmathsetmacro{\xy}{\xlength*sin(\xangle)}
\pgfmathsetmacro{\yx}{\ylength*cos(\yangle)}
\pgfmathsetmacro{\yy}{\ylength*sin(\yangle)}
\pgfmathsetmacro{\zx}{\zlength*cos(\zangle)}
\pgfmathsetmacro{\zy}{\zlength*sin(\zangle)}

\begin{tikzpicture}
[   x={(\xx cm,\xy cm)},
    y={(\yx cm,\yy cm)},
    z={(\zx cm,\zy cm)},
]
\draw[-latex,red] (-1,0,0) -- (3.5,0,0) node[label=\xangle:x] {};
\draw[-latex,blue] (0,-1,0) -- (0,3.5,0) node[label=\yangle:y] {};
\draw[-latex,black] (0,0,-1) -- (0,0,3.5) node[label=\zangle:z] {};

\foreach \c in {-1,...,3}
{   \fill[red] (\c,0,0) circle (0.05cm);
    \fill[blue] (0,\c,0) circle (0.05cm);
    \fill[black] (0,0,\c) circle (0.05cm);
}
\end{tikzpicture}

\end{document}

enter image description here

You can specify all axes as x={(x-component,y-component)}. In this example you can specify the angle in which a coordinate axis points (xangle) and it's length (xlength). From this, the components are automatically computed. In the options for the TikZ picture I wrote \xx cm, otherwise TikZ will assume the measures are given in points, thus resulting in a very tiny picture. Try playing around with the xyzangles and xyzlengths to see the picture change.