問題 A: 顯示交點

問題 A: 顯示交點

時間限制: 1 Sec  內存限制: 128 MB
提交: 1380  解決: 988
[提交] [狀態] [命題人:xuqiang]

題目描述

假如兩個線段所在的直線相交且它們的斜率都存在。第一個線段的兩個端點是(x1,y1)和(x2,y2),第二個線段的兩個端點是(x3,y3)和(x4,y4)。

編寫一個程序,輸入四點座標,顯示這兩條線段所在直線的交點。如果沒有相交,輸出"The two lines do not cross".

要求,設計一個計算交點的類LinearEquation;在Main類中定義LinearEquation類對象,完成交點計算

 

 

輸入

依次輸入兩條線段的4個點,如x1,y1,x2,y2,x3,y3,x4,y4。

 

輸出

如果有交點,則輸出交點的座標,如果沒有交點,則輸出"The two lines do not cross"

 

樣例輸入 Copy

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

樣例輸出 Copy

<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");
            }
        }
    }

}

 

發佈了113 篇原創文章 · 獲贊 3 · 訪問量 9923
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章