java编程判断这两条直线是否相交?,若相交,求出交点。

从键盘输入8个整数,分别表示平面上四个点,即A(X1,Y1),B(X2,Y2),C(X3,Y3)D(X4,Y4),其中0<=Xi,Yi<=1000,前两个点AB和后两个点CD分别确定一条直线,判断这两条直线是否相交?,若相交,求出交点。

要求:
1、构建一个点Point类,包含两个成员变量X,Y,其中X表示横坐标,Y表示纵坐标
2、构建一个直线Line类,包含两个成员变量A,B(其中A、B分别表示平面上的点)和一个为这两个点赋值的构造方法。
3、构建一个相交LineIntersect类,包含两个成员变量AB,CD(其中AB、CD分别表示平面上的直线)和名为JudgeLineIntersect的方法:判断这两条直线是否相交?,若相交,求出交点。
提示:
  如果两条直线重合,则输出“两条直线重合”
如果两条直线平行,则输出“两条直线平行”
 如果两条直线相交,则计算并输出它们的交点。

判断方法
d1=(y2-y1)*(x4-x3);//判断两条直线斜率是否相等,和斜率不存在时判断两直线是否平行
d2=(x2-x1)*(y4-y3); //如果d1==d2,则平行
d3=(y3-y1)*(x2-x1);//判断两条直线是否重合
d4=(x3-x1)*(y2-y1); //如果d3==d4,则重合

您好,下面是我写的代码,麻烦看一下是否符合要求。

Point类:

public class Point {

    private int x;

    private int y;

    public int getX() {

        return x;

    }

    public void setX(int x) {

        this.x = x;

    }

    public int getY() {

        return y;

    }

    public void setY(int y) {

        this.y = y;

    }

}

Line类:

public class Line {

    private Point a;

    private Point b;

    public Point getA() {

        return a;

    }

    public Point getB() {

        return b;

    }

    public Line(Point a, Point b) {

        this.a = a;

        this.b = b;

    }

}

LineIntersect类:

public class LineIntersect {

    private Line ab;

    private Line cd;

    public LineIntersect(Line ab, Line cd) {

        this.ab = ab;

        this.cd = cd;

    }

    public void JudgeLineIntersect() {

        Point A = this.ab.getA();

        Point B = this.ab.getB();

        Point C = this.cd.getA();

        Point D = this.cd.getB();

        int d1 = (B.getY() - A.getY()) * (D.getX() - C.getX());

        int d2 = (B.getX() - A.getX()) * (D.getY() - C.getY());

        int d3 = (C.getY() - A.getY()) * (B.getX() - A.getX());

        int d4 = (C.getX() - A.getX()) * (B.getY() - A.getY());

        if (d1 == d2) {

            System.out.println("两条直线平行。");

        } else if (d3 == d4) {

            System.out.println("两条直线重合。");

        } else {

            System.out.println("两条直线相交。");

            int d5 = C.getY() * D.getX() - C.getX() * D.getY();

            int d6 = A.getY() * B.getX() - A.getX() * B.getY();

            float intersect_x = ((C.getX() - D.getX()) * d6 - (A.getX() - B.getX()) * d5)

                    / ((B.getY() - A.getY()) * (D.getX() - C.getX()) - (B.getX() - A.getX()) * (D.getY() - C.getY()));

            float intersect_y = ((C.getY() - D.getY()) * d6 - (A.getY() - B.getY()) * d5)

                    / ((B.getY() - A.getY()) * (D.getX() - C.getX()) - (B.getX() - A.getX()) * (D.getY() - C.getY()));

            System.out.println("交点的X坐标为:" + intersect_x);

            System.out.println("交点的Y坐标为:" + intersect_y);

        }

    }

}

下面是主类(测试类):

import java.util.Scanner;


public class Main {

    public static void main(String[] args) {

        System.out.print("请输入A点的X坐标:");

        Scanner inAX = new Scanner(System.in);

        int ax = inAX.nextInt();

        System.out.print("请输入A点的Y坐标:");

        Scanner inAY = new Scanner(System.in);

        int ay = inAY.nextInt();

        System.out.print("请输入B点的X坐标:");

        Scanner inBX = new Scanner(System.in);

        int bx = inBX.nextInt();

        System.out.print("请输入B点的Y坐标:");

        Scanner inBY = new Scanner(System.in);

        int by = inBY.nextInt();

        System.out.print("请输入C点的X坐标:");

        Scanner inCX = new Scanner(System.in);

        int cx = inCX.nextInt();

        System.out.print("请输入C点的Y坐标:");

        Scanner inCY = new Scanner(System.in);

        int cy = inCY.nextInt();

        System.out.print("请输入D点的X坐标:");

        Scanner inDX = new Scanner(System.in);

        int dx = inDX.nextInt();

        System.out.print("请输入D点的Y坐标:");

        Scanner inDY = new Scanner(System.in);

        int dy = inDY.nextInt();

        Point A = new Point();

        A.setX(ax);

        A.setY(ay);

        Point B = new Point();

        B.setX(bx);

        B.setY(by);

        Point C = new Point();

        C.setX(cx);

        C.setY(cy);

        Point D = new Point();

        D.setX(dx);

        D.setY(dy);

        Line AB = new Line(A, B);

        Line CD = new Line(C, D);

        LineIntersect lineIntersect = new LineIntersect(AB, CD);

        lineIntersect.JudgeLineIntersect();

        inAX.close();

        inAY.close();

        inBX.close();

        inBY.close();

        inCX.close();

        inCY.close();

        inDX.close();

        inDY.close();

    }

}

下面是我刚才做的测试结果:

