-
Notifications
You must be signed in to change notification settings - Fork 0
/
getLineProperties.m
executable file
·68 lines (56 loc) · 2.07 KB
/
getLineProperties.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
% USAGE:
% pass in 2 points in the following format [x y]
% getLineProperties([x1 y1], [x2 y2])
% it has been formatted this way because many matlab matrix store points in
% this format so the following could also work
% getLineProperties(point1, point2)
%
% RETURNS:
% It returns an array of lineProperties that have the following attributes
% available to you
% lineProperties.slope
% lineProperties.inverseSlope (aka, perpendicular slope)
% lineProperties.xIntercept
% lineProperties.yIntercept
% lineProperties.midPoint (this is an array with points [x y])
% access it like so: lineProperties.midPoint(1) is equal to the X
% midpoint. midPoint(2) is equal to the Y midpoint.
function [lineProperties] = getLineProperties(point1, point2)
%set var names for easier readibility
x1 = point1(1);
y1 = point1(2);
x2 = point2(1);
y2 = point2(2);
slope = (y2 - y1) / (x2 - x1);
%find the negative reciprocal (perpendicular) of the slope
%http://www.mathsisfun.com/algebra/line-parallel-perpendicular.html
inverseSlope = -(1/slope);
midPoint(1) = ((x1 + x2) / 2);
midPoint(2) = ((y1 + y2) / 2);
%calculate y intercept (b) of the perpendicular line
%Equation: b = y1 - mx1
%http://www.wikihow.com/Find-the-Equation-of-a-Line
yIntercept = midPoint(2) - inverseSlope*midPoint(1);
%old solution, takes longer to process
%syms b;
%yIntercept = solve(midPoint(2) == slopePerp*midPoint(1) + b, b);
%yIntercept = double(yIntercept);
%
%calculate x intercept of the perpendicular line
%Equation y = mx+b... set y to 0
%Equation x = (y-b) / m
%y = 0
xIntercept = (-yIntercept / inverseSlope);
%Old solution, this takes longer to process but works the same as the above
%code.
%syms x;
%xIntercept = solve(0 == slopePerp*x + yIntercept, x);
%xIntercept = double(xIntercept);
%set the array that will be passed back
lineProperties.slope = slope;
lineProperties.inverseSlope = inverseSlope;
lineProperties.midPoint = midPoint;
lineProperties.yIntercept = yIntercept;
lineProperties.xIntercept = xIntercept;
lineProperties.midPoint = midPoint;
end