convert line to hesse normal form and plot it in matlab

To convert a line to Hesse normal form, you need to rewrite its equation in the form r * cos(theta) + s * sin(theta) = d, where r and s are the perpendicular distances from the origin to the line, theta is the angle between the x-axis and the line, and d is the signed distance from the origin to the line.

Here's an example code block that takes in the slope-intercept form of a line (y = mx + b) and converts it to Hesse normal form:

main.m
% Input values
m = 2; % slope
b = 5; % y-intercept

% Convert to Hesse normal form
theta = atan(-m); % angle between x-axis and line
r = b / cos(theta); % perpendicular distance from origin to line
s = b / sin(theta); % perpendicular distance from origin to line
d = r * cos(theta); % signed distance from origin to line
hnf = [r, s, d]; % Hesse normal form [r, s, d] 

% Plot the line
x = linspace(-10, 10); % create range of x values
y = m * x + b; % calculate corresponding y values
plot(x, y, 'LineWidth', 2) % plot the line
hold on % hold current plot
x = linspace(-10, 10); % create range of x values
y = (d - r*cos(theta))/sin(theta)*ones(1,length(x)); % calculate corresponding y values for Hesse normal form
plot(x, y, '--', 'LineWidth', 2) % plot the line in Hesse normal form
axis equal % set aspect ratio to 1:1
824 chars
21 lines

This code block will plot the original line in blue, and the line in Hesse normal form in red dashed line. Note that the axis equal command is used to set the aspect ratio to 1:1, which ensures that the line is plotted correctly.

gistlibby LogSnag