Results 1 to 20 of 20

Thread: How can I calculate the point(s) where 2 circles cross

  1. #1

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    How can I calculate the point(s) where 2 circles cross

    I seem to be unable to tell this computer to calculate the crossing- points of circles. I have the centreposition and radius of the circles calculated, now I need to claculate the crossings.
    I know I have to use to formula Radius=SQR( (x+CenterX)^2 +(y+Centery)^2). But whow would I do that in code??

    I know I'm getting stupid, but........
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  2. #2
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    Re: How can I calculate the point(s) where 2 circles cross

    Take formula of circle #1:
    (x - x')^2 + (y - y') ^2 = r'^2
    and #2:
    (x - x'')^2 + (y - y'') ^2 = r''^2

    isolate Y:
    y = sqr(r'^2 - (x - x')^2) + y'
    y = sqr(r''^2 - (x - x'')^2) + y''

    Combine:
    sqr(r'^2 - (x - x')^2) + y' = sqr(r''^2 - (x - x'')^2) + y''

    Rearrange:
    sqr(r'^2 - (x - x')^2) - sqr(r''^2 - (x - x'')^2) = y'' - y'

    Square the whole thing:
    (sqr(r'^2 - (x - x')^2) - sqr(r''^2 - (x - x'')^2))^2 = (y'' - y')^2
    for simplicity, a=sqr(r'^2 - (x - x')^2), b=sqr(r''^2 - (x - x'')^2)

    Binomial expansion:
    r'^2 - (x^2 - 2*x*x' + x'^2) + r''^2 - (x^2 - 2*x*x'' + x''^2)-2ab = (y''-y')^2

    Simplify:
    -2x^2 + 2x*(x'+x'') - 2ab = (y''-y')^2 - r'^2 - r''^2 + x'^2 + x''^2

    For simplicity, (y''-y')^2 - r'^2 - r''^2 + x'^2 + x''^2=c
    -2x^2 + 2x*(x'+x'') - 2ab = c
    2x(x' + x'' - x) - 2ab = c

    As you can see. It's kindof complicated. I probably messed up the logic somewhere though. I can't bother to go on!
    Don't pay attention to this signature, it's contradictory.

  3. #3

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: How can I calculate the point(s) where 2 circles cross

    I figured out another way, forget the way using those circle formulas.

    If two circles cross at any point, the distance between them has to be within.

    Radius1+Radius2>= Distance>= Radius1-Radius2
    (with Radius1>=Radius2!)

    If they cross consider the connecting line from Centre1 to circle2 a one side of a triangle, the other sider are the radus1 and Radius2. So you've got 3 sides of a triangle, so you can calculate the angles of this triangle.

    To calculate the crossing points just use the direction from Circle1 to Circle2, add/subtract the angle from the triangle in Centre1. This will give the direction to both (or to the one and only) crossingpoint. The distance is of course Radius 1.

    That looks like a solution which I can easily put into code.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  4. #4
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: How can I calculate the point(s) where 2 circles cross

    The easy approach for me consists in applying a transformation of coordinates, doing the calculation for a special case and rolling back the transformation.

    For example, you can make a x,y shift such that the new coordinate origin is at the center of circle 1 (radius R) and next apply a rotation such that the center of circle 2 (radius r) lies on the positive x axis at a distance a from the origin. Then it's very straightforward to arrive at these coordinates for the points of intersection:

    x(int) = +(R2 + a2 - r2) / 2a
    y(int) = +/- Sqr(R2 - x(int)2)
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  5. #5
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: How can I calculate the point(s) where 2 circles cross

    Quote Originally Posted by opus
    If they cross consider the connecting line from Centre1 to circle2 a one side of a triangle, the other sider are the radus1 and Radius2. So you've got 3 sides of a triangle, so you can calculate the angles of this triangle.
    uhhh.

    I've spent most of today looking at this (I have a piece of a4 with doodles of triangles and circles on it).

    I can see how you get the distance from circleA to circleB, and using the radii as the other two sides of the triangle. However (and being its been a long time since I was at school) I cannot see how you would get the angles so easily...

    Is there a formula I've forgotten?

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  6. #6

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: How can I calculate the point(s) where 2 circles cross

    The quick (and dirty] answer is YES!

    And since this is also true for me:
    Quote Originally Posted by Ecniv
    (and being its been a long time since I was at school)
    I "googled" "Triangle SSS" and got:

    How to solve a traingle with 3 sides given
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  7. #7
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: How can I calculate the point(s) where 2 circles cross

    Thanks that link helped.
    Seems as I get older there is more crap and its harder to find the best search criteria

    Right. So I've coded up the solving of the triangle woo.
    Now to use that to find the proper position(s) of the cross points...

    Looking at my sketched diagram, I uh cannot see any right angles :/
    Christ, never knew I had forgotten maths so badly :/
    Can you point me to another link for getting the co-ordinates.

    I have a form (in Excel) which is accepting x1,y1, x2,y2 r1,r2 and hopefully will return one or two cross points of the circle (I don't want to look for more than that )

    EDIT: I found it... second triangle angle added to the one I've just found can produce the co-ordinates

    Lemme try and I'll post up again later
    Last edited by Ecniv; Apr 12th, 2005 at 05:39 AM.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  8. #8
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: How can I calculate the point(s) where 2 circles cross

    Ok hmm it ran - but I think its wrong.
    x1 = 1
    y1 = 1
    r1 = 3
    x2 = 5
    y2 = 5
    r2 = 4

    What should the answer be?

    Edit:
    these are mine (after I used the right radius instead of the line between the points.
    ResX1 = 3.997
    ResY1 = 1.128
    ResX2 = 1.128
    ResY2 = 3.997
    Last edited by Ecniv; Apr 12th, 2005 at 08:24 AM.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  9. #9
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: How can I calculate the point(s) where 2 circles cross

    Right, something has gone wrong with it and I cannot even get those result I previously had.

    I'm attaching the spreadsheet (up to you if you want to look and download).

    Any flaws in my logic I would appreciate the pointers
    * Note - the drawing part doesn't appearr to work right, which is one reason I think the values are wrong...
    Attached Files Attached Files

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  10. #10

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: How can I calculate the point(s) where 2 circles cross

    You got some errors in the code,
    I changed the ArcCos Function(some brackets) and you didn't check wether your point1 was left or right of point2, therefore your third point could be outside the desired triangle, I changed the code for you.
    VB Code:
    1. '---------
    2. '----   NOTE: Sticking with  R A D I A N S  as the resulting angles!!
    3. '---------
    4. '---- Check for side one
    5.     If blnDebug Then Debug.Print "Side 1"
    6.     dblSideA = dblR2
    7.     dblSideB = dblR1
    8.     dblSideC = Sqr(((dblX1 - dblX2) ^ 2) + ((dblY1 - dblY2) ^ 2))
    9.     dblAngleC = ((dblSideA ^ 2) + (dblSideB ^ 2) - (dblSideC ^ 2)) / (2 * dblSideA * dblSideB)
    10.     If blnDebug Then
    11.         Debug.Print "  Side A,B,C : " & dblSideA & " : " & dblSideB & " : " & Format(dblSideC, "#0.000")
    12. '        Debug.Print "   Before arccos - AngleC : " & dblAngleC
    13.     End If
    14.     dblAngleC = ArcCosR(dblAngleC)
    15.    
    16.     dblAngleA = ((dblSideA * Sin(dblAngleC)) / dblSideC)
    17.     dblAngleA = ArcSinR(dblAngleA)
    18.     dblAngleB = DegToRad(180 - RadToDeg(dblAngleC) - RadToDeg(dblAngleA))
    19.    
    20.     If blnDebug Then
    21.         Debug.Print "  Angles (R) A, B, C : " & Format(dblAngleA, "#0.00") & " : " & Format(dblAngleB, "#0.00") & " : " & Format(dblAngleC, "#0.00")
    22.         Debug.Print "  Angles (D) A, B, C : " & Format(RadToDeg(dblAngleA), "#0.00") & " : " & Format(RadToDeg(dblAngleB), "#0.00") & " : " & Format(RadToDeg(dblAngleC), "#0.00")
    23.     End If
    24.    
    25.     dblExternalAngle = RadToDeg(ArcSinR(dbly1y2Height / dblSideC))
    26.     dblTemp = dblExternalAngle + RadToDeg(dblAngleA)
    27.  [B]   'you have to check wether Point2 is to the left or right of Point1
    28.     If dblX1 <= dblX2 Then
    29.         txtResX1.Text = Format((Cos(DegToRad(dblTemp)) * dblR1) + dblX1, "#0.000")
    30.         txtResY1.Text = Format((Sin(DegToRad(dblTemp)) * dblR1) + dblY1, "#0.000")
    31.     Else
    32.         txtResX1.Text = Format(-(Cos(DegToRad(dblTemp)) * dblR1) + dblX1, "#0.000")
    33.         txtResY1.Text = Format(-(Sin(DegToRad(dblTemp)) * dblR1) + dblY1, "#0.000")
    34.     End If[/B]
    35.    
    36. '---- Check for side two
    37.     If blnDebug Then Debug.Print "Side 2"
    38.     dblSideA = dblR1
    39.     dblSideB = dblR2
    40.     dblSideC = Sqr(((dblX1 - dblX2) ^ 2) + ((dblY1 - dblY2) ^ 2))
    41.     dblAngleC = ((dblSideA ^ 2) + (dblSideB ^ 2) - (dblSideC ^ 2)) / (2 * dblSideA * dblSideB)
    42.     If blnDebug Then
    43.         Debug.Print "  Side A,B,C : " & dblSideA & " : " & dblSideB & " : " & Format(dblSideC, "#0.000")
    44. '        Debug.Print "   Before arccos - AngleC : " & dblAngleC
    45.     End If
    46.     dblAngleC = ArcCosR(dblAngleC)
    47.    
    48.     dblAngleA = ((dblSideA * Sin(dblAngleC)) / dblSideC)
    49.     dblAngleA = ArcSinR(dblAngleA)
    50.     dblAngleB = DegToRad(180 - RadToDeg(dblAngleC) - RadToDeg(dblAngleA))
    51.    
    52.     If blnDebug Then
    53.         Debug.Print "  Angles (R) A, B, C : " & Format(dblAngleA, "#0.00") & " : " & Format(dblAngleB, "#0.00") & " : " & Format(dblAngleC, "#0.00")
    54.         Debug.Print "  Angles (D) A, B, C : " & Format(RadToDeg(dblAngleA), "#0.00") & " : " & Format(RadToDeg(dblAngleB), "#0.00") & " : " & Format(RadToDeg(dblAngleC), "#0.00")
    55.     End If
    56.    
    57.     dblExternalAngle = RadToDeg(ArcSinR(dbly1y2Height / dblSideC))
    58.     dblTemp = dblExternalAngle + RadToDeg(dblAngleA)
    59.  [B]   If dblX1 <= dblX2 Then
    60.         txtResX2.Text = Format(dblX2 - (Cos(DegToRad(dblTemp)) * dblR2), "#0.000")
    61.         txtResY2.Text = Format(dblY2 - (Sin(DegToRad(dblTemp)) * dblR2), "#0.000")
    62.     Else
    63.         txtResX2.Text = Format(dblX2 + (Cos(DegToRad(dblTemp)) * dblR2), "#0.000")
    64.         txtResY2.Text = Format(dblY2 + (Sin(DegToRad(dblTemp)) * dblR2), "#0.000")
    65.     End If
    66.    [/B]
    67.     If Err.Number <> 0 Then
    68.         MsgBox "Error : " & Err.Number & vbCrLf & Err.Description, vbOKOnly + vbInformation, "Error"
    69.         txtResX1.Text = ""
    70.         txtResY1.Text = ""
    71.         txtResX2.Text = ""
    72.         txtResY2.Text = ""
    73.         lblresult.Caption = "Error"
    74.     Else
    75.         lblresult.Caption = "Complete"
    76.     End If
    77.    
    78. End Sub
    And
    VB Code:
    1. Public Function ArcCosR(ByVal dblX As Double) As Double
    2.     On Error Resume Next
    3.         'Changed some brackets!!
    4.         ArcCosR = Atn(-dblX / Sqr(-dblX * dblX + 1)) + 2 * Atn(1)
    5. End Function
    I didn't look at your circle drawing!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  11. #11
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: How can I calculate the point(s) where 2 circles cross

    Thanks for taking the time to check. I thought there was a problem with the +/- depending on the direction of the triangle.

    However, it is still not working right, the calced values on
    10,10,30
    50,50,40

    gives (as the crossing points):
    13.341,39.813
    38.240,11.768


    But this internal triangle should give the exact opposites for the crossing points. I did a very rough sketch that appeared to prove this.

    I'll look again at it, as I think one calc is messing up somewhere

    **Edit:
    Found it !!
    Stupid brackets on the arccos were returning the wrong result. Fixed that.
    Now on to the actual positions and drawing it
    Last edited by Ecniv; Apr 14th, 2005 at 04:53 AM.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  12. #12
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: How can I calculate the point(s) where 2 circles cross

    Hi,

    An updated spreadsheet.
    The test data I've used is in the top right, the green appears to work, only the red one doesn't (not sure why yet - but this means that there are more problems inside it).

    Any advice?

    Oh the direction thing (x1 to x2 etc) I got around and use a direction angle, which when past a certain point becomes a negative sin/cos... And works for all but the one test.

    I'm gonna take a break from it and try again tomorrow probably.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  13. #13

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: How can I calculate the point(s) where 2 circles cross

    Sorry, but I can't open your file???
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  14. #14
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: How can I calculate the point(s) where 2 circles cross

    Weird...

    Ok, here's an updated file and the final spreadsheet/code.
    I tried uploading it before, but I guess it never made it (last workplace had a crap net connection after 10am...

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  15. #15

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: How can I calculate the point(s) where 2 circles cross

    Still weird, but WindowsXP tells me it doesn'T "know" the method for kompressing.
    I still can download and open the first .Zip file, but the last two i can't.
    I'm so sorry...............................
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  16. #16
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: How can I calculate the point(s) where 2 circles cross

    Same here at the new work place - no winzip inistalled .. so perhaps that is why :/

    I'll remove the zips and attach the xls. OR not... Ok rename the txt to xls and it should (in theory) work
    Attached Files Attached Files

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  17. #17

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: How can I calculate the point(s) where 2 circles cross

    Hi,
    I fixed some of the logics 8espacially the calculation of direction and of the angle.
    you had lots of mixups with Radians and degree.
    And you still have lots of variables that are not needed.

    For the calculation of the Angle, I went the easy way, and just calculated one angle and used this one for both crossing points.

    In the drawing I changed the Offset and the fact that you were adding the Y-Value, although VB "counts" Y the other way (downward is +!)
    Last edited by opus; Jul 18th, 2007 at 02:52 AM.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  18. #18
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: How can I calculate the point(s) where 2 circles cross

    uh.. just typed in the top example circles and used the draw lines function (unless this is messed up) it doesn't draw right

    opus results
    257.130,8.333
    142.870,8.333

    my results
    60.946,343.750
    339.054,343.750
    If you can prove that (a) my results are wrong and (b) that the drawing is also wrong, fair enough.

    I am pretty sure I uploaded the correct version.

    Those pesky Radians / Degrees

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  19. #19

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: How can I calculate the point(s) where 2 circles cross

    Sorry, I messed up with the versions of your/my triangle file.
    Here should be the correct one!
    Last edited by opus; Jul 18th, 2007 at 02:52 AM.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  20. #20
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: How can I calculate the point(s) where 2 circles cross

    Thanks for that. Thought it was working ok - but I guess not huh
    Well that was brain stretching.. time for some relaxing -----> Data entry - very little thought required

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width