如何获取两点之间的距离并沿线移动一个点到达另一个点

How to get the distance between two points and move one point along the line to reach the other

我正在尝试将一个点 (100,100) 移动到另一个点 (450,500)。为此,我通过减去:int dx = x2 - x1 然后减去:int dy = y2 - y1 得到点之间的距离。我得到 dy (dy / 100) * 1; 的 1% 和 dx (dx / 100) * 1; 的 1%。然后我将每个的 1% 添加到 x1 和 y1,以便沿着 JFrame 移动该点以到达第二个点。

出于某种原因,当我在 tick() 方法中移动点时,它错过了第二个点。我尝试了许多不同的方法来实现这一目标。我认为这是数学上的错误计算,或者我做的事情从根本上是错误的。

这是我的代码:

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

你应该使用 doubles 而不是 ints,否则你会失去精度。

因为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();
    }
}