2012-05-30 60 views
0

我一直在想要在网上寻找我想要的东西,但我没有太多的运气,所以我想我会问这里。Matlab:颜色编码一个3D剧情

是否可以用不同的颜色精确定位并在图上显示两个图之间有交点的点?

感谢您提供任何帮助。

下面是代码:

file1 = fopen('C:\Program Files (x86)\Notepad++\avatar1.txt'); % open text file 
file2 = fopen('C:\Program Files (x86)\Notepad++\avatar2.txt'); % open text file 
file3 = fopen('C:\Program Files (x86)\Notepad++\avatar3.txt'); % open text file 

tline1 = fgetl(file1); % read line by line and remove new line characters 
tline2 = fgetl(file2); % read line by line and remove new line characters 
tline3 = fgetl(file3); % read line by line and remove new line characters 

% declare empty arrays 
CX1 = []; 
CY1 = []; 
CZ1 = []; 

CX2 = []; 
CY2 = []; 
CZ2 = []; 

CX3 = []; 
CY3 = []; 
CZ3 = []; 


while ischar(tline1) % true if tline is a character array 
    temp = cell2mat(textscan(tline1, '<%n,%n,%n>')); 

    % convert all the cell fields to a matrix 
    CX1 = vertcat(CX1, temp(1)); 
    CY1 = vertcat(CY1, temp(2)); 
    CZ1 = vertcat(CZ1, temp(3)); 

    tline1 = fgetl(file1); 
end 

while ischar(tline2) % true if tline is a character array 
    temp = cell2mat(textscan(tline2, '<%n,%n,%n>')); 

    % convert all the cell fields to a matrix 
    CX2 = vertcat(CX2, temp(1)); 
    CY2 = vertcat(CY2, temp(2)); 
    CZ2 = vertcat(CZ2, temp(3)); 

    tline2 = fgetl(file2); 
end 

while ischar(tline3) % true if tline is a character array 
    temp = cell2mat(textscan(tline3, '<%n,%n,%n>')); 

    % convert all the cell fields to a matrix 
    CX3 = vertcat(CX3, temp(1)); 
    CY3 = vertcat(CY3, temp(2)); 
    CZ3 = vertcat(CZ3, temp(3)); 

    tline3 = fgetl(file3); 
end 

fclose(file1); % close the file 
fclose(file2); % close the file 
fclose(file3); % close the file 

plot3(CX1, CY1, CZ1) % plot the data and label the axises 
plot3(CX2, CY2, CZ2) 
plot3(CX3, CY3, CZ3) 
xlabel('x') 
ylabel('y') 
zlabel('z') 
grid on 
axis square 
rotate3d on; % activate interactive mouse rotation 
+0

我不完全理解你的问题 - 你想用一种颜色绘制一个图形,用另一种颜色叠加另一个图形,然后突出显示相交点? – n00dle

+0

是的,但它是3张图。所以3种不同的颜色会突出显示或精确定位交叉点。 –

+0

您是否在问如何在交叉点中放置标记......或者如何确定是否存在交叉点,以及如果存在,它们在哪里? – tmpearce

回答

0

改变颜色很简单,这只是增加了颜色代码的plot3命令,例如一个案例:

plot3(CX1, CY1, CZ1, 'b'); % blue lines/markers 
plot3(CX2, CY2, CZ2, 'r'); % red lines/markers 
plot3(CX3, CY3, CZ3, 'g'); % green lines/markers 

有关颜色的详细信息代码,请参阅Matlab Colourspec Page

根据是否想要点的交点(即出现在所有3个数据集中的特定点)或加入点的线的交点,交点可能有点棘手。

我认为前者应该是相当容易(这是未经测试,并假设CX1等都是垂直向量):

figure; % Open up a new figure 
hold on; % This means the everything you plot stays in the figure and is not overwritten 

% Plot the original points 
plot3(CX1, CY1, CZ1, '-*b'); % blue lines/markers 
plot3(CX2, CY2, CZ2, '-*r'); % red lines/markers 
plot3(CX3, CY3, CZ3, '-*g'); % green lines/markers 

% turn those 1xn vectors into 3xn matrices for each set of points 
points1 = [CX1, CY1, CZ1]; 
points2 = [CX2, CY2, CZ2]; 
points3 = [CX3, CY3, CZ3]; 

% Find the intersection of the 3 sets 
CX_intersect = intersect(points1, intersect(points2, points3, 'rows'), 'rows'); 

% Draw a scatter plot of the intersection points. the 'mo' means: 
% m: magenta in colour, o: circular markers 
scatter3(CX_intersect(:,1),CX_intersect(:,2),CX_intersect(:,3),'mo'); 

交集的工作原理是这样:

说我们有3点矩阵,每个包含多个3d点。我们称之为ABC

要找到所有3组之间的交集,我们首先找到仅在AB中相交的点。我们现在有一组我们知道的点数在AB,所以现在我们只需要检查这些点是否也在C之内。我们通过做另一个交点来做到这一点

我只是把这些连在一起成一行代码,这可能不是很有用,所以我很抱歉。为ABC交叉口的代码如下:

D = intersect(A,B,'rows') % we use rows because each row represents a 3D point 
E = intersect(C,D,'rows') % E is the intersection of the 3 sets. 

然后我们就可以代替d进线E = ...,我们得到:

E = intersect(intersect(A,B,'rows'), C, 'rows'); 

希望帮助!

+0

这就是我以为你改变颜色,但你的代码只是把它们全部变成青柠色......至于交叉点,我想找到3个矩阵之间的任何交点,并且如果存在交叉点,用不同的颜色精确定位并显示交叉点的矢量值 –

+0

我不确定现在发生了什么,但是您的交点工作正常该图完全消失?另外,你知道他们为什么只变成1柠檬绿色吗? –

+0

我已经编辑我的示例到一个更完整的。我想,因为你没有使用'hold on',所以只显示最后一个'plot3'调用(默认情况下,Matlab在绘制新的绘图之前删除旧的绘图)。这将解释为什么他们都是绿色的。 交叉点在问题中将更容易解释,所以我现在编辑它。 – n00dle