There are two points lastPoint
and prePoint
. They determine a line.
I try to calculate slope of this line using this:
let kbase = (lastPoint.Y - prePoint.Y) / (lastPoint.X - prePoint.X);
let bbase = lastPoint.Y - kbase * lastPoint.X; //y = kx b -> y - kx;
let ybase = `${kbase} * x ${bbase}`;
So, if Y
are equals then kbase
is zero, it means straight line equation is y = b
.
After that I try to calculate slope of perpendicular line to the first line, but I get error, because 1 / kbase
where kbase == 0
.
let kh = -1 * (1 / kbase);
let bh = startPoint.Y - kh * startPoint.X;
let yh = `${kh} * x ${bh}`;
How to find a slope of perpendicular line if line equation is y = b
?
CodePudding user response:
The explicit form of a line equation, y = mx p
does not allow to represent verticals, as the slope is infinite.
For your purposes, you can use the implicit form, ax by c = 0
, which does not have this problem. An orthogonal line can be given the equation bx - ay d = 0
.
CodePudding user response:
As others have said, the slope of a vertical line is undefined. This is just how it works mathematically, and there are no problems with your code, and your math is correct. As you have realized, a horizontal line is a function that follows the form y = ox b and its slope is 0. The perpendicular line of a horizontal line results in a vertical line which is not a function. This means that there are several values of y that correspond to one value you of x. Here is why that is important.
The general way of calculating slop is the change in y over the change in x. Because a vertical line is not a function, there are multiple y's for one x. Here is an example. Let's say that we had the equation x=5. Here are some points: (5,1),(5,2),(5,3)(5,7). If you calculated slope with the first two points, it would result in (2-1)/(5-5) which is 1/0, and if you calculated slope with the last two points, it would result in (7-3)/(5-5) which is 4/0. If you continued to do this, you would have an infinite number of undefined values for the slope of a vertical line. Keep in mind that 1/0 does not necessarily equal 2/0 or 6/0.Even if you tried to find a horizontal line and take the inverse reciprocal, which you did, you would not be able to find a defined value.
For example, if slope is the change in y over the change in x, then the inverse reciprocal would be -1 * the change in x over the change in y. Let's say that we had the horizontal line y = 0x 5. The coefficient of the x or slope is 0. Four points would be (5,5), (9,5), (3,5) (6,5), and the two slope calculations for the slope of the perpendicular line would be 4/0 and 3/0 which leads to the same situation as last time.
To conclude, the answer to your question "How to find a slope of perpendicular line if line equation is y = b?" is that there is no defined value for the slope of a perpendicular line, and you definitely wont find one through the code you are using.