我有以下代码,其中有两行(L1和L2),我可以找到这些行之间的交点。然而,L1永远不会改变,但L2会改变。这就是我遇到问题的地方。我有一个带有x和y坐标列表的.txt文件。如何更改代码,以便我可以将L2中的数字660和122.75替换为列表中的前两个数字。然后保存交叉点并重新开始,但这次用我列表中的第二组数字替换这两个数字。
此致 耶
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 :(得分: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