在Android / java中解决两个方程的系统

时间:2012-03-22 17:45:43

标签: java android

我有两个方程式(线公式的距离和斜率)

d = sqrt((x2-x1)^ 2 +(y2-y1)^ 2)
m =(y2-y1)/(x2-x1)

已知:d,m,x1,y1
未知:x2,y2

问题是距离方程不是线性的......

有没有办法在java中编码(使用Android兼容库)来解决?我试着做简单的猜测,但它太慢了。

由于

编辑:三角形代码

        Point p1 = new Point();
        Point p2 = new Point();
        projection.toPixels(gp1, p1);
        projection.toPixels(gp2, p2);

        Point p3 = new Point();
        double slope = (p2.y - p1.y) / (p2.x - p1.x);
        double x = 0;
        if (p2.y - p1.y >= 0 && p2.x - p1.x >= 0) {
            x = - Math.sqrt(600 / (1 + slope*slope)) + p2.x;
        } else if (p2.y - p1.y >= 0 && p2.x - p1.x < 0) {
            x = Math.sqrt(600 / (1 + slope*slope)) + p2.x;
        } else if (p2.y - p1.y < 0 && p2.x - p1.x >= 0) {
            x = - Math.sqrt(600 / (1 + slope*slope)) + p2.x;
        } else if (p2.y - p1.y < 0 && p2.x - p1.x < 0) {
            x = Math.sqrt(600 / (1 + slope*slope)) + p2.x;
        }
        double y = -slope*p2.x + slope*x + p2.y;

        p3.set((int) x, (int) y);

        double inverseSlope = 0;
        if (slope == 0) {
            inverseSlope = Double.MAX_VALUE;
        } else {
            inverseSlope = -1 / slope;
        }

        x = -Math.sqrt(300 / (1 + inverseSlope*inverseSlope)) + p3.x;
        y = -Math.sqrt(300 / (1 + inverseSlope*inverseSlope))*inverseSlope + p3.y;

        Point p4 = new Point();
        p4.set((int) x, (int) y);

        x = Math.sqrt(300 / (1 + inverseSlope*inverseSlope)) + p3.x;
        y = Math.sqrt(300 / (1 + inverseSlope*inverseSlope))*inverseSlope + p3.y;
        Point p5 = new Point();
        p5.set((int)x, (int) y);
        Path path = new Path();
        path.moveTo(p2.x, p2.y);
        path.lineTo(p4.x, p4.y);
        path.moveTo(p4.x, p4.y);
        path.lineTo(p5.x, p5.y);
        path.moveTo(p5.x, p5.y);
        path.lineTo(p2.x, p2.y);
        path.moveTo(p2.x, p2.y);
        canvas.drawPath(path, mPaint);

看来它是由斜率总是一个整数引起的,所以当它是&lt; 1它是0,这是不好的......

2 个答案:

答案 0 :(得分:2)

请查看下面的代数:

定义

x = x2-x1

y = y2-y1

然后

m * x = y

d ^ 2 = x ^ 2 + m ^ 2 * x ^ 2 =(1 + m ^ 2)* x ^ 2

因此

x = sqrt(d ^ 2 /(1 + m ^ 2))

然后

x2 - x1 = sqrt(d ^ 2 /(1 + m ^ 2))

所以

x2 = sqrt(d ^ 2 /(1 + m ^ 2)+ x1

类似地

y = sqrt(d ^ 2 /(1 + m ^ 2))* m

y2 = sqrt(d ^ 2 /(1 + m ^ 2))* m + y1

所以答案是:

x2 = sqrt(d ^ 2 /(1 + m ^ 2))+ x1

y2 = sqrt(d ^ 2 /(1 + m ^ 2))* m + y1

答案 1 :(得分:2)

你正在寻找的“图书馆”被称为数学:)

你可以问wolfram alpha: http://www.wolframalpha.com/input/?i=solve+d+%3D+sqrt%28+%28x2+-+x1%29%5E2+%2B+%28y2+-+y1%29%5E2+%29%2C+m+%3D+%28y2+-+y1%29+%2F+%28x2+-+x1%29+over+the+reals (不要问我为什么它知道x2和y2是有趣的)

可以手工完成这些事情,但要小心不要掉线。尽管这些东西是在学校教授的,但他们对细节并不尊重。请注意,在这种情况下,有两种可能的解决方案,您需要确保d> 0在所有情况下!

First solution Second solution