How do I make a scatter 3D plot move towards a line
Show older comments
would it be possible to make a moving plot for these scatter points to move towards a line in the middle of the 3d plot and stop when it reaches the line?
a rough example would be:

clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
countmax=5; % units are in micrometres
view([-37.5 30])
X = zeros(1,countmax);
Y = zeros(1,countmax);
Z = zeros(1,countmax);
n=0;
m=0;
y=1;
d = 10;
%d = 15/1000; %distance between each molecule => 15/1000 is 15 micrometre
s = 6; % size of particle
for i = 2:countmax
for j = 2:countmax
for k = 2:countmax
X(i) = X(i-1) + d; %%1 = distance between each molecules (add an equation for it)
Y(j) = Y(j-1) + d;
Z(k) = Z(k-1) + d;
end
end
end
q= 1;
for n = 1:countmax
for m = 1:countmax
b = repelem(X(n),countmax);
c = repelem(Y(m),countmax);
a = [b;c;Z]; %% Check Coordinates
scatter3(a(1,:),a(2,:),a(3,:),s,'Filled','r') %% 4 value from left changes the size of each point
%xlim([d (countmax-1)*d]) % Axis limitsg
%ylim([d (countmax-1)*d])
%zlim([d (countmax-1)*d])
xlabel('My x label') % Axis Labels
ylabel('My y label')
zlabel('My z label')
hold on
rotate3d on
end
end
grid off;
Image = getframe();
K = sum(sum(rgb2gray(Image.cdata)==255));
percentageofwhite(y) = K/numel(rgb2gray(Image.cdata))*100;
q = percentageofwhite(y);
%delete(findall(gcf,'type','text'))
txt=text(0.0,0.95,sprintf('Water Volume = %0.3f%%',q(1,1)),'Units','normalized');
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
countmax=5; % units are in micrometres
view([-37.5 30])
X = zeros(1,countmax);
Y = zeros(1,countmax);
Z = zeros(1,countmax);
n=0;
m=0;
y=1;
d = 10;
%d = 15/1000; %distance between each molecule => 15/1000 is 15 micrometre
s = 6; % size of particle
for i = 2:countmax
for j = 2:countmax
for k = 2:countmax
X(i) = X(i-1) + d; %%1 = distance between each molecules (add an equation for it)
Y(j) = Y(j-1) + d;
Z(k) = Z(k-1) + d;
end
end
end
q= 1;
for n = 1:countmax
for m = 1:countmax
b = repelem(X(n),countmax);
c = repelem(Y(m),countmax);
a = [b;c;Z]; %% Check Coordinates
scatter3(a(1,:),a(2,:),a(3,:),s,'Filled','r') %% 4 value from left changes the size of each point
%xlim([d (countmax-1)*d]) % Axis limitsg
%ylim([d (countmax-1)*d])
%zlim([d (countmax-1)*d])
xlabel('My x label') % Axis Labels
ylabel('My y label')
zlabel('My z label')
hold on
rotate3d on
end
end
grid off;
Image = getframe();
K = sum(sum(rgb2gray(Image.cdata)==255));
percentageofwhite(y) = K/numel(rgb2gray(Image.cdata))*100;
q = percentageofwhite(y);
%delete(findall(gcf,'type','text'))
txt=text(0.0,0.95,sprintf('Water Volume = %0.3f%%',q(1,1)),'Units','normalized');
Answers (1)
Thiago Henrique Gomes Lobato
on 10 May 2020
It mostly depends of what points should move (only neighbours or all of them), but after you decide it you can calculate the closest point in the line for each point and then move it lineary in that direction. Here is an example moving all points to a line defined by three points
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
countmax=5; % units are in micrometres
view([-37.5 30])
X = zeros(1,countmax);
Y = zeros(1,countmax);
Z = zeros(1,countmax);
n=0;
m=0;
y=1;
d = 10;
%d = 15/1000; %distance between each molecule => 15/1000 is 15 micrometre
s = 6; % size of particle
for i = 2:countmax
for j = 2:countmax
for k = 2:countmax
X(i) = X(i-1) + d; %%1 = distance between each molecules (add an equation for it)
Y(j) = Y(j-1) + d;
Z(k) = Z(k-1) + d;
end
end
end
q= 1;
for n = 1:countmax
for m = 1:countmax
b = repelem(X(n),countmax);
c = repelem(Y(m),countmax);
a = [b;c;Z]; %% Check Coordinates
scatter3(a(1,:),a(2,:),a(3,:),s,'Filled','r') %% 4 value from left changes the size of each point
%xlim([d (countmax-1)*d]) % Axis limitsg
%ylim([d (countmax-1)*d])
%zlim([d (countmax-1)*d])
xlabel('My x label') % Axis Labels
ylabel('My y label')
zlabel('My z label')
hold on
rotate3d on
end
end
grid off;
Image = getframe();
K = sum(sum(rgb2gray(Image.cdata)==255));
percentageofwhite(y) = K/numel(rgb2gray(Image.cdata))*100;
q = percentageofwhite(y);
%delete(findall(gcf,'type','text'))
txt=text(0.0,0.95,sprintf('Water Volume = %0.3f%%',q(1,1)),'Units','normalized');
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
countmax=5; % units are in micrometres
view([-37.5 30])
X = zeros(1,countmax);
Y = zeros(1,countmax);
Z = zeros(1,countmax);
n=0;
m=0;
y=1;
d = 10;
%d = 15/1000; %distance between each molecule => 15/1000 is 15 micrometre
s = 6; % size of particle
for i = 2:countmax
for j = 2:countmax
for k = 2:countmax
X(i) = X(i-1) + d; %%1 = distance between each molecules (add an equation for it)
Y(j) = Y(j-1) + d;
Z(k) = Z(k-1) + d;
end
end
end
q= 1;
[XX,YY,ZZ] = meshgrid(X,Y,Z);
AllDimensions = [XX(:),YY(:),ZZ(:)];
LinePoints = [30,10,20;
30,20,20;
30,30,20;
30,40,20];
% Find which points should be directed to where
LinePointMap = zeros(length(AllDimensions),1);
for idx=1:length(AllDimensions)
[~,idxMin] = min( sum(abs(AllDimensions(idx,:)-LinePoints).^2,2) );
LinePointMap(idx) = idxMin;
end
Differences = LinePoints(LinePointMap,:)-AllDimensions;
MaxStep = 50;
for idxAnimation = 1:MaxStep
scatter3(AllDimensions(:,1)+idxAnimation/MaxStep*Differences(:,1),...
AllDimensions(:,2)+idxAnimation/MaxStep*Differences(:,2),...
AllDimensions(:,3)+idxAnimation/MaxStep*Differences(:,3),...
s,'Filled','r') %% 4 value from left changes the size of each point
xlim([d (countmax-1)*d]) % Axis limitsg
ylim([d (countmax-1)*d])
zlim([d (countmax-1)*d])
xlabel('My x label') % Axis Labels
ylabel('My y label')
zlabel('My z label')
hold off
rotate3d on
grid off;
pause(0.1)
end
Image = getframe();
K = sum(sum(rgb2gray(Image.cdata)==255));
percentageofwhite(y) = K/numel(rgb2gray(Image.cdata))*100;
q = percentageofwhite(y);
%delete(findall(gcf,'type','text'))
txt=text(0.0,0.95,sprintf('Water Volume = %0.3f%%',q(1,1)),'Units','normalized');
6 Comments
sui zhi lau
on 10 May 2020
Thiago Henrique Gomes Lobato
on 10 May 2020
Edited: Thiago Henrique Gomes Lobato
on 10 May 2020
What version of matlab do you have? Some old versions don't perform the implicity expansion. Change to this line and it should work
[~,idxMin] = min( sum(abs( repmat(AllDimensions(idx,:),size(LinePoints,1),1)-LinePoints).^2,2) );
Also, in my example I use three points, because your line in the example was just a segment in the middle. If you want a parametric line the distance that you should minimize is not the distance to point but the distance to a parametric line, for reference you can check here.
sui zhi lau
on 10 May 2020
Thiago Henrique Gomes Lobato
on 10 May 2020
Meshgrid will not allow you to create a line like that. Imagine a line in +X, the values of Y and Z remains constant and only X change. The same if the line is in Y and Z. If you have no inclination on your line you will only need to change one coordinates (it will be more depending of how you want to tillt it). So, suppose you want to make a line in Z, the result will be something like this:
halfLength = 10;
Zpositions = max(Z)/2-halfLength:max(Z)/2+halfLength;
Xposition = max(X)/2; Yposition = max(Y)/2;
LinePoints = zeros(length(Zpositions),3);
LinePoints(:,3) = Zpositions;
LinePoints(:,1) = Xposition; % if it doesn't work, = repmat(Xposition,length(Zpositions),1)
LinePoints(:,2) = Yposition;
sui zhi lau
on 10 May 2020
Thiago Henrique Gomes Lobato
on 10 May 2020
Your variable names are wrong, i.e, you used different variable names in each step
halfLength = 10;
Ypositions = max(Y)/2-halfLength:max(Y)/2+halfLength;
Xposition = max(X)/2;
Zposition = max(Z)/2;
LinePoints = zeros(length(Ypositions),3);
LinePoints(:,3) = Zposition;
LinePoints(:,1) = Xposition; % if it doesn't work, = repmat(Xposition,length(Zpositions),1)
LinePoints(:,2) = Ypositions;
Categories
Find more on Data Distribution 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!