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