Centre and radius of a circle passing through 3 points in a plane: Difference between revisions

Added various BASIC dialects (BASIC256, Chipmunk Basic, Gambas, PureBasic, Yabasic)
m (→‎{{header|Phix}}: added an assertion)
(Added various BASIC dialects (BASIC256, Chipmunk Basic, Gambas, PureBasic, Yabasic))
Line 1:
{{draft task}}
Write a function which returns the centre and radius of a circle passing through three point in a plane. Demonstrate the function using the points (22.83,2.07) (14.39,30.24) and (33.65,17.31)
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">call findCircle(22.83, 2.07, 14.39, 30.24, 33.65, 17.31)
end
 
subroutine findCircle(x1, y1, x2, y2, x3, y3)
x12 = x1 - x2
x13 = x1 - x3
y12 = y1 - y2
y13 = y1 - y3
y31 = y3 - y1
y21 = y2 - y1
x31 = x3 - x1
x21 = x2 - x1
 
sx13 = x1 * x1 - x3 * x3
sy13 = y1 * y1 - y3 * y3
sx21 = x2 * x2 - x1 * x1
sy21 = y2 * y2 - y1 * y1
 
f = (sx13 * x12 + sy13 * x12 + sx21 * x13 + sy21 * x13) / (y31 * x12 - y21 * x13) / 2
g = (sx13 * y12 + sy13 * y12 + sx21 * y13 + sy21 * y13) / (x31 * y12 - x21 * y13) / 2
 
c = -x1 * x1 - y1 * y1 - 2 * g * x1 - 2 * f * y1
h = -g
k = -f
r = sqr(h * h + k * k - c)
 
print "Centre is at ("; h; ", "; k; ")"
print "Radius is "; r
print
print "Check radius as the distance between the centre and the first point:"
print sqr((22.83 - h) ^ 2 + (2.07 - k) ^ 2)
end subroutine</syntaxhighlight>
{{out}}
<pre>Centre is at (18.9785156601, 16.2654107977)
Radius is 14.708624
 
Check radius as the distance between the centre and the first point:
14.708624</pre>
 
==={{header|Chipmunk Basic}}===
{{trans|FreeBASIC}}
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="vbnet">100 sub findcircle(x1,y1,x2,y2,x3,y3)
110 x12 = x1-x2
120 x13 = x1-x3
130 y12 = y1-y2
140 y13 = y1-y3
150 y31 = y3-y1
160 y21 = y2-y1
170 x31 = x3-x1
180 x21 = x2-x1
190 '
200 sx13 = x1*x1-x3*x3
210 sy13 = y1*y1-y3*y3
220 sx21 = x2*x2-x1*x1
230 sy21 = y2*y2-y1*y1
240 '
250 f = (sx13*x12+sy13*x12+sx21*x13+sy21*x13)/(y31*x12-y21*x13)/2
260 g = (sx13*y12+sy13*y12+sx21*y13+sy21*y13)/(x31*y12-x21*y13)/2
270 '
280 c = -x1*x1-y1*y1-2*g*x1-2*f*y1
290 h = -g
300 k = -f
310 r = sqr(h*h+k*k-c)
320 '
330 print "Centre is at ( ";h;", ";k;")"
340 print "Radius is ";r
350 print
360 print "Check radius as the distance between the centre and the first point:"
370 print sqr((22.83-h)^2+(2.07-k)^2)
380 end sub
390 cls
400 findcircle(22.83,2.07,14.39,30.24,33.65,17.31)
410 end</syntaxhighlight>
 
