7

I have a set of points. I would like to "enclose" the points by a region. Since a picture is worth a thousand words:

enter image description here

The dots are the points I want to enclose. I want the ability to control the enclosure "rate". A parameter that allows me to go from the convex hull of the points to the minimal area(but within a padding). The Yellow region is the convex hull while the red region is a sort of "minimal" region(with padding). I would like to be able to get any region "in between". e.g., 0 would be minimal, 1 would be "maximal"(convex hull) and 1/2 would sort of be an average

The point of this is to "highlight" a group of points(which you can see from the picture that regions do a good job) but I in some cases I'll need to prevent overlap by not using the convex hull(for example, if you combined the points from the blue and red then the convex hull of those points would overlap with the green while the minimal region would not).

This maybe more of a mathematics problem than tikz but maybe someone knows of an easy way to accomplish this. (I am using lualatex and would prefer lua code(or possibly C/C++) since it would surely run faster)

BTW, the point is to make it easy to use. I would like to simply specify the points, the parameter, and the color and that's it else it will become too tedious as there are a lot of regions to deal with.

Tom Bombadil
  • 40,123
Uiy
  • 6,132
  • 2
  • @PeterGrill It looks like it is. Not sure about the bumerang though. – percusse Apr 16 '12 at 21:25
  • 2
    @PeterGrill's linked question seems to be appropriate for the convex hull, but the question about minimal paths might get very involving, especially for a lrger number of nodes. I think this is related to the travelling salesman problem where you would drop the longest edge. Even bigger sets might require an iterative approach like ruin and recreate, although I don't know how to implement either or how soon this would exceed TeX's capacity. – Tom Bombadil Apr 16 '12 at 21:36
  • @TomBombadil I agree but I don't think we should be relying on TeX engine for finding out the unique solution for NP-hard problems. This is very much like a reducing labor type of attempt, at least that's how I got it. Might be wrong. – percusse Apr 16 '12 at 21:47
  • @PeterGrill: The TSP is a global optimization problem. The minimal region is simply the union of all expanded line segments of adjacent points. (for example, in the red region we do not care about the two distant points) this is quite easy to compute but some smoothing and visualize optimization should take place(although once one has the interpolation done maybe just setting the parameter close to 0 will be good enough) – Uiy Apr 16 '12 at 22:19
  • In any case my number of points is rather small so even if it were the TSP could be computed in a few seconds. (I'll have a maximum of about 10 points and on average just a few) – Uiy Apr 16 '12 at 22:22
  • Basically I want to go from the concave hull to the convex hull – Uiy Apr 16 '12 at 22:27
  • 1
    This will help: http://www.iis.sinica.edu.tw/page/jise/FILE/AcceptedList/100/100295-ANC.pdf (The difference is I have few points and want to find a rather smooth and large concave hull around the line segments connecting adjacent points) – Uiy Apr 16 '12 at 22:44
  • 2
    Alpha hull is the word you want to look for, see this question on the gis site, http://gis.stackexchange.com/q/1200/751. Couldn't find any tikz library with a quick google search, but some stat packages and many gis packages are capable of computing such polygons. – Andy W Apr 16 '12 at 23:11
  • @AndyW I think the "alpha hull" is just a technique that uses "alpha shapes" to compute the concave hull. This would work for what I want to do since I just need something simple(but the region boundary should be smooth and simple(no holes)). There probably is no tex/tikz solution but maybe someone smarter than me can get something to work with lua and tikz... I hope. – Uiy Apr 17 '12 at 07:12
  • It is just a semantic difference between fields, alpha shapes and alpha hulls are the same thing (except alpha hulls have been generalized to more than two dimensions). You might look at some of the open source libraries to compute them (I know the statistical package R can) to see if they can be ported to your language of choice. I suspect it is not a trivial task though. – Andy W Apr 17 '12 at 12:02
  • @AndyW The biggest problem is I don't have the time or I would ;/ I'll try to settle for something simpler ;/ – Uiy Apr 17 '12 at 17:37

1 Answers1

9

[en]I'm not sure it meets your problem completely but I think it is a constructive draft

Update for the latest version of pgf: it is noted that the definition of atan2 has been modified in the latest versions, we have moved from atan2 (x, y) to atan2 (y, x).

[fr]je ne suis pas sur que cela reponde complètement à ton problème mais je pense que c'est une ébauche constructive

Mise à jour pour la dernière version de pgf : il est a noté que la définition de atan2 a été modifié dans les dernières versions, on est passé de atan2(x,y) à atan2(y,x).

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{backgrounds}

\newdimen\qx
\newdimen\qy
\begin{document}

\begin{tikzpicture}

\foreach \nn/\cood in {1/{0,0},2/{1,2},3/{3,2},4/{4,1}}{
\node [circle,draw,fill=green,label=N\nn](N\nn) at (\cood) {};
}

\foreach \nn [remember=\nn as \lastx (initially 1)] in {2,3,4}{
\begin{scope}[shift={(N\lastx)}]
\pgfextractx\qx{\pgfpointanchor{N\nn}{center}}
\pgfextracty\qy{\pgfpointanchor{N\nn}{center}}
\pgfmathsetmacro{\angle}{atan2(\qy,\qx)}
\begin{scope}[rotate=\angle,on background layer]
\node[circle,minimum width=1cm,](N1a)at(N\lastx){};
\node[circle,minimum width=1cm,](N2a)at(N\nn){};
\coordinate(N1s) at (N1a.{\angle-90});
\coordinate(N1n) at (N1a.{\angle+90});
\coordinate(N2s) at (N2a.{\angle-90});
\coordinate(N2n) at (N2a.{\angle+90});
\draw[fill=pink,draw=pink] (N1s) (N1s) -- (N2s) arc (-90:90:0.5cm)--(N1n) arc (90:270:0.5cm)  ;
\end{scope}
\end{scope}
}

\end{tikzpicture}
\end{document}

enter image description here

rpapa
  • 12,350