Java 如何求两条直线的交点
我被支持为一个点和一条线编写一个带构造函数的类,在Line类中,我被支持编写一个方法来查找两条线是否相交。这是我的重点课程:Java 如何求两条直线的交点,java,Java,我被支持为一个点和一条线编写一个带构造函数的类,在Line类中,我被支持编写一个方法来查找两条线是否相交。这是我的重点课程: public class Point { static double x; static double y; public Point(double x, double y) { this.x = x; this.y = y; } } 这是我的线路课 public class Line { public Line(Point x, Point y)
public class Point {
static double x;
static double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
}
这是我的线路课
public class Line {
public Line(Point x, Point y) {
}
// create two points for a line
static Point x1y1;
static Point x2y2;
// create another two points for another line
static Point x3y3;
static Point x4y4;
public static void main(String[] args) {
// create lines
Line line1 = new Line(x1y1, x2y2);
Line line2 = new Line(x3y3, x4y4);
//initialize points
x1y1 = new Point(1.0, 1.0);
x2y2 = new Point(3.0, 3.0);
x3y3 = new Point(1.0, 2.0);
x4y4 = new Point(4.0, 2.0);
//call method to find if lines intersect
findIntersection(line1, line2);
System.out.println(x1y1.x);
}
public static void findIntersection(Line line1, Line line2) {
double denominator = (x1y1.x - x2y2.x) * (x3y3.y - x4y4.y)
- (x1y1.y - x2y2.y) * (x3y3.x - x4y4.x);
double px = 0;
double py = 0;
if (denominator == 0) {
System.out.println("Lines are parallel, they do not intersect");
} else {
px = ((x1y1.x * x2y2.y - x1y1.y * x2y2.x) * (x3y3.x - x4y4.x) - (x1y1.x - x2y2.x)
* (x3y3.x * x4y4.y - x3y3.y * x4y4.x))
/ denominator;
py = ((x1y1.x * x2y2.y - x1y1.y * x2y2.x) * (x3y3.y - x4y4.y) - (x1y1.y - x2y2.y)
* (x3y3.x * x4y4.y - x3y3.y * x4y4.x))
/ denominator;
System.out.println(px + "," + py);
}
}
}
问题是我初始化了所有的点,因此直线应该相交,但是当我试图打印出点的值时,第三个点的x-es和y-s的值等于第四个点,尽管我用不同的值初始化它们,因此该方法计算出直线不相交。为什么前三个y和x的值等于第四个 这是因为
Point
类的x
和y
是static
。这意味着,Point
类的每个实例将使用相同的值。删除static
以修复它
public class Point {
double x;
double y;
无论如何,您也应该改进类设计:两个类(点、线)和另一个main方法
线路也一样。否则,每一条线都会有相同的点。(我认为您使用了4个变量来避免这种情况。)
像这样的
public class Point {
double x;
double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
}
public class Line {
Point a;
Point b;
public Line(Point a, Point b) {
this.a = a;
this.b = b;
}
public Point itIntersect(Line line) {
// here change the logic
Point point = null;
double denominator = (x1y1.x - x2y2.x) * (x3y3.y - x4y4.y)
- (x1y1.y - x2y2.y) * (x3y3.x - x4y4.x);
double px = 0;
double py = 0;
if (denominator != 0) {
px = ((x1y1.x * x2y2.y - x1y1.y * x2y2.x) * (x3y3.x - x4y4.x) - (x1y1.x - x2y2.x)
* (x3y3.x * x4y4.y - x3y3.y * x4y4.x))
/ denominator;
py = ((x1y1.x * x2y2.y - x1y1.y * x2y2.x) * (x3y3.y - x4y4.y) - (x1y1.y - x2y2.y)
* (x3y3.x * x4y4.y - x3y3.y * x4y4.x))
/ denominator;
// System.out.println(px + "," + py);
point = new Point(px, py);
}
return point;
}
}
另外,我没有改变intersect的逻辑,因为它太长了,但是你可以这样做来改进设计
然后您需要执行Point=line.itIntersect(另一行)
如果为空,则不相交,否则返回点
p.S将
x
和y
设为私有,我没有,因为这是一个示例。您的行构造函数是空的,所以您的“行”实际上是。。。nothing和findIntersection使用行作为参数,但从不使用行,只使用您声明的静态int。当你说“前三个等于第三个”时,你指的是什么x和y?嗯。我可能可以修复它并用它来寻找据点。为什么在这种情况下也要练习使用System.out类(我因此而被扣分)?你将使用System.out在控制台中打印交叉点。