How to get the standard line equation from a position and angle
2 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Raag
on 20 Feb 2025
Hi Lama,
To compute the standard line equation from a position and angle in a 2D mesh, we need to ensure the line stays within the domain. Here's an example of how can we modify your code:
First we need to use a parametric form for the line:
% Parameter t starts at 0
x = @(t) x0 + t * cos(theta0);
y = @(t) y0 + t * sin(theta0);
Next, calculate t for intersections with the boundaries (x = 0), (x = 1), (y = 0), and (y = 1). Handle zero values for cos and sin to avoid division by zero:
% Initialize t_min and t_max
t_min = 0;
t_max = Inf;
% Check for vertical line (cos(theta0) = 0)
if cos(theta0) ~= 0
t_x0 = (0 - x0) / cos(theta0);
t_x1 = (1 - x0) / cos(theta0);
t_min = max(t_min, min(t_x0, t_x1));
t_max = min(t_max, max(t_x0, t_x1));
end
% Check for horizontal line (sin(theta0) = 0)
if sin(theta0) ~= 0
t_y0 = (0 - y0) / sin(theta0);
t_y1 = (1 - y0) / sin(theta0);
t_min = max(t_min, min(t_y0, t_y1));
t_max = min(t_max, max(t_y0, t_y1));
end
Next we need to plot the line segment only within the valid (t) range:
tvals = linspace(t_min, t_max, 50);
xvals = x(tvals);
yvals = y(tvals);
plot(xvals, yvals, 'm', 'LineWidth', 2);
By following these steps, you ensure the line remains within the specified domain and handle potential division by zero issues.
For further details, you can refer to MATLAB documentation on functions like:
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!