1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
%\VignetteIndexEntry{Qhull examples}
\documentclass{article}
\usepackage{Sweave}
\SweaveOpts{echo=TRUE}
\usepackage{hyperref}
\usepackage[british]{babel}
\title{Qhull examples}
\author{David C. Sterratt}
\begin{document}
\maketitle
This document presents examples of the \texttt{geometry} package
functions which implement functions using the
\href{http://www.qhull.org}{Qhull library}.
\section{Convex hulls in 2D}
\label{qhull-eg:sec:convex-hull-2d}
\subsection{Calling \texttt{convhulln} with one argument}
\label{qhull-eg:sec:call-convh-with}
With one argument, convhulln returns the indices of the points of the
convex hull.
<<>>=
library(geometry)
ps <-matrix(rnorm(30), , 2)
ch <- convhulln(ps)
head(ch)
@
\subsection{Calling \texttt{convhulln} with \texttt{options}}
\label{qhull-eg:sec:call-convh-with}
We can supply Qhull options to \texttt{convhulln}; in this case it
returns an object of class \texttt{convhulln} which is also a list.
For example \texttt{FA} returns the generalised \texttt{area} and
\texttt{vol}ume. Confusingly in 2D the generalised area is the length
of the perimeter, and the generalised volume is the area.
<<>>=
ps <-matrix(rnorm(30), , 2)
ch <- convhulln(ps, options="FA")
print(ch$area)
print(ch$vol)
@
A \texttt{convhulln} object can also be plotted.
<<fig=TRUE>>=
plot(ch)
@
We can also find the normals to the ``facets'' of the convex hull:
<<>>=
ch <- convhulln(ps, options="n")
head(ch$normals)
@
Here the first two columns and the $x$ and $y$ direction of the normal,
and the third column defines the position at which the face intersects
that normal.
\subsection{Testing if points are inside a convex hull with \texttt{inhulln}}
\label{qhull-eg:sec:testing-if-points}
The function \texttt{inhulln} can be used to test if points are inside
a convex hull. Here the function \texttt{rbox} is a handy way to
create points at random locations.
<<fig=TRUE>>=
tp <- rbox(n=200, D=2, B=4)
in_ch <- inhulln(ch, tp)
plot(tp[!in_ch,], col="gray")
points(tp[in_ch,], col="red")
plot(ch, add=TRUE)
@
\section{Delaunay triangulation in 2D}
\label{qhull-eg:sec:dela-triang-2d}
\subsection{Calling \texttt{delaunayn} with one argument}
\label{qhull-eg:sec:call-delaunayn-with}
With one argument, a set of points, \texttt{delaunayn} returns the
indices of the points at each vertex of each triangle in the triangulation.
<<fig=TRUE>>=
ps <- rbox(n=10, D=2)
dt <- delaunayn(ps)
head(dt)
trimesh(dt, ps)
points(ps)
@
\subsection{Calling \texttt{delaunayn} with \texttt{options}}
\label{qhull-eg:sec:call-dela-with}
We can supply Qhull options to \texttt{delaunayn}; in this case it
returns an object of class \texttt{delaunayn} which is also a list.
For example \texttt{Fa} returns the generalised \texttt{area} of each
triangle. In 2D the generalised area is the actual area; in 3D it
would be the volume.
<<>>=
dt2 <- delaunayn(ps, options="Fa")
print(dt2$areas)
dt2 <- delaunayn(ps, options="Fn")
print(dt2$neighbours)
@
\end{document}
% LocalWords: Qhull convhulln ps rnorm ume inhulln rbox tp gray dt
% LocalWords: delaunayn trimesh Fn
|