在Java中,我应该如何命令一个2D点沿着距离的矢量线移动到另一个点



我有一个简单的类2Dpoints,它有两个字段x和y。我想写一个代码,这样我就可以命令一个点慢慢移动到另一个点,就像它在它们距离的矢量线上移动一样。但我不知道怎么做?我最初认为它应该包含一个for循环,这样它就会知道,它应该移动到另一个点类似于(int d=0;d<distance;d++(,但我不知道该如何命令它,使它在直线上移动?

import java.lang.Math.*;
public class Punkt { 
private int x;
private int y;
public Punkt(int x, int y) {
this.x=x;
this.y=y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setX(int distance) {
x = x + distance;
}
public void setY(int distance) {
y = y + distance;
}
public void moveAbout(int dx, int dy) {
x = x + dx;
y = y + dy;
}
/// method for calculating the distance to another point
public double giveDistance(Punkt otherPoint) {
return Math.sqrt(
(otherPoint.getY() - y) * 
(otherPoint.getY() - y) + 
(otherPoint.getX() - x) * 
(otherPoint.getX() - x));

}
}

我已经评论了主要行:

import static java.lang.Math.*;
/**
* Immutable structure. Functional way
*/
class Point { 
public final double x;
public final double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
/**
* Here you are. This is what you want to implement.
* from.moveTo(0.0, to) => from
* from.moveTo(1.0, to) => to
*
* @param by - from 0.0 to 1.0 (from 0% to 100%)
* @param target - move toward target by delta
*/
public Point moveTo(double by, Point target) {
Point delta = target.sub(this);
return add(delta.dot(by));
}
public Point add(Point point) {
return new Point(x + point.x, y + point.y);
}
public Point sub(Point point) {
return new Point(x - point.x, y - point.y);
}
public Point dot(double v) {
return new Point(v * x, v * y);
}
public double dist(Point point) {
return sub(point).len();
}
public double len() {
return sqrt(x * x + y * y);
}
public String toString() {
return x + ":" + y;
}
}
class Main {
public static void main(String[] args) {
Point source = new Point(2, 3);
Point target = new Point(-4, 9);
// You can utilize the cycle or implement kind of timer to animate something
for (int t = 0; t <= 100; t++) {
System.out.println(source.moveTo(0.01 * t, target));
}
}
}

https://replit.com/join/sucvdhpqoa-redneckz

@AlexanderAlexandrov我已经相应地将变量的类型更改为双倍,现在在我的一个类中,我有一个方法givePoints,它使用Scanner询问用户他想要多少点和坐标,然后将它们保存到一个点数组中,第一个元素始终为(0,0(。另一种方法将点阵列作为参数,并按照它们到点的距离(0,0(的顺序对它们进行排序。这些方法非常有效。问题出在方法hitThepoints上。在这里,我想首先创建点阵列,对它们进行排序,然后命令我的机器人命中所有点。机器人是类对象机器人延伸圆,位置类型为点,最初位于点(0,0(

public void hitThePoints(){
Point[] poi=sortPoints (givePoints()); //Creates a sorted array of points
Point short=new Point(poi[1].getX(),poi[1].getY());
System.out.println(" the nearest point is :");
System.out.println("("+short.getX()+ ","+short.getY()+")");

for(int i=1; i<poi.length;i++){
Point source=robot.position;
Point target=new Point(poi[i].getX(), poi[i].getY());
while(source.getX()!=target.getX() && 
source.getY()!=target.getY()){

robot.bewegeUm((source.moveTo(0.01,target)).getX(), 
(source.moveTo(0.01,target)).getY());
if(source.getX()!=target.getX() && 
source.getY()!=target.getY()){break;}
System.out.println(source.getX() +","+ source.getY());

}
}
}

相关内容

最新更新