How do I make a scatter 3D plot move towards a line

조회 수: 1 (최근 30일)
sui zhi lau
sui zhi lau 2020년 5월 10일
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');

답변 (1개)

Thiago Henrique Gomes Lobato
Thiago Henrique Gomes Lobato 2020년 5월 10일
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
sui zhi lau
sui zhi lau 2020년 5월 10일
Ah so if I want to tilit it in the y direction, it would be seomthing like this?
because i tried it but i could not get it to run. Like they said it was undefined for the Zposition, I do not know what am I doing wrong.
halfLength = 10;
Ypositions = max(Y)/2-halfLength:max(Y)/2+halfLength;
Xposition = max(X)/2;
Zposition = max(Z)/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;
Thiago Henrique Gomes Lobato
Thiago Henrique Gomes Lobato 2020년 5월 10일
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;

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Pseudorandom and Quasirandom Number Generation에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by