==={{header|Gambas}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">Sub findCircle(x1 As Float, y1 As Float, x2 As Float, y2 As Float, x3 As Float, y3 As Float)
 
Dim x12 As Float = x1 - x2
Dim x13 As Float = x1 - x3
Dim y12 As Float = y1 - y2
Dim y13 As Float = y1 - y3
Dim y31 As Float = y3 - y1
Dim y21 As Float = y2 - y1
Dim x31 As Float = x3 - x1
Dim x21 As Float = x2 - x1
Dim sx13 As Float = x1 * x1 - x3 * x3
Dim sy13 As Float = y1 * y1 - y3 * y3
Dim sx21 As Float = x2 * x2 - x1 * x1
Dim sy21 As Float = y2 * y2 - y1 * y1
Dim f As Float = (sx13 * x12 + sy13 * x12 + sx21 * x13 + sy21 * x13) / (y31 * x12 - y21 * x13) / 2
Dim g As Float = (sx13 * y12 + sy13 * y12 + sx21 * y13 + sy21 * y13) / (x31 * y12 - x21 * y13) / 2
Dim c As Float = -x1 * x1 - y1 * y1 - 2 * g * x1 - 2 * f * y1
Dim h As Float = -g
Dim k As Float = -f
Dim r As Float = Sqr(h * h + k * k - c)
Print "Centre is at (" & h & ", " & k & ")"
Print "Radius is "; r
Print
Print "Check radius as the distance between the centre and the first point:"
Print Sqr((22.83 - h) ^ 2 + (2.07 - k) ^ 2)
 
End Sub
 
Public Sub Main()
findCircle(22.83, 2.07, 14.39, 30.24, 33.65, 17.31)
End </syntaxhighlight>
{{out}}
<pre>Centre is at (18.9785156601488, 16.2654107977159)
Radius is 14,7086239783342
 
Check radius as the distance between the centre and the first point:
14,7086239783342</pre>
 
==={{header|PureBasic}}===
<syntaxhighlight lang="basic">Procedure.d findCircle(x1, y1, x2, y2, x3, y3)
Define.d x12, x13, y12, y13, y31, y21, x31, x21, sx13, sy13, sx21, sy21
Define.d f, g, c, h, k, r
x12 = x1 - x2
x13 = x1 - x3
y12 = y1 - y2
y13 = y1 - y3
y31 = y3 - y1
y21 = y2 - y1
x31 = x3 - x1
x21 = x2 - x1
sx13 = x1 * x1 - x3 * x3
sy13 = y1 * y1 - y3 * y3
sx21 = x2 * x2 - x1 * x1
sy21 = y2 * y2 - y1 * y1
f = (sx13 * x12 + sy13 * x12 + sx21 * x13 + sy21 * x13) / (y31 * x12 - y21 * x13) / 2
g = (sx13 * y12 + sy13 * y12 + sx21 * y13 + sy21 * y13) / (x31 * y12 - x21 * y13) / 2
c = -x1 * x1 - y1 * y1 - 2 * g * x1 - 2 * f * y1
h = -g
k = -f
r = Sqr(h * h + k * k - c)
PrintN("Centre is at (" + Str(h) + ", " + Str(k) + ")")
PrintN("Radius is " + Str(r))
PrintN("")
PrintN("Check radius as the distance between the centre and the first point: " + Str(Sqr(Pow((22.83 - h), 2) + Pow((2.07 - k), 2))))
EndProcedure
 
OpenConsole()
findCircle(22.83, 2.07, 14.39, 30.24, 33.65, 17.31)
PrintN(#CRLF$ + "--- terminado, pulsa RETURN---"): Input()
PrintN(#CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()</syntaxhighlight>
 
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="qbasic">findCircle(22.83, 2.07, 14.39, 30.24, 33.65, 17.31)
END
 
SUB findCircle(x1, y1, x2, y2, x3, y3)
x12 = x1 - x2
x13 = x1 - x3
y12 = y1 - y2
y13 = y1 - y3
y31 = y3 - y1
y21 = y2 - y1
x31 = x3 - x1
x21 = x2 - x1
 
sx13 = x1 * x1 - x3 * x3
sy13 = y1 * y1 - y3 * y3
sx21 = x2 * x2 - x1 * x1
sy21 = y2 * y2 - y1 * y1
 
f = (sx13 * x12 + sy13 * x12 + sx21 * x13 + sy21 * x13) / (y31 * x12 - y21 * x13) / 2
g = (sx13 * y12 + sy13 * y12 + sx21 * y13 + sy21 * y13) / (x31 * y12 - x21 * y13) / 2
 
c = -x1 * x1 - y1 * y1 - 2 * g * x1 - 2 * f * y1
h = -g
k = -f
r = SQR(h * h + k * k - c)
 
PRINT "Centre is at (", h, ", ", k, ")"
PRINT "Radius is ", r
PRINT
PRINT "Check radius as the distance between the centre and the first point:"
PRINT SQR((22.83 - h) ^ 2 + (2.07 - k) ^ 2)
END SUB</syntaxhighlight>
{{out}}
<pre>Centre is at (18.9785, 16.2654)
Radius is 46804.6
 
Check radius as the distance between the centre and the first point:
46804.6</pre>
 
=={{header|C++}}==
2,169

edits