温馨提示:内容为网友见解,仅供参考
第1个回答  2022-06-29
您好,下面是我写的代码,麻烦看一下是否符合要求。Point类:public class Point {
private int x; private int y; public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; }}Line类:public class Line {
private Point a; private Point b; public Point getA() { return a; } public Point getB() { return b; } public Line(Point a, Point b) { this.a = a; this.b = b; }}LineIntersect类:public class LineIntersect { private Line ab; private Line cd; public LineIntersect(Line ab, Line cd) { this.ab = ab; ***.cd = cd; } public void JudgeLineIntersect() { Point A = this.ab.getA(); Point B = this.ab.getB(); Point C = ***.cd.getA(); Point D = ***.cd.getB(); int d1 = (B.getY() - A.getY()) * (D.getX() - C.getX()); int d2 = (B.getX() - A.getX()) * (D.getY() - C.getY()); int d3 = (C.getY() - A.getY()) * (B.getX() - A.getX()); int d4 = (C.getX() - A.getX()) * (B.getY() - A.getY()); if (d1 == d2) { System.out.println("两条直线平行。"); } else if (d3 == d4) { System.out.println("两条直线重合。"); } else { System.out.println("两条直线相交。"); int d5 = C.getY() * D.getX() - C.getX() * D.getY(); int d6 = A.getY() * B.getX() - A.getX() * B.getY(); float intersect_x = ((C.getX() - D.getX()) * d6 - (A.getX() - B.getX()) * d5) / ((B.getY() - A.getY()) * (D.getX() - C.getX()) - (B.getX() - A.getX()) * (D.getY() - C.getY())); float intersect_y = ((C.getY() - D.getY()) * d6 - (A.getY() - B.getY()) * d5) / ((B.getY() - A.getY()) * (D.getX() - C.getX()) - (B.getX() - A.getX()) * (D.getY() - C.getY())); System.out.println("交点的X坐标为:" + intersect_x); System.out.println("交点的Y坐标为:" + intersect_y); } }}下面是主类(测试类):import java.util.Scanner;
public class Main { public static void main(String[] args) { System.out.print("请输入A点的X坐标:"); Scanner inAX = new Scanner(***.in); int ax = inAX.nextInt(); System.out.print("请输入A点的Y坐标:"); Scanner inAY = new Scanner(***.in); int ay = inAY.nextInt(); System.out.print("请输入B点的X坐标:"); Scanner inBX = new Scanner(***.in); int bx = inBX.nextInt(); System.out.print("请输入B点的Y坐标:"); Scanner inBY = new Scanner(***.in); int by = inBY.nextInt(); System.out.print("请输入C点的X坐标:"); Scanner inCX = new Scanner(***.in); int cx = inCX.nextInt(); System.out.print("请输入C点的Y坐标:"); Scanner inCY = new Scanner(***.in); int cy = inCY.nextInt(); System.out.print("请输入D点的X坐标:"); Scanner inDX = new Scanner(***.in); int dx = inDX.nextInt(); System.out.print("请输入D点的Y坐标:"); Scanner inDY = new Scanner(***.in); int dy = inDY.nextInt(); Point A = new Point(); A.setX(ax); A.setY(ay); Point B = new Point(); B.setX(bx); B.setY(by); Point C = new Point(); C.setX(cx); C.setY(cy); Point D = new Point(); D.setX(dx); D.setY(dy); Line AB = new Line(A, B); Line CD = new Line(C, D); LineIntersect lineIntersect = new LineIntersect(AB, CD); lineIntersect.JudgeLineIntersect(); inAX.close(); inAY.close(); inBX.close(); inBY.close(); inCX.close(); inCY.close(); inDX.close(); inDY.close(); }}下面是我刚才做的测试结果:

java编程判断这两条直线是否相交?,若相交,求出交点。
int d4 = (C.getX() - A.getX()) * (B.getY() - A.getY());if (d1 == d2) { System.out.println("两条直线平行。");} else if (d3 == d4) { System.out.println("两条直线重合。");} else { System.out.println("两条直线相交。");int d5 = C.getY() * D.g...

用java写一个一条直线是否与圆相交的函数?
b=2:直线与圆相交 b=1:直线与圆不相交!

谈谈"求线段交点"的几种算法
算法二 判断每一条线段的两个端点是否都在另一条线段的两侧 是则求出两条线段所在直线的交点 否则不相交 第一步判断两个点是否在某条线段的两侧 通常可采用投影法 求出线段的法线向量 然后把点投影到法线上 最后根据投影的位置来判断点和线段的关系 见下图 点a和点b在线段cd法线上的投影如图所示 ...

java取得线段与矩形的交点
(1)有一个点在矩形内部,另一个点在矩形外部,这时候只有一个交点。(2)两个点都在矩形外部,这时候有两个交点。(1)和(2)不同时满足。对于(1)通过判断A、B相对于矩形4条边的位置就可以确定。对于(2)先计算出AB、DE、EF、FG、GD这5条直线的斜率,然后可以根据斜率判断是否相交,若斜...

java判断两圆是否相交的概率
java判断两圆是否相交的概率,两个圆相交的交点是偶点。在这里我们一起疏理一下两圆的位置关系,与它们有公共点的情况。第一两圆没有公共点,说明两圆相离或内含。第二两圆只有一个公共点,说明两圆相切。第三种两圆有两个公共点,说明两圆柤交这可以个点就是它们交点 ...

如何高效地判断两个单链表是否有交叉
分别获得两个链表的长度,计算出两个链表的长度差d,较长的链表首先移动d步,然后两个链表同时向表尾移动,当出现两个节点相同时即为交点。时间复杂度:O(m) + O(n) = O(max(m,n))(2)示例代码:package cn.zifangsky.linkedlist.questions;import java.util.HashMap;import java.util.Map;...

相似回答