在 Matlab 中用方程画线
draw line with Equation in Matlab
此代码生成半径为 r 的圆。是否可以通过将圆方程更改为线方程来创建具有特定角度和长度的线?如果这是可行的,方程式应该是什么?
请指导
clc;clear;
mask = zeros(400,600);
position = [200,300];
r = 50;
cx = position(1);
cy = position(2);
[ix,iy] = size(mask);
[x,y]= meshgrid(-(cx-1):(ix-cx),-(cy-1):(iy-cy));
circlemask =((x.^2+y.^2)<=r^2)';
您可以按如下方式关注:
首先参考这个 link 以了解如何在给定斜率的情况下获得直线的一端和直线的一端。
其次,代码需要找到一些交点,为此我使用了来自这个 link 的 MATLAB 文件交换函数 InterX。
检查下面的代码:
mask = zeros(400,600);
% mesh grid for the zone
[X,Y]= meshgrid(1:600,1:400) ;
L1 = [X(:) Y(:)] ;
% Given line details
A = [200,300]; % one end of line
th = 45 ; % slope of line in degrees
m = tand(th) ; % slope of the line
d = 100 ; % Length of the line we want
% get the other end of line
x = [A(1)+ d*sqrt(1/(1+m^2)) A(1)- d*sqrt(1/(1+m^2))] ;
y = [A(2)+ m*d*sqrt(1/(1+m^2)) A(2)- m*d*sqrt(1/(1+m^2))] ;
B = [x(1) y(1)] ;
% Get nearest neighbors of points of the line in mask
idx1 = knnsearch(L1,A) ;
idx2 = knnsearch(L1,B) ;
% Get intersection points
L2 = [[A(1) x(1)]' [A(2) y(1)]']' ; % take A and B points
P = InterX(L1',L2) ;
idx = knnsearch(L1,P') ;
mask(idx) = 1 ;
imshow(mask)
代码的输出如下所示:
此代码生成半径为 r 的圆。是否可以通过将圆方程更改为线方程来创建具有特定角度和长度的线?如果这是可行的,方程式应该是什么? 请指导
clc;clear;
mask = zeros(400,600);
position = [200,300];
r = 50;
cx = position(1);
cy = position(2);
[ix,iy] = size(mask);
[x,y]= meshgrid(-(cx-1):(ix-cx),-(cy-1):(iy-cy));
circlemask =((x.^2+y.^2)<=r^2)';
您可以按如下方式关注:
首先参考这个 link 以了解如何在给定斜率的情况下获得直线的一端和直线的一端。
其次,代码需要找到一些交点,为此我使用了来自这个 link 的 MATLAB 文件交换函数 InterX。
检查下面的代码:
mask = zeros(400,600);
% mesh grid for the zone
[X,Y]= meshgrid(1:600,1:400) ;
L1 = [X(:) Y(:)] ;
% Given line details
A = [200,300]; % one end of line
th = 45 ; % slope of line in degrees
m = tand(th) ; % slope of the line
d = 100 ; % Length of the line we want
% get the other end of line
x = [A(1)+ d*sqrt(1/(1+m^2)) A(1)- d*sqrt(1/(1+m^2))] ;
y = [A(2)+ m*d*sqrt(1/(1+m^2)) A(2)- m*d*sqrt(1/(1+m^2))] ;
B = [x(1) y(1)] ;
% Get nearest neighbors of points of the line in mask
idx1 = knnsearch(L1,A) ;
idx2 = knnsearch(L1,B) ;
% Get intersection points
L2 = [[A(1) x(1)]' [A(2) y(1)]']' ; % take A and B points
P = InterX(L1',L2) ;
idx = knnsearch(L1,P') ;
mask(idx) = 1 ;
imshow(mask)
代码的输出如下所示: