# 問題 A: 顯示交點

### 問題 A: 顯示交點

[提交] [狀態] [命題人:xuqiang]

``````<span style="color:#333333"><span style="color:#333333">2 2 5 -1.0 4.0 2.0 -1.0 -2.0
</span></span>``````

``<span style="color:#333333"><span style="color:#333333">The intersecting point is: (2.89,1.11)</span></span>``

``````import java.util.Scanner;

class LinearEquation{

private double x1;
private double x2;
private double x3;
private double x4;
private double y1;
private double y2;
private double y3;
private double y4;
private double x;
private double y;
private double k1;
private double k2;

public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double getK1() {
return k1;
}
public void setK1(double k1) {
this.k1 = k1;
}
public double getK2() {
return k2;
}
public void setK2(double k2) {
this.k2 = k2;
}
public double getX1() {
return x1;
}
public void setX1(double x1) {
this.x1 = x1;
}
public double getX2() {
return x2;
}
public void setX2(double x2) {
this.x2 = x2;
}
public double getX3() {
return x3;
}
public void setX3(double x3) {
this.x3 = x3;
}
public double getX4() {
return x4;
}
public void setX4(double x4) {
this.x4 = x4;
}
public double getY1() {
return y1;
}
public void setY1(double y1) {
this.y1 = y1;
}
public double getY2() {
return y2;
}
public void setY2(double y2) {
this.y2 = y2;
}
public double getY3() {
return y3;
}
public void setY3(double y3) {
this.y3 = y3;
}
public double getY4() {
return y4;
}
public void setY4(double y4) {
this.y4 = y4;
}

public LinearEquation(){

}

public LinearEquation(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4){
this.x1=x1;
this.x2=x2;
this.x3=x3;
this.x4=x4;
this.y1=y1;
this.y2=y2;
this.y3=y3;
this.y4=y4;
k1=(y2-y1)/(x2-x1);
k2=(y4-y3)/(x4-x3);
x=(k2*x4-y4-k1*x2+y2)/(k2-k1);
//System.out.println(x);
y=k1*(x-x2)+y2;
//System.out.println(y);
//double tpx,tpy;
if(x1>x2) {
this.x1=x2;
this.x2=x1;
this.y1=y2;
this.y2=y1;
}
if(x3>x4) {
this.x3=x4;
this.x4=x3;
this.y3=y4;
this.y4=y3;
}
}

}
public class Main {

public static void main(String[] args) {
// TODO 自動生成的方法存根
//double x1,y1,x2,y2,x3,y3,x4,y4;
//double[][] gd = new double[10][10];
Scanner scan = new Scanner(System.in);
int flag = 0;
while(scan.hasNext()) {
LinearEquation le = new LinearEquation(scan.nextDouble(),scan.nextDouble(),scan.nextDouble(),scan.nextDouble(),scan.nextDouble(),scan.nextDouble(),scan.nextDouble(),scan.nextDouble());
//System.out.println(le.getX3()+"  " +le.getX4());
if((le.getX()>=le.getX1()&&le.getX()<=le.getX2())||(le.getX()>=le.getX3()&&le.getX()<=le.getX4())) {
flag=1;
}
if(flag==1) {
System.out.printf("The intersecting point is: (%.2f,%.2f)\n",le.getX(),le.getY());
}else {
System.out.println("The two lines do not cross");
}
}
}

}
``````