Java 2D:将点P移至距另一个点一定距离的位置? [英] Java 2D: Moving a point P a certain distance closer to another point?

查看:70
本文介绍了Java 2D:将点P移至距另一个点一定距离的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将Point2D.Double移近另一个Point2D.Double的最佳方法是什么?

What is the best way to go about moving a Point2D.Double x distance closer to another Point2D.Double?

试图进行编辑,但是为了维护而去了.不,这不是功课

Tried to edit, but so went down for maintenance. No this is not homework

我需要将飞机(A)移向跑道(C)的尽头,并指向正确的方向(角度a).

I need to move a plane (A) towards the end of a runway (C) and point it in the correct direction (angle a).

替代文字http://img246.imageshack.us/img246/9707/planec. png

到目前为止,这里是我所拥有的,但是似乎很混乱,执行这样的事情的通常方法是什么?

Here is what I have so far, but it seems messy, what is the usual way to go about doing something like this?

    //coordinate = plane coordinate (Point2D.Double)
    //Distance = max distance the plane can travel in this frame

    Triangle triangle = new Triangle(coordinate, new Coordinate(coordinate.x, landingCoordinate.y),  landingCoordinate);

    double angle = 0;

    //Above to the left
    if (coordinate.x <= landingCoordinate.x && coordinate.y <= landingCoordinate.y)
    {
        angle = triangle.getAngleC();
        coordinate.rotate(angle, distance);
        angle = (Math.PI-angle);
    }
    //Above to the right
    else if (coordinate.x >= landingCoordinate.x && coordinate.y <= landingCoordinate.y)
    {
        angle = triangle.getAngleC();
        coordinate.rotate(Math.PI-angle, distance);
        angle = (Math.PI*1.5-angle);
    }

    plane.setAngle(angle);

可以在 http://pastebin.com/RtCB2kSZ

请记住,飞机可以在跑道点附近的任何位置

Bearing in mind the plane can be in in any position around the runway point

推荐答案

要解救的载体!

给出点A和B.创建一个从A到B的向量V(通过执行B-A).将向量V归一化为单位向量,然后将其乘以所需的距离d,最后将所得向量添加到点A.即:

Given points A and B. Create a vector V from A to B (by doing B-A). Normalize vector V into a unit vector and then just multiply it with the distance, d, you want and finally add the resulting vector to point A. ie:

  A_moved = A + |(B-A)|*d

Java(英语)

  Vector2D a_moved = a.add(b.subtract(a).norm().multiply(d));

没有角度,不需要讨厌的事情.

No angles, no nasty trig needed.

这篇关于Java 2D:将点P移至距另一个点一定距离的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