英文:
How to find the other endpoint given an endpoint and the midpoint
问题
我正在开发一个桌面应用程序,将帮助学生学习代数。我有一个名为Vector2的类,它只存储两个变量,即x和y。我正在编写一个方法,该方法将在给定端点和中点的情况下返回一条线的终点。为了展示我如何做到这一点,这里是一个方法示例:
public static Vector2 midpoint(double xa, double ya, double xb, double yb){
Vector2 v = new Vector2(0, 0);
v.x = (xa + xb) / 2;
v.y = (ya + yb) / 2;
return v;
}
在此基础上,如果我想创建一个方法来获取另一个端点,应该如何操作?
示例:如果给定的端点为(-3, -5),给定的中点为(-6, -2),则输出应为(-9, 1)。
编辑:针对新的问题,我已经有了答案,方法非常简单。以下是最终方法:
public static Vector2 otherEndpoint(double endPointX, double EndPointY, double midPointX, double midPointY){
Vector2 v = new Vector2(0,0);
v.x = (endPointX + midPointX) / 2;
v.y = (EndPointY + midPointY) / 2;
v.multiply(2);
return v;
}
v.multiply()
是我创建的一个方法,可以将每个点乘以输入的任意数字。
英文:
I am working on a desktop app that will help students with algebra. I have a class called Vector2 that just stores two variables, x and y. I am working on a method that will return the endpoint of a line given an endpoint and a midpoint. Just to show how I have been doing this here is a method
public static Vector2 midpoint(double xa, double ya, double xb, double yb){
Vector2 v = new Vector2(0, 0);
v.x = (xa + xb) / 2;
v.y = (ya + yb) / 2;
return v;
}
Given this how would I make a method that will give me the other endpoint?
Example: if the given endpoint is (-3, -5) and the given midpoint is (-6, -2) then the output should be (-9, 1)
EDIT FOR NEW PPL: I have the answer, it is pretty simple. Here is the final method
public static Vector2 otherEndpoint(double endPointX, double EndPointY, double midPointX, double midPointY){
Vector2 v = new Vector2(0,0);
v.x = (endPointX + midPointX) / 2;
v.y = (EndPointY + midPointY) / 2;
v.multiply(2);
return v;
}
v.multiply()
is a method that I made that will multiply each point by whatever number you put in
答案1
得分: 1
public final class Point {
private final int x;
private final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
public static Point getMidpoint(Point one, Point two) {
int x = (one.x + two.x) / 2;
int y = (one.y + two.y) / 2;
return new Point(x, y);
}
public static Point getEndpoint(Point one, Point mid) {
int x = (2 * mid.x) - one.x;
int y = (2 * mid.y) - one.y;
return new Point(x, y);
}
英文:
public final class Point {
private final int x;
private final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
public static Point getMidpoint(Point one, Point two) {
int x = (one.x + two.x) / 2;
int y = (one.y + two.y) / 2;
return new Point(x, y);
}
public static Point getEndpoint(Point one, Point mid) {
int x = (2 * mid.x) - one.x;
int y = (2 * mid.y) - one.y;
return new Point(x, y);
}
答案2
得分: 1
public static Vector2 fromEndAndMidpoint(double xa, double ya, double xmid, double ymid) {
Vector2 v = new Vector2();
v.x = 2 * xmid - xa;
v.y = 2 * ymid - ya;
return v;
}
英文:
public static Vector2 fromEndAndMidpoint(double xa, double ya, double xmid, double ymid) {
Vector2 v = new Vector2();
v.x = 2 * xmid - xa;
v.y = 2 * ymid - ya;
return v;
}
答案3
得分: 1
这是展示如何计算它的方法。
请注意,这有效是因为您知道一个点在中间。所以所需的端点距离中点只有一步,或者距离另一端有两步。
public static Vector2 midpoint(double xe, double ye, double xm,
double ym) {
// 计算从终点到中点的步长,分别针对 x 和 y。
double xstep = xm - xe;
double ystep = ym - ye;
// 然后将步长添加到中点以获得终点。
return new Vector2(xm + xstep, ym + ystep);
}
请注意,上述方法不能用来找到任意点。要找到任意点,您必须简单地通过计算斜率 m
和纵截距 b
来找到直线方程 y = mx + b
。
英文:
Here is the method that shows how to compute it.
Note that this works because you know that one point is in the middle. So the desired endpoint is just one step away from the middle or two steps away from the other end.
public static Vector2 midpoint(double xe, double ye, double xm,
double ym) {
// calculate the step to go from the end point to the middle
// for both x and y.
double xstep = xm - xe;
double ystep = ym - ye;
// then just add the step to the middle to get the end point.
return new Vector2(xm + xstep, ym + ystep);
}
Note that the above will not work to find any point. For that you must simply find the equation of the line y = mx + b
by computing the slope, m
, and y-intercept, b
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论