Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linear equation java [closed]

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

    }
}
like image 633
xdaimon Avatar asked Sep 27 '12 00:09

xdaimon


1 Answers

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.

like image 170
arcy Avatar answered Sep 18 '22 13:09

arcy