Wow, you guys are making this WAY too complicated, with all your fancy algebraic equations. That1Smart Guy is living up to his name--he's got the idea. You simply find the perpendicular bisectors of each side of the triangle and find the intersection point. That's the center. The radius is the distance to any of the vertices of the triangle. I'll post some code in a sec.
EDIT:
Crap--I forgot, I have DBP, not DBC. Anyways here's what I came up with:
Type Pos
X as Float
Y as Float
EndType
Type Line2D
P1 as Pos
P2 as Pos
EndType
Set Display Mode Desktop Width(), Desktop Height(), 32
Sync On
Position Mouse 710, 405
Line_Start()
Dim Points(2) as Pos
Dim MidPoints(2) as Pos
Dim Slopes(2) as Float
Dim PerpSlopes(2) as Float
Dim PerpBisectors(2) as Line2D
Circumcenter as Pos
CircumcircleRadius as Float
Points(0).X = 500
Points(0).Y = 400
Points(1).X = 200
Points(1).Y = 300
Points(2).X = 700
Points(2).Y = 600
CurPoint = 2
Do
CLS
Points(CurPoint - 1).X = MouseX()
Points(CurPoint - 1).Y = MouseY()
MidPoints(0).X = (Points(0).X + Points(1).X) / 2
MidPoints(0).Y = (Points(0).Y + Points(1).Y) / 2
MidPoints(1).X = (Points(1).X + Points(2).X) / 2
MidPoints(1).Y = (Points(1).Y + Points(2).Y) / 2
MidPoints(2).X = (Points(2).X + Points(0).X) / 2
MidPoints(2).Y = (Points(2).Y + Points(0).Y) / 2
Line Points(0).X, Points(0).Y, Points(1).X, Points(1).Y
Line Points(1).X, Points(1).Y, Points(2).X, Points(2).Y
Line Points(2).X, Points(2).Y, Points(0).X, Points(0).Y
If (ShiftKey())
Text Points(0).X, Points(0).Y - 20, "1"
Text Points(1).X, Points(1).Y - 20, "2"
Text Points(2).X, Points(2).Y - 20, "3"
EndIf
If (Shiftkey())
Circle MidPoints(0).X, MidPoints(0).Y, 5
Circle MidPoints(1).X, MidPoints(1).Y, 5
Circle MidPoints(2).X, MidPoints(2).Y, 5
EndIf
Slopes(0) = -(Points(1).Y - Points(0).Y) / (Points(1).X - Points(0).X)
Slopes(1) = -(Points(2).Y - Points(1).Y) / (Points(2).X - Points(1).X)
Slopes(2) = -(Points(0).Y - Points(2).Y) / (Points(0).X - Points(2).X)
If Slopes(0) = 0
Slopes(0) = 0.0001
EndIf
If Slopes(1) = 0
Slopes(1) = 0.0001
EndIf
If Slopes(2) = 0
Slopes(2) = 0.0001
EndIf
PerpSlopes(0) = -1 / Slopes(0)
PerpSlopes(1) = -1 / Slopes(1)
PerpSlopes(2) = -1 / Slopes(2)
PerpBisectors(0).P1.X = MidPoints(0).X - 1000
PerpBisectors(0).P1.Y = MidPoints(0).Y + (1000.0 * PerpSlopes(0))
PerpBisectors(0).P2.X = MidPoints(0).X + 1000
PerpBisectors(0).P2.Y = MidPoints(0).Y - (1000.0 * PerpSlopes(0))
PerpBisectors(1).P1.X = MidPoints(1).X - 1000
PerpBisectors(1).P1.Y = MidPoints(1).Y + (1000.0 * PerpSlopes(1))
PerpBisectors(1).P2.X = MidPoints(1).X + 1000
PerpBisectors(1).P2.Y = MidPoints(1).Y - (1000.0 * PerpSlopes(1))
PerpBisectors(2).P1.X = MidPoints(2).X - 1000
PerpBisectors(2).P1.Y = MidPoints(2).Y + (1000.0 * PerpSlopes(2))
PerpBisectors(2).P2.X = MidPoints(2).X + 1000
PerpBisectors(2).P2.Y = MidPoints(2).Y - (1000.0 * PerpSlopes(2))
LineIntersection(PerpBisectors(0).P1.X, PerpBisectors(0).P1.Y, PerpBisectors(0).P2.X, PerpBisectors(0).P2.Y, PerpBisectors(1).P1.X, PerpBisectors(1).P1.Y, PerpBisectors(1).P2.X, PerpBisectors(1).P2.Y)
Circumcenter.X = Line_ColX()
Circumcenter.Y = Line_ColY()
If (ShiftKey())
Circle Circumcenter.X, Circumcenter.Y, 5
EndIf
CircumcircleRadius = Dist2D(Points(0).X, Points(0).Y, Circumcenter.X, Circumcenter.Y)
Circle Circumcenter.X, Circumcenter.Y, CircumcircleRadius
If (ShiftKey())
Line PerpBisectors(0).P1.X, PerpBisectors(0).P1.Y, PerpBisectors(0).P2.X, PerpBisectors(0).P2.Y
Line PerpBisectors(1).P1.X, PerpBisectors(1).P1.Y, PerpBisectors(1).P2.X, PerpBisectors(1).P2.Y
Line PerpBisectors(2).P1.X, PerpBisectors(2).P1.Y, PerpBisectors(2).P2.X, PerpBisectors(2).P2.Y
EndIf
Print "Slope between 1 and 2: ";Slopes(0)
Print "Slope between 2 and 3: ";Slopes(1)
Print "Slope between 3 and 1: ";Slopes(2)
Print "Slope of perpindicular bisector of 1 and 2: ";PerpSlopes(0)
Print "Slope of perpindicular bisector of 2 and 3: ";PerpSlopes(1)
Print "Slope of perpindicular bisector of 3 and 1: ";PerpSlopes(2)
Print "Location of circumcenter: {";Circumcenter.X;", ";Circumcenter.Y;"}"
Print "Radius of circumcircle: ";CircumcircleRadius
Print ""
Print "Mouse Location: {";MouseX();", ";MouseY();"}"
Print "Scancode(): ";Scancode()
Print "FPS: ";Screen FPS()
Print ""
Print "Press the shift key to show extra visual information, such as perpendicular bisectors."
Sync
Loop
End
Function Dist2D(X1 as Float, Y1 as Float, X2 as Float, Y2 as Float)
TEMP# = Sqrt(((X2 - X1) * (X2 - X1)) + ((Y2 - Y1) * (Y2 - Y1)))
EndFunction TEMP#
rem This function should be called before the other 3 functions
function Line_Start()
global Line_Col_X as integer
global Line_Col_Y as integer
endfunction
rem This function returns the X-Coordinate of the last collision (so LineIntersection(...) has to be called before this function)
function Line_ColX()
endfunction Line_Col_X
rem This function returns the collisions Y-Coordinate
function Line_ColY()
endfunction Line_Col_Y
rem This function calculates
function LineIntersection(XS1,YS1,XE1,YE1, XS2,YS2,XE2,YE2)
rem Debugging: Vertical lines are not allowed!
if XS1 = XE1 then XE1 = XS1+1
if XS2 = XE2 then XE2 = XS2+1
rem First line
rem y=mx+b
rem Calculate m
xdif1 = (XE1-XS1)
ydif1 = (YE1-YS1)
m1# = ydif1/(1.0*xdif1)
rem Calculate b
b1# = YS1-XS1*m1#
rem Second line
rem Calculate m
xdif2 = (XE2-XS2)
ydif2 = (YE2-YS2)
m2# = ydif2/(1.0*xdif2)
rem Calculate b
b2# = YS2-XS2*m2#
rem Collision-Detection
rem m1#*x+b1# = m2#*x+b2# | -(m2#*x)-b1#
rem <=> (m1#-m2#)*x = b2#-b1# | /(m1#-m2#)
rem <=> x = (b2#-b1#)/(m1#-m2#)
rem Calculate X-Pos
x# = (b2#-b1#)/(m1#-m2#)
Line_Col_X = x#
rem Calculate Y-Pos
rem y=mx+b
rem y# = m1#*x#+b1#
y# = m1#*x#+b1#
Line_Col_Y = y#
rem Make sure that XS<XE, YS<YE
if XS1 > XE1 then XT1 = XS1 : XS1 = XE1 : XE1 = XT1` : YT1 = YS1 : YS1 = YE1 : YE1 = YT1
if YS1 > YE1 then YT1 = YS1 : YS1 = YE1 : YE1 = YT1`XT1 = XS1 : XS1 = XE1 : XE1 = XT1 : YT1 = YS1 : YS1 = YE1 : YE1 = YT1
if XS2 > XE2 then XT2 = XS2 : XS2 = XE2 : XE2 = XT2` : YT2 = YS2 : YS2 = YE2 : YE2 = YT2
if YS2 > YE2 then YT2 = YS2 : YS2 = YE2 : YE2 = YT2`XT2 = XS2 : XS2 = XE2 : XE2 = XT2 : YT2 = YS2 : YS2 = YE2 : YE2 = YT2
rem Check, if lines collide
if x# => XS1
if x# <= XE1
if x# => XS2
if x# <= XE2
if y# => YS1
if y# <= YE1
if y# => YS2
if y# <= YE2
exitfunction 1
endif
endif
endif
endif
endif
endif
endif
endif
endfunction 0
Thanks to Mr Kohlenstoff (sp?) for the line intersection. You can move one point of the triangle with the mouse. If anyone here is kind enough to convert this to DBC that'd be great.
However hopefully it should get across the general idea regardless of whether or not you can run it. Basically, first you find the slope and midpoint of each edge. Then you find the negative reciprocal of each edge:
NegRecip = -1 / Slope
That, combined with the midpoint of that edge will give you the edge's perpendicular bisector. Then you find the intersection of two of these lines using a function like the one I used, and the intersection point is the circumcenter. You can find the radius by finding the distance between any point and the circumcenter, which gives you your circumcircle.
If you have any questions feel free to ask, especially since you probably can't run the code.
Little screen shot because most of you don't have DBP:
EDIT2:
I'm working on a DBC-compatible function right now, so no one needs to convert this. Hopefully I can post it by tomorrow morning.
i like orange