Code covered by the BSD License

### Highlights from polymorph

5.0
5.0 | 2 ratings Rate this file 9 Downloads (last 30 days) File Size: 4.51 KB File ID: #32208 Version: 1.3

# polymorph

### Mike Sheppard (view profile)

13 Jul 2011 (Updated )

Morphs two polygons

File Information
Description

Requires: "Fast and Robust Curve Intersections"
http://www.mathworks.com/matlabcentral/fileexchange/11837

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)

EXAMPLE:

numsteps=20; numpts=10;
P1=rand(3,2);
x2=rand(8,1); y2=rand(8,1); k2 = convhull(x2,y2); P2=[x2(k2) y2(k2)];
P=polymorph(P1,P2,numsteps,numpts);
for i=1:size(P,3)
hold on, plot(P(:,1,i),P(:,2,i)), axis([0 1 0 1]), pause(0.1)
end

Acknowledgements

Fast And Robust Curve Intersections inspired this file.

MATLAB release MATLAB 7.12 (R2011a)
Other requirements Fast and Robust Curve Intersections http://www.mathworks.com/matlabcentral/fileexchange/11837
24 Sep 2013 Daniel Golden

### Daniel Golden (view profile)

Works perfectly. Excellent work!

13 Jul 2011 Daniel

### Daniel (view profile)

WOW! That is exactly what I have been looking for! The code seems to be robust and worked perfect for my needed cases. Thanks!