Matlab code for generating some shapes using signed distance
Show older comments
In order to get a good understanding of my question, please, you may first run the matlab code below. This code can generate the shape for rectangle, ellipse and circle using signed distance if you uncomment the portion corresponding to each shape.
Please, I need a code that can give the shapes in the attached picture (Picture_1.jpg) using signed distance. Thank you so much.
%%%Signed distance code for generating shapes
clear all;
close all;
clc;
DomainWidth=2;
DomainHight=1;
ENPC=40;
ENPR=80;
EW = DomainWidth / ENPR; % The width of each finite element.
EH = DomainHight / ENPC; % The hight of each finite element.
M = [ ENPC + 1 , ENPR + 1 ];
[ x,y ] = meshgrid( EW * [ -0.5 : ENPR + 0.5 ] , EH * [ -0.5 : ENPC + 0.5 ]);
[ FENd.x, FENd.y, FirstNdPCol] = MakeNodes(ENPR,ENPC,EW,EH);
LSgrid.x = x(:); LSgrid.y = y(:); % The coordinates of Level Set grid 1
cx = DomainWidth/2;
cy= DomainHight/2;
a = cx;
b = 0.8*cy;
%%Generate circle
tmpPhi= sqrt ( ( LSgrid . x - cx ) .^2 + ( LSgrid . y - cy ) .^2 ) - DomainHight/2;
LSgrid.Phi = -((tmpPhi.')).';
%%Generate ellipse
% tmpPhi= ( (LSgrid . x - cx)/a ) .^2 + (( LSgrid . y - cy)/(0.8*cy) ) .^2 - 1;
% LSgrid.Phi = -((tmpPhi.')).';
%%Generate rectangle
% lower = [cx - 0.5 * DomainWidth, cy - 0.25 * DomainHight];
% upper = [cx + 0.5 * DomainWidth, cy + 0.25 * DomainHight];
% Phi11 =max(LSgrid.x - upper(1), lower(1) - LSgrid . x );
% Phi11 =max(Phi11,LSgrid.y - upper(2));
% Phi11 =max(Phi11,lower(2) - LSgrid . y );
% LSgrid.Phi = -Phi11;
FENd.Phi = griddata( LSgrid.x, LSgrid.y, LSgrid.Phi, FENd.x, FENd.y, 'cubic');
figure(10)
% Figure of the full contour plot of the signed distance of the shape
contourf( reshape( FENd.x, M), reshape(FENd.y , M), reshape(FENd.Phi, M));
axis equal; grid on;drawnow; colorbar;
figure(11)
% Figure of the scaled contour plot showing only the shape
contourf( reshape( FENd.x, M), reshape(FENd.y , M), reshape(FENd.Phi, M), [0 0] );
axis equal; grid on;drawnow; colorbar;
figure(12)
%Figure of the signed distance function
h3=surface(x, y, reshape(-LSgrid.Phi , M + 1)); view([37.5 30]); axis equal; grid on;
set(h3,'FaceLighting','phong','FaceColor','interp', 'AmbientStrength',0.6); light('Position',[0 0 1],'Style','infinite'); colorbar;
%%Code for creating nodes
function [NodesX, NodesY, FirstNdPCol] = MakeNodes(EleNumPerRow,EleNumPerCol,EleWidth,EleHight)
[ x , y ]= meshgrid( EleWidth * [ 0 : EleNumPerRow ], EleHight * [0 : EleNumPerCol]);
FirstNdPCol = find( y(:) == max(y(:)));
NodesX = x(:); NodesY = y(:);
end
6 Comments
jonas
on 13 Oct 2018
What is the purpose of this? Do you just want to draw the shapes? If so, better use polyshape.
SAMUEL AYINDE
on 13 Oct 2018
jonas
on 13 Oct 2018
Well, it's not clear from your figure. You should rather show this figure:

SAMUEL AYINDE
on 13 Oct 2018
Edited: SAMUEL AYINDE
on 13 Oct 2018
jonas
on 13 Oct 2018
Not sure if I can, but I will give it a try. Anyway, if the question is clear then usually someone will answer eventually :)
SAMUEL AYINDE
on 13 Oct 2018
Accepted Answer
More Answers (1)
SAMUEL AYINDE
on 23 Apr 2019
0 votes
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

