Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Given 3 points, draw a circle thru them 5

Status
Not open for further replies.

GPerk

Programmer
Jul 6, 2002
161
US
I have 3 points on the screen, x1,y1; x2,y2; x3,y3.
How can I draw a circle thru them?
That is, how can I find the point that is equidistant from the 3 points?

Note that this point would lie on the intersection of the two lines that are perpendicular bisectors of the line segments (x1,y1)-(x2,y2) and (x2,y2)-(x3,y3). Is this the right approach?
Is there an API function that does this?
Any pointers/suggestions will be appreciated.
 
I don't know of an API or built-in function that will do this - I guess you'll have to figure out the center and radius using basic geometry.
 
Are these points randomly positioned? If so, surely what you want to achieve may not always be possible. If the three points happen to be in a straight line, for example, how can they all appear on the circumference of a circle?
 
Quite easy - you just need a circle of infinite radius...

Got one of those?

mmilan.
 
>If the three points happen to be in a straight line

This is the only situation where it is not possible. All other layouts of three points result in a triangle, and all triangles have a circumcircle on which the verticees lie.
 
Ah but there are an infinite number of straight lines that could arise from 3 such randomly positioned points!
 
We're straying dangerously close to considering the cardinality of infinite sets here...
 
Guilty as charged - getting back to the point, has GPerk considered the fact that many of his circles will be so large that not only will only part of the circle be visible but it may not be possible to view all 4 points (i.e. his 3 plus centre of circle) simultaneously (without scrolling)?

 
>>Note that this point would lie on the intersection of the two lines that are perpendicular bisectors of the line segments (x1,y1)-(x2,y2) and (x2,y2)-(x3,y3).
Is this the right approach?

<<
It will work.
You will need the formula for the intersection of the two lines, also.

>>
Is there an API function that does this?
<<

No. Down to geometry I'm afraid.
 
First, a big star for JGES. The exact solution I was looking for is at the URL that JGES gives:
Thanks to all that responded.

As to the case where the 3 points are in straight:
Wouldn't it still be possible to draw a circle thru them?
After all, Einstein said that space is curved - so, wouldn't that straight line eventually curve around and go thru the 3 points again!
I believe Einstein also said that parallel lines meet (somewhere out there).
 
There is no direct API for this purpose, however, you can draw a circle if you compute its center and radius.

The determinantal form of the equation of a circle is:
[tt]
| x²+y² x y 1|
|x1²+y1² x1 y1 1| = 0
|x2²+y2² x2 y2 1|
|x3²+y3² x3 y3 1|
[/tt]
where (x1, y1), (x2, y2) and (x3, y3) are three points which lie on the circle.

The above determinant can be expanded and rewritten in the following form.

a(x²+y²) + bx + cy + d = 0

The coefficients a, b, c and d can be evaluated by expanding the above determinant. (The expansion of the determinent is quite horrible!)

After determining these coefficients, the above equation can be rearranged in the standard or desired form of the circle, i.e.,

(x - p)² + (y - q)² = r²

Where (p, q) is the center of the circle and r is its radius. After p, q and r are known, the circle can be drawn easily.

See the following code which explains this method.
___
[tt]
Option Explicit
Dim Px(2) As Double, Py(2) As Double 'coordinates of the three points lying on the circle
Dim numPoints As Long 'number of points drawn
Private Sub Form_Load()
BackColor = 0
WindowState = vbMaximized
AutoRedraw = True
End Sub

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
On Error Resume Next
If numPoints = 0 Then Cls
Px(numPoints) = X
Py(numPoints) = Y
Circle (X, Y), 45, vbRed
numPoints = numPoints + 1
If numPoints = 3 Then
numPoints = 0
Dim p As Double, q As Double, r As Double
SolveCircle Px(0), Py(0), Px(1), Py(1), Px(2), Py(2), p, q, r
Circle (p, q), r, vbGreen
End If
End Sub

'this procedure takes the coordinates of the three points lying on the circle and computes its center and radius
Sub SolveCircle(X1 As Double, Y1 As Double, X2 As Double, Y2 As Double, X3 As Double, Y3 As Double, p As Double, q As Double, r As Double)
Dim a As Double, b As Double, c As Double, d As Double
'equation of circle in the form
' a * (x ^ 2 + y ^ 2) + b * x + c * y + d = 0
'having...

a = (X1 * Y2 + X2 * Y3 + X3 * Y1) - (X2 * Y1 + X3 * Y2 + X1 * Y3)

b = (X2 * X2 * Y1 + X3 * X3 * Y2 + X1 * X1 * Y3) - (X1 * X1 * Y2 + X2 * X2 * Y3 + X3 * X3 * Y1) _
+ (Y2 * Y2 * Y1 + Y3 * Y3 * Y2 + Y1 * Y1 * Y3) - (Y1 * Y1 * Y2 + Y2 * Y2 * Y3 + Y3 * Y3 * Y1)

c = (Y1 * Y1 * X2 + Y2 * Y2 * X3 + Y3 * Y3 * X1) - (Y2 * Y2 * X1 + Y3 * Y3 * X2 + Y1 * Y1 * X3) _
+ (X1 * X1 * X2 + X2 * X2 * X3 + X3 * X3 * X1) - (X2 * X2 * X1 + X3 * X3 * X2 + X1 * X1 * X3)

d = (X1 * X1 + Y1 * Y1) * (X3 * Y2 - X2 * Y3) _
+ (X2 * X2 + Y2 * Y2) * (X1 * Y3 - X3 * Y1) _
+ (X3 * X3 + Y3 * Y3) * (X2 * Y1 - X1 * Y2)

'solving for radius and center of the circle
p = -b / 2 / a
q = -c / 2 / a
r = Sqr(p * p + q * q - d / a)
End Sub[/tt]
___

Run the above code and click on the form randomly. A circle will be drawn passing through the three points defining the circle.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top