I am trying to adapt this metapost patatoid generator to fit a list of pairs given to a macro.
To that purpose I loop through the list of pairs and determine the greatest x and y distances between all points with their coordinates. Then, I the the patatoid generator with the given width and height and shift it at the lowest coordinates to fit (almost) them all.
def drawlistofpoints(suffix p) =
numeric i ; i := 0 ;
forever:
if known p[i]:
drawdot p[i] withpen pencircle scaled 3pt ;
i := i+1 ;
fi ;
exitif unknown p[i] ;
endfor ;
enddef ;
vardef patatoid(expr w,h,x,y) =
hide(
numeric i,maxi,maxd ; i:=0; maxd := 0 ;
numeric dist[] ;
pair tmpp[] ;
path sq, p ;
sq := unitsquare xyscaled (w,h) shifted (x, y) ;
for i = 0 upto 3:
tmpp[i] := point (i + uniformdeviate(1)) of sq ;
endfor ;
tmpp[4] := tmpp[0] ;
for i= 0 upto 3 :
dist[i] := abs(tmpp[i+1] - tmpp[i]) ;
if (dist[i] > maxd) :
maxd := dist[i] ;
maxi := i ;
fi ;
endfor ;
p := for i = 0 upto maxi : tmpp[i] .. endfor (uniformdeviate(1))[tmpp[maxi],tmpp[maxi+1]] for i = maxi + 1 upto 3: .. tmpp[i] endfor .. cycle ;
)
p
enddef ;
def drawset(suffix p)=
numeric i,w,h,lx,hx,ly,hy; i:=0; w:=0; h:=0; lx:=0; hx:=0; ly:=0; hy:=0;
numeric offset ; offset = 2cm ;
forever:
if known p[i]: % this is a naive approach
if (xpart p[i]) < lx: lx := (xpart p[i]); fi
if (xpart p[i]) > hx: hx := (xpart p[i]); fi
if (ypart p[i]) < ly: ly := (ypart p[i]); fi
if (ypart p[i]) > hy: hy := (ypart p[i]); fi
i:=i+1;
fi
exitif unknown p[i];
endfor ;
hx := hx+offset ; lx := lx-offset ; hy := hy+offset ; ly := ly-offset ;
w := abs(hx - lx) ; h := abs(hy - ly) ;
draw patatoid(w,h,lx,ly) withpen pencircle scaled 1pt ;
drawlistofpoints(p) ;
enddef ;
However, if a point have "unusual" coordinates and is not packed with the others it will finish outside of the patatoid:
\startTEXpage
\startMPcode
pair a,b,c,d,e,f, g,h;
a := (0.5cm,0.75cm) ; b := (1.5cm,0.75cm) ; c := (2.5cm,0.75cm) ;
d := (0.5cm,2.25cm) ; e := (1.5cm,2.25cm) ; f := (2.5cm,2.25cm) ;
g := (2.25cm,4.5cm) ; h := (-1.5cm,-0.75cm) ;
pair points[];
points[0] := a ;
points[1] := b ;
points[2] := c ;
points[3] := d ;
points[4] := e ;
points[5] := f ;
points[6] := g ;
points[7] := h ;
drawset(points) ;
\stopMPcode
\stopTEXpage
How can I make sure to fit them all?



