简体   繁体   English

使用 Cramer 规则查找线相交 - 获得不正确的 y 坐标

[英]Finding line intersect using Cramer's rule - Getting incorrect y coordinate

I'm looking to find the intersect of 2 lines using Cramer's rule.我正在寻找使用 Cramer 规则的 2 条线的交点。 This is for an exercise from the book An Introduction To Java Programming (Exercise 3.25 from Chapter 3 and 8.31 from Chapter 8. They are both basically the same idea just the one from chapter 8 uses arrays).这是 Java 编程简介(第 3 章中的练习 3.25 和第 8 章中的练习 8.31。它们基本上是相同的想法,只是第 8 章中的一个使用数组)。

The exercise tells us to use Cramer's Rule and provides the general formula.练习告诉我们使用克莱默规则并提供通用公式。

(y1 - y2)x - (x1 - x2)y = (y1 - y2)x1 - (x1 - x2)y1 (y1 - y2)x - (x1 - x2)y = (y1 - y2)x1 - (x1 - x2)y1

(y3 - y4)x - (x3 - x4)y = (y3 - y4)x3 - (x3 - x4)y3 (y3 - y4)x - (x3 - x4)y = (y3 - y4)x3 - (x3 - x4)y3

My problem is that I'm getting the y coordinate as a negative while I expected it to be a positive.我的问题是我得到的 y 坐标是负数,而我预计它是正数。

This is the output that I'm getting.这是我得到的输出。

The intersecting point is at (2.888888888888889, -1.1111111111111112)

This is what I expected to get.这是我期望得到的。

The intersecting point is at (2.888888888888889, 1.1111111111111112)

Here is the code I have come up with.这是我想出的代码。

public class Test{
    public static void main(String[] args){
        double[][] points = {{2, 2}, {5, -1.0}, {4.0, 2.0}, {-1.0, -2.0}};
        double[] result = getIntersectingPoint(points);
        if(result == null)
            System.out.println("The 2 lines are parallel");
        else
            System.out.println("The intersecting point is at (" + result[0] + ", " + result[1] + ")");
    }

    /*
     *For two lines to see  if they intersect
     *(y1 - y2)x - (x1 - x2)y = (y1 - y2)x1 - (x1 - x2)y1
     *(y3 - y4)x - (x3 - x4)y = (y3 - y4)x3 - (x3 - x4)y3
     */
    public static double[] getIntersectingPoint(double[][] points){
        double[] result = new double[2];

        double a = points[0][1] - points[1][1]; //y1 - y2;
        double b = points[0][0] - points[1][0]; //x1 - x2;
        double c = points[2][1] - points[3][1]; //y3 - y4;
        double d = points[2][0] - points[3][0]; //x3 - x4;
        double e = a * points[0][0] - b * points[0][1]; //(y1 - y2)x1 - (x1 - x2)y1
        double f = c * points[2][0] - d * points[2][1]; //(y3 - y4)x3 - (x3 - x4)y3

        double determinant = a * d - b * c;

        //There is no solution to the equation,
        //this shows the  lines are parallel
        if(determinant == 0)
            return null;

        //There is a solution to the equation
        //this shows the lines intersect.
        else{
            result[0] = (e * d - b * f) / determinant;
            result[1] = (a * f - e * c) / determinant;
        }
        return result;
    }
}

I have searched around and found some solutions in general to the exercise.我已经四处搜索并找到了一些针对该练习的一般解决方案。 But I haven't found anything about my issue of getting a negative y coordinate.但是我没有发现任何关于我获得负 y 坐标的问题。

Here is a picture of the intersecting lines这是相交线的图片相交线

To answer my own question...回答我自己的问题...

I finally figured out the problem.我终于想通了问题所在。 At the end of the function getIntersectingPoint there us the following line.在函数getIntersectingPoint的末尾,有以下一行。

result[0] = (e * d - b * f) / determinant;

this needs to be changed to the following.这需要更改为以下内容。

result[0] = (b * f - e * d) / determinant;

A little in my defense.有点为我辩护。 I don't really understand Cramer's rule, I was just following what the book was telling me.我真的不明白克莱默的规则,我只是按照书上告诉我的。 the book shows the following as the rule.这本书显示了以下规则。 (This is given in Chapter 1 Exercise 1.13). (这在第 1 章练习 1.13 中给出)。

     ax + by = e
     cx + dy = f
     x = ed - bf / ad - bc 
     y = af - ec / ad - bc

Given this I just adjusted it to find the line intersect.鉴于此,我只是调整它以找到相交的线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM