2016-04-27 115 views
0

我有以下代碼,我有兩條線(L1和L2),我可以找到線之間的交點。然而,L1永遠不會改變,但L2會。這是我遇到問題的地方。我有一個帶有x和y座標列表的.txt文件。如何修改代碼,以便我可以將L2中的數字660和122.75換成列表中的前兩個數字。然後保存交叉點並重新開始,但這次用列表中的第二組數字替換這兩個數字。更改線座標matlab

問候, 耶

clc; 
clear; 

%% Load data 

filename = 'C:\blade1_data.txt'; 
delimiter = ' '; 
formatSpec = '%f%f%[^\n\r]'; 
fileID = fopen(filename,'r'); 
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'MultipleDelimsAsOne', true, 'ReturnOnError', false); 
fclose(fileID); 
X_coords = dataArray{:, 1}; 
Y_coords = dataArray{:, 2}; 
clearvars filename delimiter formatSpec fileID dataArray ans; 

%% Find intersection between lines 

L1 = [656.25 122.75; 611.625 378.875]; % Defines rotor line 
L2 = [0 122.75; 660 122.75]; % Defines coordinate where blade tip was located 


dx = diff(L1); %# Take the differences down each column 
dy = diff(L2); 
den = dx(1)*dy(2)-dy(1)*dx(2); %# Precompute the denominator 
ua = (dx(2)*(L2(1)-L2(3))-dy(2)*(L1(1)-L1(3)))/den; 
ub = (dx(1)*(L2(1)-L2(3))-dy(1)*(L1(1)-L1(3)))/den; 
% Intersection point of the two lines: 

xi = L1(1)+ua*dx(1); 
yi = L2(1)+ua*dy(1); 

回答

0

工作代碼

clc; 
    clear; 

%% Load data 

filename = 'C:data.txt'; 
delimiter = ' '; 
formatSpec = '%f%f%[^\n\r]'; 
fileID = fopen(filename,'r'); 
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'MultipleDelimsAsOne', true, 'ReturnOnError', false); 
fclose(fileID); 
X_coords = dataArray{:, 1}; 
Y_coords = dataArray{:, 2}; 
clearvars filename delimiter formatSpec fileID dataArray ans; 

%% Find intersection between lines 
nfiles = length(X_coords); 

for n = 1: nfiles; 
    ptx = X_coords(n:n,1); 
    pty = Y_coords(n:n,1); 
L1 = [656.25 122.75; 611.625 378.875]; % Defines rotor line 
L2 = [0 pty; ptx pty]; % Defines coordinate where blade tip was located 


dx = diff(L1); %# Take the differences down each column 
dy = diff(L2); 
den = dx(1)*dy(2)-dy(1)*dx(2); %# Precompute the denominator 
ua = (dx(2)*(L2(1)-L2(3))-dy(2)*(L1(1)-L1(3)))/den; 
ub = (dx(1)*(L2(1)-L2(3))-dy(1)*(L1(1)-L1(3)))/den; 
% Intersection point of the two lines: 

xi = L1(1)+ua*dx(1); 
yi = L2(1)+ua*dy(1); 

data(n,:) = [xi yi]; 
end