finding a 3 point circe

BlitzMax Forums/BlitzMax Programming/finding a 3 point circe

Ryan Burnside(Posted 2006) [#1]
Hi, I need a good algorithem for finding the center and radius of a 3 point circle.

maybe something like:
radius(x1#,y1#,x2#,y2#,z3#,y3#)
center(x1#,y1#,x2#,y2#,z3#,y3#)

I know you have to connect the points and find the perpindicular linear intersections....that's all I know really.
Thanks in advance.


Genexi2(Posted 2006) [#2]
Cant recall off the top of my head, but wouldn't finding the average of the X and the Y of the given set of points do the trick without resorting to triangulating the circle or any other means of doing so?


Byteemoz(Posted 2006) [#3]
How about that:
SuperStrict

Function CircleFromPoints(p1x#, p1y#, p2x#, p2y#, p3x#, p3y#, cx# Var, cy# Var, rad# Var)
	Local A#, B#, C#, D#, E#, F#, G#
	A = p2x - p1x
	B = p2y - p1y
	C = p3x - p1x
	D = p3y - p1y

	E = A * (p1x + p2x) + B * (p1y + p2y)
	F = C * (p1x + p3x) + D * (p1y + p3y)	
	G = 2 * (A * (p3y - p2y) - B * (p3x - p2x))
	
	If G = 0 Then
		cx = 0.0
		cy = 0.0
		rad = 0.0
	Else
		cx = (D * E - B * F) / G
		cy = (A * F - C * E) / G
		rad = Sqr((p1x - cx) ^ 2 + (p1y - cy) ^ 2)
	EndIf
EndFunction

' a test
AppTitle = "LMB = point1 ; MMB = point2 ; RMB = point3"
Graphics 800, 600

Local p1x# = 400, p1y# = 250, p2x# = 350, p2y# = 350, p3x# = 450, p3y# = 350, cx#, cy#, rad#

Repeat
	WaitEvent
	
	If MouseHit(MOUSE_LEFT) Then p1x = MouseX() ; p1y = MouseY()
	If MouseHit(MOUSE_MIDDLE) Then p2x = MouseX() ; p2y = MouseY()
	If MouseHit(MOUSE_RIGHT) Then p3x = MouseX() ; p3y = MouseY()
	
	CircleFromPoints p1x, p1y, p2x, p2y, p3x, p3y, cx, cy, rad
	
	Cls
	SetColor 255, 255, 255
	DrawOval cx - rad, cy - rad, rad * 2, rad * 2
	SetColor 0, 64, 0
	DrawOval cx - 1, cy - 1, 3, 3
	SetColor 255, 0, 0
	DrawRect p1x - 1, p1y - 1, 3, 3
	DrawRect p2x - 1, p2y - 1, 3, 3
	DrawRect p3x - 1, p3y - 1, 3, 3
	Flip
Until KeyHit(KEY_ESCAPE) Or AppTerminate()
( from http://cgafaq.info/wiki/Circle_Through_Three_Points )

-- Byteemoz


Ryan Burnside(Posted 2006) [#4]
Thanks this works very well.


Beaker(Posted 2006) [#5]
That should be put in the code archive.


Byteemoz(Posted 2006) [#6]
Has been added: Circle through three points
-- Byteemoz