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

(Added Algol 68)
(5 intermediate revisions by 4 users not shown)
Line 1:
{{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|ALGOL 68}}==
Line 343 ⟶ 349:
 
Check radius as the distance between the centre and the first point: 14.70862397833418</pre>
 
=={{header|EasyLang}}==
{{trans|Wren}}
<syntaxhighlight>
proc circ x1 y1 x2 y2 x3 y3 . cx cy cr .
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
cx = -g
cy = -f
cr = sqrt (cx * cx + cy * cy - c)
.
circ 22.83 2.07 14.39 30.24 33.65 17.31 cx cy cr
print "Centre: (" & cx & ", " & cy & ") Radius: " & cr
</syntaxhighlight>
 
{{out}}
<pre>
Centre: (18.98, 16.27) Radius: 14.71
</pre>
 
=={{header|F_Sharp|F#}}==
Line 401 ⟶ 439:
Check radius as the distance between the centre and the first point:
14.70862397833418</pre>
 
=={{header|jq}}==
'''Adapted from [[#Julia|Julia]]'''
 
'''Works with jq, the C implementation of jq'''
 
'''Works with gojq, the Go implementation of jq'''
<syntaxhighlight lang="jq">
# Emit {x,y,r} corresponding to the circle through the three points
# specified as [x,y] pairs.
def findcircle($p1; $p2; $p3):
 
def assertEq($p; $q): if ($p - $q)|length < 1e-12 then . else "assertion failed: \($p) != \($q)" | error end;
 
def ss($a; $b) : ($a|.*.) + ($b|.*.);
$p1 as [$a,$b]
| $p2 as [$c,$d]
| $p3 as [$e,$f]
 
| ($a - $e) as $ae
| ($d - $b) as $db
| ($b - $f) as $bf
| ($e - $c) as $ec
| ($c - $a) as $ca
| ($f - $d) as $fd
 
| ss($a; $b) as $a2b2
| ss($c; $d) as $c2d2
| ss($e; $f) as $e2f2
 
| {x: (0.5 * ($a2b2 * $fd + $c2d2 * $bf + $e2f2 * $db) / ($a * $fd + $c * $bf + $e * $db)),
y: (0.5 * ($a2b2 * $ec + $c2d2 * $ae + $e2f2 * $ca) / ($b * $ec + $d * $ae + $f * $ca)) }
# any one of these should do / be nearly identical:
| [ss(.x-$a; .y-$b), ss(.x-$c; .y-$d), ss(.x-$e; .y-$f)] as $r123
| assertEq( $r123|max; $r123|min )
| .r = (($r123 | add) / 3 | sqrt) ;
 
findcircle( [22.83, 2.07]; [14.39, 30.24]; [33.65, 17.31])
| "Centre = \([.x, .y]), radius = \(.r)"
</syntaxhighlight>
{{output}}
<pre>
Centre = [18.978515660148815,16.26541079771587], radius = 14.708623978334177
</pre>
 
=={{header|Julia}}==
Line 430 ⟶ 513:
</syntaxhighlight>{{out}}
<pre>Centre = (18.978515660148815, 16.26541079771587), radius = 14.708623978334177</pre>
 
 
=={{header|Kotlin}}==
Line 527 ⟶ 609:
 
You may [https://ato.pxeger.com/run?1=jZO9TsMwFIUHtjzFHRhs4ZrGLrSAypOwhBCkih8JJxkqxJJHACkMCFQhEHPrdutU3oKtfQIeATv1T6UqKJ6ur3x8vntkv3yI6CofjT7z7LLV-9khaX4O8UDE1wkCdPb7-iiXs3cCupqqChPTXY3npqsq3y1K2y1KDBjuA1DrOE5us0SgaqPXzRB21am3VfEEfUDI-CwmsAfGaTHB8P0MyFwGLbBm6gwyCE5Q1V6g9FagOaygKL1A1xsO47kR6CEx7DtUvyxlPZahqscwFPW2wMhWSMvZV4OQpKORzUKSjk42C0k6Wvl_SNONkLax7Ny1GDbaWltglTFeRyWii0GeQv9UReQelT9fDYNckD5tNRlN70QWPARBGg3NqwfEGO1xwmi7q5912KH8iPA2ZR295ZweHpCwS3mIT9afxvwd-4f-AA Attempt This Online!]
=={{header|Swift}}==
<syntaxhighlight lang="swift">
import Foundation
import Matrix
 
extension Matrix where Element: SignedNumeric {
func minor(row: Int, column: Int) -> Element {
var submatrix = self
_ = submatrix.remove(rowAt: row - 1)
_ = submatrix.remove(columnAt: column - 1)
return submatrix.determinant as Element
}
}
 
enum MatrixErrors: Error {
case notEnoughPoints, tooManyPoints, pointsOnALine, miscError
}
 
func circleFrom3Points(points: (Double,Double)... ) throws -> (Double,Double,Double){
var pointArray: [[Double]] = [[0,0,0,0]]
for p in points {
pointArray.append([pow(p.0, 2) + pow(p.1, 2), p.0, p.1, 1])
}
guard pointArray.count > 3 else { throw MatrixErrors.notEnoughPoints }
guard pointArray.count < 5 else { throw MatrixErrors.tooManyPoints }
var matrix = Matrix(elements:pointArray)
var m11 = matrix.minor(row: 1, column: 1)
guard m11 != 0 else { throw MatrixErrors.pointsOnALine }
var m12 = matrix.minor(row: 1, column: 2)
var m13 = matrix.minor(row: 1, column: 3)
 
let x = 0.5 * m12 / m11
let y = -0.5 * m13 / m11
let r = (pow(x - pointArray[1][1],2) + pow(y - pointArray[1][2],2)).squareRoot()
return (x,y,r)
}
 
do {
let (x,y,r) = try circleFrom3Points(points: (22.83,2.07), (14.39,30.24), (33.65,17.31))
print("x:\(x), y:\(y), r: \(r)")
} catch {
debugPrint(error)
exit(1)
}
</syntaxhighlight>
<pre>
x:18.978515660148812, y:16.265410797715873, r: 14.708623978334185
</pre>
 
=={{header|Wren}}==
2,442

edits