[英]How to get the distance between two points and move one point along the line to reach the other
I am trying to move a point(100,100) to another point(450,500). 我正在尝试将一个点(100,100)移到另一个点(450,500)。 To do this I got the distance between the points by subtracting:
int dx = x2 - x1
and then subtracting: int dy = y2 - y1
. 为此,我要减去两点之间的距离:
int dx = x2 - x1
然后减去: int dy = y2 - y1
。 I get 1% of of dy (dy / 100) * 1;
我得到
dy (dy / 100) * 1;
1% dy (dy / 100) * 1;
and 1% of dx (dx / 100) * 1;
dx (dx / 100) * 1;
1% dx (dx / 100) * 1;
. 。 Then I add the 1% of each to x1 and y1 in order to move the point along the JFrame to reach the second point.
然后,将每个1%的值分别添加到x1和y1,以便沿JFrame移动该点以到达第二个点。
For some reason when I move the point in the tick()
method it misses the second point. 由于某些原因,当我在
tick()
方法中移动该点时,它会遗漏第二个点。 I have tried numerous different ways to achieve this. 我尝试了许多不同的方法来实现这一目标。 I assume it's a mathematical miscalculation or I am doing something fundamentally wrong.
我认为这是数学上的错误计算,或者我所做的事情根本上是错误的。
Here is my code: 这是我的代码:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JFrame;
public class Distance extends JFrame implements Runnable {
private int x1, y1;
private int x2, y2;
private int width, height;
private int dx, dy;
public Distance() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(850, 600);
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
init();
}
private void init() {
x1 = y1 = 100;
x2 = 450;
y2 = 500;
width = height = 20;
dx = x2 - x1;
dy = y2 - y1;
Thread thread = new Thread(this);
thread.start();
}
@Override
public void paint(Graphics g) {
g.setColor(Color.BLACK);
g.fillOval(x1, y1, width, height);
g.fillOval(x2, y2, width, height);
repaint();
}
private void tick() {
int moveX = (dx / 100) * 1;
int moveY = (dy / 100) * 1;
x1 += moveX;
y1 += moveY;
}
@Override
public void run() {
while(true) {
try {
tick();
Thread.sleep(100);
} catch(InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new Distance();
}
}
You should use double
s instead of int
s, otherwise, you're loosing precision. 您应该使用
double
而不是int
,否则会降低精度。
Since fillOval
only takes int
as parameters, you have to cast them but don't worry, it doesn't affect the rendering. 由于
fillOval
仅将int
作为参数,因此您必须强制转换它们,但不用担心,它不会影响渲染。
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class Distance extends JFrame implements Runnable {
private double x1, y1;
private double x2, y2;
private double width, height;
private double dx, dy;
public Distance() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(850, 600);
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
init();
}
private void init() {
x1 = y1 = 100;
x2 = 450;
y2 = 500;
width = height = 20;
dx = x2 - x1;
dy = y2 - y1;
Thread thread = new Thread(this);
thread.start();
}
@Override
public void paint(Graphics g) {
g.setColor(Color.BLACK);
g.fillOval((int)x1, (int)y1, (int)width, (int)height);
g.fillOval((int)x2, (int)y2, (int)width, (int)height);
repaint();
}
private void tick() {
double moveX = (dx / 100) * 1;
double moveY = (dy / 100) * 1;
x1 += moveX;
y1 += moveY;
}
@Override
public void run() {
while(true) {
try {
tick();
Thread.sleep(100);
} catch(InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new Distance();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.