第三章第二十五题(几何:交点)(Geometry: intersecting point)

第三章第二十五题(几何:交点)(Geometry: intersecting point)

  • *3.25(几何:交点)两条直线的交点可以通过下面的线性方程组求解:
    在这里插入图片描述

    这个线性方程组可以应用Cramer法则求解(见编程练习题3.3)。如果方程无解,则两条直线平行。

    编写一个程序,提示用户输入这四个点,然后显示它们的交点。

    下面是这个程序的运行示例:

    Enter x1, y1, x2, y2, x3, y3, x4, y4: 2 2 5 -1.0 4.0 2.0 -1.0 -2.0
    The intersecting point is at (2.88889, 1.1111)
    Enter x1, y1, x2, y2, x3, y3, x4, y4: 2 2 7 6.0 4.0 2.0 -1.0 -2.0
    The two lines are parallel

    *3.25(Geometry: intersecting point) The intersecting point of the two lines can be found by solving the following linear equations:
    在这里插入图片描述
    This linear equation can be solved using Cramer’s rule (see Programming Exercise 3.3). If the equation has no solutions, the two lines are parallel.
    Write a program that prompts the user to enter four points and displays the intersecting point.

    Here are sample runs:
    Enter x1, y1, x2, y2, x3, y3, x4, y4: 2 2 5 -1.0 4.0 2.0 -1.0 -2.0
    The intersecting point is at (2.88889, 1.1111)
    Enter x1, y1, x2, y2, x3, y3, x4, y4: 2 2 7 6.0 4.0 2.0 -1.0 -2.0
    The two lines are parallel

  • 参考代码:

package chapter03;

import java.util.Scanner;

public class Code_25 {public static void main(String[] args) {
    double x1, y1, x2, y2, x3, y3, x4, y4;
    double a, b, c, d, e, f, x, y, discriminant;

    System.out.print("Enter x1, y1, x2, y2, x3, y3, x4, y4: ");
    Scanner input = new Scanner(System.in);
    x1 = input.nextDouble(); y1 = input.nextDouble();
    x2 = input.nextDouble(); y2 = input.nextDouble();
    x3 = input.nextDouble(); y3 = input.nextDouble();
    x4 = input.nextDouble(); y4 = input.nextDouble();

    a = y1 - y2;
    b = x2 - x1;
    c = y3 - y4;
    d = x4 - x3;
    e = a * x1 + b * y1;
    f = c * x3 + d * y3;

    discriminant = a * d - b * c;

    if(discriminant != 0)
    {
        x = (e * d - b * f) / discriminant;
        y = (a * f - e * c) / discriminant;
        System.out.println("The intersecting point is at(" + x + ", " + y + ")");
    }
    else
        System.out.println("The two lines are parallel");

    input.close();
}
}

  • 结果显示:
Enter x1, y1, x2, y2, x3, y3, x4, y4: 2 2 5 -1.0 4.0 2.0 -1.0 -2.0
The intersecting point is at(2.888888888888889, 1.1111111111111112)

Process finished with exit code 0

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值