gpt4 book ai didi

java - 在Android/java中求解两个方程组

转载 作者:行者123 更新时间:2023-12-01 19:06:37 26 4
gpt4 key购买 nike

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

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);

看来这是由斜率始终是整数引起的,因此当它 < 1 时,它是 0,这不好......

最佳答案

请复习下面的代数:

定义

x = x2-x1

y = y2-y1

然后

米 * 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

关于java - 在Android/java中求解两个方程组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9827695/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com