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

(Added Easylang)
Line 439:
Check radius as the distance between the centre and the first point:
14.70862397833418</pre>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
 
'''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 s($a;$b): $a*$a + $b*$b;
def t($a;$b): $a*$a - $b*$b;
 
$p1 as [$x1, $y1]
| $p2 as [$x2, $y2]
| $p3 as [$x3, $y3]
 
| ($x1 - $x2) as $x12
| ($x1 - $x3) as $x13
| ($y1 - $y2) as $y12
| ($y1 - $y3) as $y13
| ($y3 - $y1) as $y31
| ($y2 - $y1) as $y21
| ($x3 - $x1) as $x31
| ($x2 - $x1) as $x21
 
| t($x1; $x3) as $tx13
| t($y1; $y3) as $ty13
| t($x2; $x1) as $tx21
| t($y2; $y1) as $ty21
 
| { y: ($tx13 * $x12 + $ty13 * $x12 + $tx21 * $x13 + $ty21 * $x13),
x: ($tx13 * $y12 + $ty13 * $y12 + $tx21 * $y13 + $ty21 * $y13) }
| .y = .y / ($y21 * $x13 - $y31 * $x12) / 2
| .x = .x / ($x21 * $y13 - $x31 * $y12) / 2
| (2 * (.x * $x1 + .y * $y1) - s($x1; $y1)) as $c
| .r = (( s(.x; .y) - $c) | sqrt) ;
 
def demo($p1; $p2; $p3):
def sq: .*.;
def d($a; $b): (($a[0] - $b[0])|sq) + (($a[1] - $b[1])|sq) | sqrt;
findCircle($p1; $p2; $p3)
| "Centre is at [\(.x), \(.y)]",
"Radius is \(.r)",
 
"\nCheck radius as the distance between the centre and the first point: "
+ "\( d($p1; [.x,.y]) )"
;
 
demo([22.83, 2.07]; [14.39, 30.24]; [33.65, 17.31])
</syntaxhighlight>
{{output}}
<pre>
Centre is at [18.978515660148815, 16.265410797715866]
Radius is 14.708623978334177
 
Check radius as the distance between the centre and the first point: 14.708623978334177
</pre>
 
=={{header|Julia}}==
Line 468 ⟶ 529:
</syntaxhighlight>{{out}}
<pre>Centre = (18.978515660148815, 16.26541079771587), radius = 14.708623978334177</pre>
 
 
=={{header|Kotlin}}==
2,484

edits