Function FindCircleCenter(Return_Type As String, Radius As Double, _
x_0 As Double, y_0 As Double, _
x_1 As Double, y_1 As Double)
Dim MidptDist As Double
Dim x_coordinate As Double
Dim y_coordinate As Double
Dim x_mid As Double
Dim y_mid As Double
Dim Slope_Test As Double
Dim Orthogonal_Slope As Double
Dim Triangle_Height As Double
Dim Theta As Double
MidptDist = Distance_Between_Points(x_0, y_0, x_1, y_1) / 2
x_mid = Midpoint_Between_Points("X", x_0, y_0, x_1, y_1)
y_mid = Midpoint_Between_Points("Y", x_0, y_0, x_1, y_1)
Triangle_Height = Sqr(Radius ^ 2 - MidptDist ^ 2)
If MidptDist > Radius Then
FindCircleCenter = "no solution"
Exit Function
ElseIf MidptDist = Radius Then
x_coordinate = x_mid
y_coordinate = y_mid
GoTo ExitTheFunction
ElseIf MidptDist = 0 Then
FindCircleCenter = "infinite solutions"
Exit Function
End If
If x_0 = x_1 Then
x_coordinate = x_0 + Triangle_Height
y_coordinate = y_mid
GoTo ExitTheFunction
End If
Slope_Test = Slope_Between_Points(x_0, y_0, x_1, y_1)
If Slope_Test = 0 Then
x_coordinate = x_mid
y_coordinate = y_mid + Triangle_Height
GoTo ExitTheFunction
Else
Orthogonal_Slope = -1 / Slope_Test
Theta = Atn(Orthogonal_Slope)
x_coordinate = x_mid + Triangle_Height * Cos(Theta)
y_coordinate = y_mid + Triangle_Height * Sin(Theta)
End If
ExitTheFunction:
Select Case UCase(Left(Return_Type, 1))
Case Is = "X": FindCircleCenter = x_coordinate
Case Is = "Y": FindCircleCenter = x_coordinate
Case Else: FindCircleCenter = CVErr(xlErrValue)
End Select
End Function
Function Distance_Between_Points(x_0 As Double, y_0 As Double, _
x_1 As Double, y_1 As Double, _
Optional z_0 As Double = 0, _
Optional z_1 As Double = 0)
Dim x As Double
Dim y As Double
Dim z As Double
x = (x_1 - x_0) ^ 2
y = (y_1 - y_0) ^ 2
z = (z_1 - z_0) ^ 2
Distance_Between_Points = Sqr(x + y + z)
End Function
Function Slope_Between_Points(x_0 As Double, y_0 As Double, _
x_1 As Double, y_1 As Double)
Slope_Between_Points = (y_1 - y_0) / (x_1 - x_0)
End Function
Function Midpoint_Between_Points(Return_Type As String, _
x_0 As Double, y_0 As Double, _
x_1 As Double, y_1 As Double, _
Optional z_0 As Double = 0, _
Optional z_1 As Double = 0)
Dim x As Double
Dim y As Double
Dim z As Double
x = (x_1 - x_0) / 2
y = (y_1 - y_0) / 2
z = (z_1 - z_0) / 2
Select Case UCase(Left(Return_Type, 1))
Case Is = "X": Midpoint_Between_Points = x_0 + x
Case Is = "Y": Midpoint_Between_Points = y_0 + y
Case Is = "Z": Midpoint_Between_Points = z_0 + z
Case Else: Midpoint_Between_Points = CVErr(xlErrValue)
End Select
End Function