Simultaneous equation in VB?
Author Message Simultaneous equation in VB?

I require to calculate the intersection points of two overlapping circles.
The expression for the two circles are:

Circle1

(x-h)^2 + (y-k)^2 = r^2

Circle2

(x-m)^2 + (y-n)^2 = s^2

Where h,k,m and n are the respective centres offset from the zero origin and
r and s are the respective radii.

With pen and paper I can get the result by solving the two equations
simultaneously using the algebra method I learnt 35 years ago. The algebra
method involves a fair bit of expression manipulation before getting to the
point of being able to solve for x and then for y. To manipulate these
expressions in VB code seems rather complex so I figured there must be some
other method that can be used.

As an alternative I tried placing the two circle expressions in a For/Next
loop with a Step value of 0.0001 and incrementing  'y' in both expressions
by the Step amount until Cirlcle1 'x' value approximately matches Circle2
'x' value. This method returned some results but proved to be unreliable and
slow.

I would  be most appreciative if some of you Math/Code fundies could point
me in the right direction with this problem.

TIA

Ian Drennan

Tue, 14 Sep 2004 15:08:02 GMT  Simultaneous equation in VB?

Quote:

> I require to calculate the intersection points of two overlapping circles.
> The expression for the two circles are:

> Circle1

> (x-h)^2 + (y-k)^2 = r^2

> Circle2

> (x-m)^2 + (y-n)^2 = s^2

> Where h,k,m and n are the respective centres offset from the zero origin and
> r and s are the respective radii.

You can get an approximation, or at least find out if the circles
are likely to intersect at all, by finding where these squares
intersect:

line (h-r, k-r) - (h+r, k+r), , b
line (m-s, n-s) - (m+s, n+s), , b

Or maybe start by seeing if sqr((h-m)^2 + (k-n)^2) <= (r+s)

--
Joe Foster <mailto:jlfoster%40znet.com>     Got Thetans? <http://www.xenu.net/>
WARNING: I cannot be held responsible for the above        They're   coming  to
because  my cats have  apparently  learned to type.        take me away, ha ha!

Tue, 14 Sep 2004 16:38:56 GMT  Simultaneous equation in VB?

Quote:
>I require to calculate the intersection points of two overlapping circles.
>The expression for the two circles are:

>Circle1

>(x-h)^2 + (y-k)^2 = r^2

>Circle2

>(x-m)^2 + (y-n)^2 = s^2

>Where h,k,m and n are the respective centres offset from the zero origin and
>r and s are the respective radii.

>With pen and paper I can get the result by solving the two equations
>simultaneously using the algebra method I learnt 35 years ago. The algebra
>method involves a fair bit of expression manipulation before getting to the
>point of being able to solve for x and then for y. To manipulate these
>expressions in VB code seems rather complex so I figured there must be some
>other method that can be used.

>As an alternative I tried placing the two circle expressions in a For/Next
>loop with a Step value of 0.0001 and incrementing  'y' in both expressions
>by the Step amount until Cirlcle1 'x' value approximately matches Circle2
>'x' value. This method returned some results but proved to be unreliable and
>slow.

>I would  be most appreciative if some of you Math/Code fundies could point
>me in the right direction with this problem.

>TIA

>Ian Drennan

Try this website on for size...  the specific equations you need are at the
very end of the page.  However...  there are several other equations listed you
may find interesting and useful.

http://www.sonoma.edu/users/w/wilsonst/papers/geometry/circles/

Dan   :-)

Tue, 14 Sep 2004 17:36:31 GMT  Simultaneous equation in VB?
Many thanks Joe and Dan
With your help I have now managed to get my intercepts to work.
This is what the my code looks like for anyone else who might need to do
this.
Regards
Ian

