Hyp*_*ino 3 java math geometry point line
在Java中,我有一个Line具有两个变量的类:m并且b,这样的行遵循公式mx + b.我有两条这样的台词.我如何找到两条线的交点x和y坐标?(假设斜坡不同)
这是class Line:
import java.awt.Graphics;
import java.awt.Point;
public final class Line {
public final double m, b;
public Line(double m, double b) {
this.m = m;
this.b = b;
}
public Point intersect(Line line) {
double x = (this.b - line.b) / (this.m - line.m);
double y = this.m * x + this.b;
return new Point((int) x, (int) y);
}
public void paint(Graphics g, int startx, int endx, int width, int height) {
startx -= width / 2;
endx -= width / 2;
int starty = this.get(startx);
int endy = this.get(endx);
Point points = Format.format(new Point(startx, starty), width, height);
Point pointe = Format.format(new Point(endx, endy), width, height);
g.drawLine(points.x, points.y, pointe.x, pointe.y);
}
public int get(int x) {
return (int) (this.m * x + this.b);
}
public double get(double x) {
return this.m * x + this.b;
}
}
Run Code Online (Sandbox Code Playgroud)
小智 9
让我们假设你有这两个功能:
y = m1*x + b1
y = m2*x + b2
Run Code Online (Sandbox Code Playgroud)
要找到x-axis我们所做的交点:
m1*x+b1 = m2*x+b2
m1*x-m2*x = b2 - b2
x(m1-m2) = (b2-b1)
x = (b2-b1) / (m1-m2)
Run Code Online (Sandbox Code Playgroud)
要查找y,可以使用函数表达式并替换x其值(b2-b1) / (m1-m2).
所以:
y = m1 * [(b2-b1) / (m1-m2)] + b1
Run Code Online (Sandbox Code Playgroud)
你有(this.b - line.b),改为(line.b - this.b).
public Point intersect(Line line) {
double x = (line.b - this.b) / (this.m - line.m);
double y = this.m * x + this.b;
return new Point((int) x, (int) y);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16886 次 |
| 最近记录: |