Java problem: four points are known, find if the lines are parallel, and show the intersection

Title:
The two points above the first straight line are (x1, y1) and (x2, y2), the two points above the second straight line are (x3, y3) and (x4, y4), the
two straight lines The intersection can be solved by the following system of linear equations:
(y1-y2) x-(x1-x2) y = (y1-y2) x1-(x1-x2) y1
(y3-y4) x-(x3-x4) y = (Y3-y4) x3-(x3-x4) y3
If the equation has no solution, the two straight lines are parallel.
Write a program that prompts the user to enter these four points, and then displays their intersection.

代码如下:
package com;
import java.util.Scanner;
class Equation {
private int x1;
private int y1;
private int x2;
private int y2;
private int x3;
private int y3;
private int x4;
private int y4;
public Equation(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.x3 = x3;
this.y3 = y3;
this.x4 = x4;
this.y4 = y4;
}

public int getX1() {
    return x1;
}
public int getY1() {
    return y1;
}
public int getX2() {
    return x2;
}
public int getY2() {
    return y2;
}
public int getX3() {
    return x3;
}
public int getY3() {
    return y3;
}
public int getX4() {
    return x4;
}
public int getY4() {
    return y4;
}
public boolean isSolvab1e() {
    int i,j,k,l,m,n,o;
     i=y1-y2;j=x1-x3;
     k=y3-y4;l=x3-x4;
     m=(y1-y2)*x1-(x1-x2)*y1;
     n=(y3-y4)*x3-(x3-x4)*y3;
     o=i*l-j*k;
     if(o!=0){
         return true;
     }
     else{return false;}
}
public int getX(){
    int i,j,k,l,m,n,o;
    i=y1-y2;j=x1-x3;
    k=y3-y4;l=x3-x4;
    m=(y1-y2)*x1-(x1-x2)*y1;
    n=(y3-y4)*x3-(x3-x4)*y3;
    return (m*l-j*n)/(i*l-j*k);
}
public int getY(){
    int i,j,k,l,m,n,o;
    i=y1-y2;j=x1-x3;
    k=y3-y4;l=x3-x4;
    m=(y1-y2)*x1-(x1-x2)*y1;
    n=(y3-y4)*x3-(x3-x4)*y3;
    return (i*n-m*k)/(i*l-j*k);
}

}
public class test {
public static void main (String [] args) {
System.out.println ("Please enter the value of four points:");
Scanner sc = new Scanner (System.in);
int x1 = sc. nextInt ();
int y1 = sc.nextInt ();
int x2 = sc.nextInt ();
int y2 = sc.nextInt ();
int x3 = sc.nextInt ();
int y3 = sc.nextInt ();
int x4 = sc.nextInt ();
int y4 = sc.nextInt ();
Equation x = new Equation (x1, y1, x2, y2, x3, y3, x4, y4);
if (x.isSolvab1e () == true ) {
System.out.println ("The intersection of two straight lines is");
System.out.println ("x is" + x.getX ());
System.out.println ("y is" + x.getY ());
} else {
System.out.println ("Line parallel");
}
}
}
operation result:
Insert picture description here

This article is original, please indicate the source.
If it helps you, please give me a thumbs up!

Published 9 original articles · won 9 · visited 2164

Guess you like

Origin blog.csdn.net/grandniu/article/details/105471460