'This calculates the intersection points of two overlapping circles
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub FindIntercepts()
Dim XintersectA As Single 'first  x intersect
Dim YintersectA As Single 'first y intersect
Dim XintersectB As Single 'second  x intersect
Dim YintersectB As Single 'second y intersect
Dim d, r, s, h, k, m, n As Single ' intermediate variables to make
expression easier to write and follow
Dim CheckOverLap As Boolean
r = R1
s = R2
h = Centre1X
k = Centre1Y
m = Centre2X
n = Centre2Y
d = Sqr((((m - h) ^ 2) + ((n - k) ^ 2)))

If Sqr((h - m) ^ 2 + (k - n) ^ 2) <= (r + s) Then ' Check for
overlap to avoid run time error
CheckOverLap = True

'expression to find the XintersectA value
''''''
XintersectA = ((m + h) / 2) + ((m - h) * ((r ^ 2) - (s ^ 2))) / (2
* (d ^ 2)) + (((n - k) / (2 * (d ^ 2))) * Sqr(((((r + s) ^ 2) - (d ^ 2)) *
((d ^ 2) - ((s - r) ^ 2)))))

'expression to find the YintersectA value
'''''
YintersectA = ((n + k) / 2) + ((n - k) * ((r ^ 2) - (s ^ 2))) / (2 *
(d ^ 2)) - (((m - h) / (2 * (d ^ 2))) * Sqr(((((r + s) ^ 2) - (d ^ 2)) * ((d
^ 2) - ((s - r) ^ 2)))))

'expression to find the XintersectB value
'''''
XintersectB = ((m + h) / 2) + ((m - h) * ((r ^ 2) - (s ^ 2))) / (2 *
(d ^ 2)) - (((n - k) / (2 * (d ^ 2))) * Sqr(((((r + s) ^ 2) - (d ^ 2)) * ((d
^ 2) - ((s - r) ^ 2)))))

'expression to find the YintersectB value
'''''
YintersectB = ((n + k) / 2) + ((n - k) * ((r ^ 2) - (s ^ 2))) / (2 *
(d ^ 2)) + (((m - h) / (2 * (d ^ 2))) * Sqr(((((r + s) ^ 2) - (d ^ 2)) * ((d
^ 2) - ((s - r) ^ 2)))))

Pic2.Cls
Pic2.Print " First X intersect         = " & Round(XintersectA, 2)
Pic2.Print " First Y intersect         = " & Round(YintersectA, 2)
Pic2.Print ""
Pic2.Print " Second X intersect   = " & Round(XintersectB, 2)
Pic2.Print " Second Y intersect   = " & Round(YintersectB, 2)
Pic2.Print ""

Else
Pic2.Print " Overlap =  " & CheckOverLap
MsgBox ("No overlap of circles.   Cannot solve."), , ("Intercept
Check")
End If

Pic1.ForeColor = vbYellow
Pic1.Circle (XintersectA, YintersectA), 0.3
Pic1.Circle (XintersectB, YintersectB), 0.3

End Sub

Quote:

> > I require to calculate the intersection points of two overlapping
circles.
> > The expression for the two circles are:

> > Circle1

> > (x-h)^2 + (y-k)^2 = r^2

> > Circle2

> > (x-m)^2 + (y-n)^2 = s^2

> > Where h,k,m and n are the respective centres offset from the zero origin
and
> > r and s are the respective radii.

> You can get an approximation, or at least find out if the circles
> are likely to intersect at all, by finding where these squares
> intersect:

> line (h-r, k-r) - (h+r, k+r), , b
> line (m-s, n-s) - (m+s, n+s), , b

> Or maybe start by seeing if sqr((h-m)^2 + (k-n)^2) <= (r+s)

> --
> Joe Foster <mailto:jlfoster%40znet.com>     Got Thetans?

<http://www.xenu.net/>

- Show quoted text -

Quote:
> WARNING: I cannot be held responsible for the above        They're
coming  to
> because  my cats have  apparently  learned to type.        take me away,
ha ha!

Wed, 15 Sep 2004 17:15:42 GMT

 Page 1 of 1 [ 4 post ]

Relevant Pages