6

I am trying to convert the equation below into programming code. The purpose 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've been told to use cramers rule, but cramers rule has 6 diff variables. I'll be starting off with 4 different points as 8 variables (x1, y1, x2, y2, x3, y3, x4, y4)

I'm using Java. Any help would be appreciated. All the asked questions on this site are for different types of linear equations with long complicated code, I didnt find anything that was relevant to me.

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

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();

    }
}
Bhargav Rao
  • 41,091
  • 27
  • 112
  • 129
xdaimon
  • 88
  • 2
  • 8
  • I don't know where you got the idea that Cramer's Rule was appropriate or had "6 diff variables". And the points are either given, so they aren't variables, or you need least squares fitting. Your information is wrong from start to finish. – duffymo Sep 27 '12 at 00:39
  • Hard to imagine a less friendly user interface.Maybe input four pairs of coordinates. – stark Sep 27 '12 at 00:41

3 Answers3

4

I don't know matrices, so I would solve it a different way.

You know enough to calculate m and b for each line

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

b = y1 - m(x1)

Calculate m and b for one line and m' and b' for the other.

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

mx + b = m'x + b'

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

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

You still have to ensure that the x,y you have found are on your line SEGMENTS; unless the lines are parallel, they will intersect somewhere, but not necessarily between the endpoints of the line segments you've started with.

arcy
  • 11,973
  • 8
  • 54
  • 89
1

(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've been told to use cramers rule, but cramers rule has 6 diff variables.

No. That's completely wrong. Cramer's rule is a simple technique to solve equations of the form Ax = b, but where A is a NxN matrix, and x and b are N vectors. What you need to do is to formulate those two equations as a matrix expression. I'll give you the left hand side that corresponds to the above. I'll leave the right hand side and the application of Cramer's rule up to you.

        A            *   x    = b

⌈ y1-y2   -(x1-x2) ⌉   ⌈ x ⌉
|                  | * |   |
⌊ y3-y4   -(x3-x4) ⌋   ⌊ y ⌋

Multiplying that matrix A and the vector [x,y] yields

⌈ 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 hand sides of your pair of equations.

Community
  • 1
  • 1
David Hammen
  • 30,597
  • 8
  • 54
  • 98
0

A line passes through two points.

You have four points and two lines.

The equation of a 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, can't you?

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

Invert this to solve for (x, y) where the two lines intersect. If you can't invert the matrix, it means they don't intersect.

Cramer's Rule? Sure, it's easy to write for a 2x2 case. LU decomposition would be more general.

duffymo
  • 293,097
  • 41
  • 348
  • 541
  • LU decomposition is a very powerful technique, but the questioner is asking a high school or freshman level question. He hasn't been taught about matrix decompositions, and probably won't be taught it for at least another couple of years. – David Hammen Oct 13 '12 at 14:14
  • Neither of us can infer the age of the OP from the question. Some older folks don't know math or linear algebra. I'd rather present the right information, because it's about everyone who reads the answer, not just the OP. – duffymo Oct 13 '12 at 15:33