Java linear equation

I am trying to convert the equation below into programming code. The goal is to find the intersecting point of the two lines. And to pront

(y1 - y2) x - (x1 - x2) y = (y1 - y2) x1 - (x1 - x2) y1

(y3 - y4) x - (x3 - x4) y = (y3 - y4) x3 - (x3 - x4) y3

I was told to use the cramers rule, but the cramers rule has 6 diff variables. I will start from 4 different points in the form of 8 variables (x1, y1, x2, y2, x3, y3, x4, y4)

I am using Java. Any help would be greatly appreciated. All the questions asked on this site are for different types of linear equations with a long complex code, I did not find anything that would matter to me.

This is what I have, not so much, but the transition from the above equation to something programmable really brings me down.

import java.util.Scanner; public class E325 { public static void main(String[] args) { /* * The purpose of this program is to find the intersect * of two lines given by the user by four points * * Get the four points. x1,y1 x2,y2 x3,y3 x4,y4 */ Scanner input = new Scanner(System.in); System.out.print("Enter x1 y1, x2 y2, x3 y3, x4 y4: "); double x1 = input.nextDouble(); double y1 = input.nextDouble(); double x2 = input.nextDouble(); double y2 = input.nextDouble(); double x3 = input.nextDouble(); double y3 = input.nextDouble(); double x4 = input.nextDouble(); double y4 = input.nextDouble(); } } 
+6
source share
3 answers

I do not know the matrices, so I would solve it differently.

You know enough to calculate m and b for each row

m = (y2-y1) / (x2-x1)

b = y1 - m (x1)

Compute m and b for one row and m 'and b' for another.

Now at the intersection x, y on both lines are the same, so y = mx + b and y = m'x + b '. therefore

mx + b = m'x + b '

x = (m'x + b '- b) / m

Insert x into mx + b to get y for that x.

You still need to make sure that the x, y that you found are on your SEGMENTS line; if the lines are not parallel, they will intersect somewhere, but not necessarily between the endpoints of the line segments you started with.

+4
source

(y1 - y2) x - (x1 - x2) y = (y1 - y2) x1 - (x1 - x2) y1
(y3 - y4) x - (x3 - x4) y = (y3 - y4) x3 - (x3 - x4) y3

I was told to use the cramers rule, but the cramers rule has 6 diff variables.

No. This is completely wrong. The Cramer rule is a simple method for solving equations of the form Ax = b, but where A is the matrix NxN, and x and b are N vectors. What you need to do is formulate these two equations as a matrix expression. I will give you the left side corresponding to the above. I will leave the right side and the Cramer rule application up to you.

  A * x = b ⌈ y1-y2 -(x1-x2) βŒ‰ ⌈ x βŒ‰ | | * | | ⌊ y3-y4 -(x3-x4) βŒ‹ ⌊ y βŒ‹ 

Multiplying this matrix A and the vector [x, y], we obtain

 ⌈ y1-y2 -(x1-x2) βŒ‰ ⌈ x βŒ‰ ⌈ (y1-y2)*x - (x1-x2)*y βŒ‰ | | * | | = | | ⌊ y3-y4 -(x3-x4) βŒ‹ ⌊ y βŒ‹ ⌊ (y3-y4)*x - (x3-x4)*y βŒ‹ 

Note that this result is exactly the same as the left sides of your pair of equations.

+1
source

The line goes through two points.

You have four dots and two lines.

The equation of the line is well known:

 y = m*x + b 

where m is the slope and b is the y-intercept.

If you have two lines, they look like this:

 -m1*x + y = b1 -m2*x + y = b2 

You can see the matrix equation, right?

 [ -m1 1 ]{x} = {b1} [ -m2 1 ]{y} {b2} 

Invert this to solve for (x, y) where the two lines intersect. If you cannot invert the matrix, this means that they do not intersect.

Cramer's Rule? Of course, it's easy to write for the 2x2 case. LU decomposition will be more general.

0
source

Source: https://habr.com/ru/post/1436496/


All Articles