How to get the standard line equation from a position and angle
2 ビュー (過去 30 日間)
古いコメントを表示
Hi all,
I want to compute the standard line equation of the form
in a 2D meshgrig within the positive domain
given the starting point
and an angle
. The constraint I want to impose on this equation is that the line should hit the next boundary on the mesh. Here is what I do to calculate the equations cofficients:





The problem with my code is that the line takes negative values (outside the domain).
close all;
clear all;
%Step 1: Define position and angle variables
%position variable (s:x,y)
smin = 0; %boundary minimum length
L = 1; %boundary maximum length
ns = 25; %number of points on S axis
s = linspace(smin,L,ns); %boundary variable
%angle variable (θ,p)
theta_min = pi/4; %minimum angle
theta_max = pi/2; %maximum angle
nt = 25; %number of angles steps
theta = linspace(theta_min,theta_max,nt); %angle variable
%-----
%Step 2: construct the meshgrid
[X,Y] = meshgrid(s,s);
% mesh needs X,Y and Z so create z
Z = zeros(size(X));
%Visualise the grid
figure;
mesh(X,Y,Z,'Marker','o','EdgeColor',"k") %or surf
axis equal tight
box on
view(2)
set(gca,'ytick',[])
xlabel('$S$','Interpreter','latex')
set(gca,'TickLabelInterpreter','latex')
set(gca,'FontSize',16)
hold on
%-----
%Step 3:
%initial position (s0)
x0 = s(randi(length(s)));
y0 = 0; %the bottom edge
if x0 == y0
x0 = s(randi(length(s))); %the bottom edge
end
%initial angle (θ0)
theta0 = theta(randi(length(theta)));
%-----
%Step 4: Construct the first line equation from the position and direction
%cofficients
A = cos(theta0);
B = sin(theta0);
C = -x0*cos(theta0)-y0*sin(theta0);
%line equation
L1 = (-C/B) + (-A/B) * s;
%plotting the line on the grid
plot(s,L1,'m','LineWidth',2)
axis([min(s) max(s) min(s) max(s)])
%-----
Any help would be apprecited.
Thanks.
0 件のコメント
回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!