Requires: "Fast and Robust Curve Intersections"
P=POLYMORPH(P1,P2) returns an array for the morphing of polygon P1 to polygon P2. Both polygons must be two dimensional but can have different number of points.
POLYMORPH(P1,P2,NUMSTEPS) morphs the polygons P1 into polygon P2 within a specified NUMSTEPS number of steps. (Default is 10)
POLYMORPH(P1,P2,NUMSTEPS,NUMPTS) interpolates the two polygons so they have at least NUMPTS number of points. All data points in original input polygons are contained in the output polygons, while also ensuring interpolated polygons have the same number of points. The algorithm uses the least common multiple of #P1 and #P2. If NUMPTS<LCM then LCM is used. (Default is 1, implying use LCM)
POLYMORPH(P1,P2,NUMSTEPS,NUMPTS,EXACT) with non-zero value for EXACT if you want to have exactly NUMPTS number of points. This is beneficial for a couple scenarios:
- Splicing multiple sets of polygons together that may have different number of points between each polygon. P1, P2, P3,...
- Forced downsampling for larger polygons where the overall shape is important, but not the inclusion of every original data point. This will save computation time.
Note: Due to forcing a new constraint on number of output points, some of the original data points may not appear in the output interpolated polygons when EXACT is non-zero. (Default is 0)
Algorithm will automatically try and solve for a morphing that produces no self-intersections through time, and yields a minimum total distance-squared for the path of each point. If no such path is found, the best self-intersecting morph is given.
With more NUMPTS a higher chance of discovering a non-intersecting morph is found, however the intermediate shapes might not be preserved - that is, from a triangle to a triangle an intermediate shape might be a quadrilateral.
Note: For the output, the last point for each polygon is identical to the first point, for closure. If EXACT=1 there will be NUMPTS rows of points given, but NUMPTS-1 unique points in the polygons.
Output is an array where
P(:,:,1) P1 polygon (with interpolated points)
P(:,:,t) an intermediate polygon
P(:,:,NUMSTEPS) P2 polygon (with interpolated points)
x2=rand(8,1); y2=rand(8,1); k2 = convhull(x2,y2); P2=[x2(k2) y2(k2)];
hold on, plot(P(:,1,i),P(:,2,i)), axis([0 1 0 1]), pause(0.1)