2012-05-30 64 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