简体   繁体   English

向相反方向移动点

[英]Move points in the opposite direction

I am working on a project in Java. 我正在用Java开发一个项目。 I am trying to move the points p2 , p3 , p4 just outside the circumference of the circle in the opposite direction to the point p1. 我试图将点p2p3p4沿与点p1相反的方向移动到圆的圆周之外。 Below is the image, which describes the problem, I am trying to solve. 下图是描述我要解决的问题的图像。

将点移动到圆的边缘

//given two points, calculates the angle
public static double calcAngle(Point2D.Double p1, Point2D.Double p2) {
    double deltaX = p2.x - p1.x;
    double deltaY = p2.y - p1.y;
    return (Math.atan2(deltaY, deltaX) * 180 / Math.PI);
}

//calculates a point on a circle given the angle, center of the circle and the radius
public static Point2D.Double pointOnCircle(Point2D.Double point, double radius , double angle) {
    double x = Math.abs(point.x + (radius * Math.cos(angle  * Math.PI / 180F)));
    double y = Math.abs(point.y + (radius * Math.sin(angle  * Math.PI / 180F)));
    return new Point2D.Double(x,y);
}

How do I calculate the angle in Java coordinate system and destination co-ordinates for each of the points p2 , p3 , p4 ? 如何在Java坐标系和目标坐标中为每个点p2p3p4计算角度?

I am yet to try the code above and would like to know if my approach is right before proceeding, since it is a part of the bigger project. 我尚未尝试上面的代码,并且想知道我的方法在继续之前是否正确,因为它是较大项目的一部分。 Thanks in advance! 提前致谢!

Your general idea seems workable but overly complicated. 您的总体思路似乎可行,但过于复杂。 There is no need to convert from x/y-vector to angle and then back. 无需先将x / y矢量转换为角度再转换为角度。 SImply scaling vectors will be enough. 简单地缩放向量就足够了。

Point2D p = p2; // likewise for p3, p4
double factor = radius / p.distance(p1);
p.setLocation(p1.getX() + (p.getX() - p1.getX())*factor,
              p1.getY() + (p.getY() - p1.getY())*factor);

This takes the vector (p - p1) , ie the vector pointing from p1 towards p , scales it by factor and adds it to the position of p1 . 这将采用向量(p - p1) ,即从p1指向p的向量,按factor对其进行缩放并将其添加到p1的位置。 The factor is chosen such that the new distance is equal to radius . 选择该factor ,以使新距离等于radius

All of this will fail if p1 and p are the same, since in this case you'll have a division by zero. 如果p1p相同,所有这些将失败,因为在这种情况下,您将被零除。 If this can be a problem for you, you might want to ensure that factor is a finite number, eg using Double.isFinite(double) . 如果这对您来说可能是个问题,则可能需要确保factor是有限的,例如使用Double.isFinite(double)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM