• Remix
  • Share
  • New Entry

on 26 Oct 2021
  • 7
  • 75
  • 0
  • 0
  • 278
% 3D version of a hypotrochoid using polyshape & triangulations.
% The API for polyshape and triangulation include a lot of methods
% and properties that are very long. This required a lot of
% crunching to make it fit.
% These are the original constants from the first version of this
% script. I used constant folding and some other tricks to
% eliminate a lot of characters from the code.
%A=.2; B=.7; C=.5; L=7;
%A=2;
%B=9;
%C=7;
%L=9;
%d=linspace(0, L*2*pi, 500);
d=(0:.001:1)*pi*18;
% X,Y differ only in the trig fcn used, so use an anon fcn handle,
% then inline X,Y into the polyshape call.
g=@(h)7*h(d)-h(d*2/9)*7;
% Use polyshape to convert the single 'face' into a set of known
% boundaries we can use in a 3d triangulation.
t=@triangulation;
T=t(polyshape(g(@cos),g(@sin)));
Warning: Polyshape has duplicate vertices, intersections, or other inconsistencies that may produce inaccurate or unexpected results. Input data has been modified to create a well-defined polyshape.
P=T.Points;
F=T.ConnectivityList;
E=T.freeBoundary;
% L = size(P,1)
L=1056;
% The points used in the 3d triangulation are just the og points
% repeating, but at different Z levels. Use trick to fill in Z
% with 0 with no code at same time we fill half the Zs with 2.
j=[P
P];
j(1:L,3)=2;
% Use fcn handles to shorten edge magic.
% The wierd carriage returns/transpose trick saves 1 char per 3
% element array, or 2 chars when transpose optional.
e=@(l,m)E(1:1112,[l
1
2])+[0
m
L]';
% This is what the above fcn handle replaces.
% E(R,[2 1 2])+[0 0 L];
% E(R,[1 1 2])+[0 L L]]
% Generate the final polygon as a triangulation.
V=t([F
F+L
e(2,0)
e(1,L)],j);
% Draw the 3d triangulation
trisurf(V,hypot(j(:,1),j(:,2)))
shading interp
axis off equal
%colormap hsv % Sad - I would have liked this
camlight
Remix Tree