views:

53

answers:

3

Sorry if this doesn't make sense... I know the length of the triangle segments and the xy coordinates of two points. How do I figure out the xy of the 3rd point?

+2  A: 

You can treat the lines with unknown end points as the radius of arcs with centres and the known points you can then calculate the intersection of the two arcs reasonably easily. There will be two possible answers for each case.

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

Edit

Here is an additional method that you may find easier to follow.

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

Edit 2

I see Bart beat me to the second site.

Jaydee
That all looks find and dandy, but I need an example of the equation. I vaguely understand how to do it mathematically, but that still won't help me figure out how to code it. I'm not very good with trig and need some serious help.
jevinkones
@jevinkones, Fine and dandy? *Need* an example? Perhaps showing a bit of appreciation and asking nicely for some pseudo code would result in better responses.
Bart Kiers
My apologies, that was pretty rude in retrospect. At the time I was (and still am) rushed to figure out how to do this. It means a lot that this was answered mathematically, I just wasn't sure how to phrase the question for what I actually needed the first time I asked and it came off as rude. No offense was intended and I am very appreciative of the answers, I just need more help because I'm not at all good with trig.
jevinkones
No worries, I know what stress is like:-)
Jaydee
@jevinkones, yeah, I thought that might have been the case. Cheers for your clarification!
Bart Kiers
Thanks again, Jaydee. Much appreciated!
jevinkones
A: 

Extending Jaydee's answer let

  1. the two known points be (0,0) and (0,d);
  2. R be the length of the segment from (0,0) to the unknown point;
  3. r be the length of the segment from (0,d) to the unknown point;

Then calculate x and a using equations 5 and 9 given in http://mathworld.wolfram.com/Circle-CircleIntersection.html. Calculate y=a/2. There are 2 possible points to complete the triangle: (x,y) and (x,-y).

Coding (untested)

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 ;
}
emory
Hi @emory, thanks for the response and the explanation of @Jaydee's answer. I'm still unsure of how to code those equations. Is it possible that you could provide a code sample?
jevinkones
I put the equations into code.
emory
Thank you, @emory! I really appreciate your help and code.
jevinkones
+2  A: 

Given the following picture (see: http://local.wasp.uwa.edu.au/~pbourke/geometry/2circle/):

alt text

here's a Java demo where I used the following variable names:

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

Which will produce the following output:

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 :: []

Note that the above is just a simple demo. Be careful with the floating point comparisons!

Bart Kiers
Thank you, Bart. This is awesome and I know I'll be able to blast through my troubles thanks to the help of you, Jaydee and Emory. Thank goodness for StackOverflow!!! Cheers!
jevinkones
@jevinkones, you're welcome.
Bart Kiers