Created
May 1, 2021 21:23
-
-
Save johanlofberg/1fa43402d071790d8a622f542a5bdb27 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
n = 6; | |
th = linspace(-pi, pi, 2*n+1); | |
xi = (1+rem((1:2*n+1),2)).*cos(th); | |
yi = (1+rem((1:2*n+1),2)).*sin(th); | |
clf; | |
hold on; | |
grid on | |
plot( xi, yi, 'b*-' ); | |
axis equal; | |
sdpvar x y | |
lambda = sdpvar(length(xi),1) | |
Model = [sos2(lambda), lambda>=0, sum(lambda)==1, | |
x == xi*lambda, y == yi*lambda]; | |
plot(Model,[x;y],[],[],sdpsettings('plot.shade',.1)) | |
optimize(Model, (x-1.5)^2 + (y-1)^2) | |
plot(1.5,1,'*r') | |
plot(value(x),value(y),'ok') | |
sdpvar x y | |
lambda = sdpvar(length(xi),1) | |
Model = [sos2(lambda), lambda>=0,sum(lambda)<=1, | |
x == xi*lambda, y == yi*lambda]; | |
n = 6; | |
th = linspace(-pi, pi, 2*n+1); | |
xi = 1+(1+rem((1:2*n+1),2)).*cos(th); | |
yi = 2+(1+rem((1:2*n+1),2)).*sin(th); | |
clf; | |
hold on; | |
grid on | |
plot( xi, yi, 'b*-' ); | |
axis equal; | |
xc = mean(xi(1:end-1)); | |
yc = mean(yi(1:end-1)); | |
lambda = sdpvar(length(xi),1) | |
Model = [sos2(lambda), lambda>=0,sum(lambda)<=1, | |
x == xc + (xi-xc)*lambda, | |
y == yc + (yi-yc)*lambda]; | |
plot(Model,[x;y],[],[],sdpsettings('plot.shade',.1)) | |
xi = [1 2 2 3 3 4 4 1 1]; | |
yi = [1 1 5 5 1 1 0 0 1]; | |
clf | |
hold on; | |
grid on | |
plot( xi, yi, 'b*-' ); | |
plot( xi-mean(xc), yi-mean(yc), 'r*--' ); | |
axis equal; | |
n = 6; | |
th = linspace(-pi, pi, 2*n+1); | |
xi = 1+(1+rem((1:2*n+1),3)).*cos(th).^3; | |
yi = 2+(1+rem((1:2*n+1),2)).*sin(th); | |
clf; | |
hold on; | |
grid on | |
plot( xi, yi, 'b*-' ); | |
axis equal; | |
sdpvar x y | |
Model = starpolygon(xi,yi,[x;y],[],[],[1;2]); | |
plot(Model,[x;y],[],[],sdpsettings('plot.shade',.1)) | |
Model = starpolygon(xi,yi,[x;y],[],.25,[1;2]); | |
plot(Model,[x;y],'b') | |
Model = starpolygon(xi,yi,[x;y],[],0.25,[1;1]); | |
plot(Model,[x;y],'g') | |
Model = starpolygon(xi,yi,[x;y],[-4;0],[],[1;2]); | |
plot(Model,[x;y],'y') | |
% Define the template star | |
n = 6; | |
th = linspace(-pi, pi, 2*n+1); | |
xs = (1+rem((1:2*n+1),2)).*cos(th); | |
ys = (1+rem((1:2*n+1),2)).*sin(th); | |
clf | |
hold on; | |
grid on | |
plot( xs, ys, 'b*-' ); | |
axis equal; | |
% Create the point-cloud | |
thn = linspace(-pi, pi, 2*3*n+1); | |
xn = 3+interp1(th,xs,thn)+.01*randn(1,2*3*n+1) | |
yn = 2+interp1(th,ys,thn)+.01*randn(1,2*3*n+1) | |
plot( xn, yn, 'r*' ); | |
% Now say that each point in point-cloud is in scaled and translated polygon | |
sdpvar t | |
c = sdpvar(2,1); | |
Model = []; | |
for i = 1:length(xn) | |
Model = [Model, starpolygon(xs,ys,[xn(i);yn(i)],c,t)]; | |
end | |
% Minimize scale factor | |
optimize(Model,t) | |
% ...and plot the scaled and translated star | |
plot( value(t)*xs+value(c(1)), value(t)*ys+value(c(2)), 'k*-' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment