AS3 / Java - 通过了解其他两个点和段长度来找到三角形点

时间:2010-09-08 08:26:42

标签: java actionscript-3 math geometry trigonometry

对不起,如果这没有意义......我知道三角形段的长度和两点的xy坐标。我怎么弄清楚第3点的xy?

3 个答案:

答案 0 :(得分:5)

鉴于以下图片(见http://local.wasp.uwa.edu.au/~pbourke/geometry/2circle/):

alt text

这是一个Java演示,我使用以下变量名称:

 picture above | Java code
---------------+----------------
 P0            | p1
 P1            | p2
 P2            | temp
 P3            | p3
 a             | a
 (a+b)         | d
 h             | h
 r0            | distanceFromP1
 r1            | distanceFromP2

public class Main {

    public static Point[] getP3(Point p1, double distanceFromP1, Point p2, double distanceFromP2) {
        double d = p1.distance(p2);

        if(d > (distanceFromP1 + distanceFromP2) || p1.equals(p2) || d < Math.abs(distanceFromP1 - distanceFromP2)) {
            // there does not exist a 3rd point, or there are an infinite amount of them
            return new Point[]{};
        }

        double a = (distanceFromP1*distanceFromP1 - distanceFromP2*distanceFromP2 + d*d) / (2*d);
        double h = Math.sqrt(distanceFromP1*distanceFromP1 - a*a);

        Point temp = new Point(p1.x + a*(p2.x - p1.x) / d, p1.y + a*(p2.y - p1.y) / d);

        return new Point[]{
                new Point(temp.x + h * (p2.y - p1.y) / d, temp.y - h * (p2.x - p1.x) / d),
                new Point(temp.x - h * (p2.y - p1.y) / d, temp.y + h * (p2.x - p1.x) / d)
        };
    }

    public static void main(String[]args) throws Exception {
        Point a = new Point(1,1);
        Point b = new Point(5,4);
        Point c = new Point(0,0);
        Point d = new Point(2,0);
        System.out.println("test 1 :: "+Arrays.toString(getP3(a, 4, b, 3)));       // 2 distinct 3rd points
        System.out.println("test 2 :: "+Arrays.toString(getP3(c, 1, d, 1)));       // 1 distinct 3rd point
        System.out.println("test 3 :: "+Arrays.toString(getP3(c, 0.99999, d, 1))); // none
        System.out.println("test 4 :: "+Arrays.toString(getP3(d, 1, d, 1)));       // infinite
        System.out.println("test 5 :: "+Arrays.toString(getP3(c, 50, d, 1)));      // none, one circle "contains" the other
    }
}

class Point {

    final double x;
    final double y;
    private final int hash;

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
        this.hash = Double.valueOf(x).hashCode() ^ Double.valueOf(y).hashCode();
    }

    public double distance(Point that) {
        double dX = this.x - that.x;
        double dY = this.y - that.y;
        return Math.sqrt(dX*dX + dY*dY);
    }

    @Override
    public boolean equals(Object o) {
        if(o == null || getClass() != o.getClass()) return false;
        Point that = (Point)o;
        return this.x == that.x && this.y == that.y;
    }

    @Override
    public int hashCode() {
        return  hash;
    }

    @Override
    public String toString() {
        return String.format("(x=%f, y=%f)", x, y);
    }
}

将产生以下输出:

test 1 :: [(x=5.000000, y=1.000000), (x=2.120000, y=4.840000)]
test 2 :: [(x=1.000000, y=0.000000), (x=1.000000, y=0.000000)]
test 3 :: []
test 4 :: []
test 5 :: []

请注意,上面只是一个简单的演示。小心浮点比较!

答案 1 :(得分:2)

您可以将具有未知终点的线条视为具有中心的弧的半径,然后您可以合理地轻松计算两个弧线的交点。每个案例都有两个可能的答案。

http://mathworld.wolfram.com/Circle-CircleIntersection.html

修改

以下是您可能更容易理解的其他方法。

http://local.wasp.uwa.edu.au/~pbourke/geometry/2circle/

编辑2

我看到巴特把我打到了第二个网站。

答案 2 :(得分:0)

扩展Jaydee's answer

  1. 两个已知点是(0,0)和(0,d);
  2. R是从(0,0)到未知点的段的长度;
  3. r是从(0,d)到未知点的段的长度;
  4. 然后使用http://mathworld.wolfram.com/Circle-CircleIntersection.html中给出的等式5和9计算x和a。计算y = a / 2。 完成三角形有两个可能的点:(x,y)和(x,-y)。

    编码(未经测试)

    double x ( double d , double R , double r )
    {
        return ( d * d - r * r + R * R ) / ( 2 * d ) ;
    }
    
    double a ( double d , double R , double r )
    {
        return ( Math . sqrt ( ( - d + r - R ) * ( -d - r + R ) * ( - d + r + R ) * ( d + r + R ) ) / d ) ;
    }
    
    double y ( double d , double R , double r )
    {
        return a ( d , R , r ) / 2 ;
    }