Centre and radius of a circle passing through 3 points in a plane: Difference between revisions
Content added Content deleted
(Added C++) |
(Added Dart) |
||
Line 42: | Line 42: | ||
return 0; |
return 0; |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
=={{header|Dart}}== |
|||
{{trans|C++}} |
|||
<syntaxhighlight lang="dart">import 'dart:math'; |
|||
void findCircle( |
|||
double x1, double y1, double x2, double y2, double x3, double y3) { |
|||
double x12 = x1 - x2; |
|||
double x13 = x1 - x3; |
|||
double y12 = y1 - y2; |
|||
double y13 = y1 - y3; |
|||
double y31 = y3 - y1; |
|||
double y21 = y2 - y1; |
|||
double x31 = x3 - x1; |
|||
double x21 = x2 - x1; |
|||
double sx13 = x1 * x1 - x3 * x3; |
|||
double sy13 = y1 * y1 - y3 * y3; |
|||
double sx21 = x2 * x2 - x1 * x1; |
|||
double sy21 = y2 * y2 - y1 * y1; |
|||
double f = (sx13 * x12 + sy13 * x12 + sx21 * x13 + sy21 * x13) / |
|||
(2 * (y31 * x12 - y21 * x13)); |
|||
double g = (sx13 * y12 + sy13 * y12 + sx21 * y13 + sy21 * y13) / |
|||
(2 * (x31 * y12 - x21 * y13)); |
|||
double c = -pow(x1, 2) - pow(y1, 2) - 2 * g * x1 - 2 * f * y1; |
|||
double h = -g; |
|||
double k = -f; |
|||
double r = sqrt(h * h + k * k - c); |
|||
print("Centre is at ($h, $k)"); |
|||
print("\nCheck radius as the distance between the centre and the first point: $r"); |
|||
} |
|||
void main() { |
|||
findCircle(22.83, 2.07, 14.39, 30.24, 33.65, 17.31); |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Centre is at (18.978515660148815, 16.265410797715866) |
|||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
Revision as of 00:06, 21 February 2024
Centre and radius of a circle passing through 3 points in a plane is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
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)
C++
This follows the lines of the C++ code here.
#include <iostream>
#include <math.h>
using namespace std;
void findCircle(float x1, float y1, float x2, float y2, float x3, float y3) {
float x12 = x1 - x2;
float x13 = x1 - x3;
float y12 = y1 - y2;
float y13 = y1 - y3;
float y31 = y3 - y1;
float y21 = y2 - y1;
float x31 = x3 - x1;
float x21 = x2 - x1;
float sx13 = pow(x1, 2) - pow(x3, 2);
float sy13 = pow(y1, 2) - pow(y3, 2);
float sx21 = pow(x2, 2) - pow(x1, 2);
float sy21 = pow(y2, 2) - pow(y1, 2);
float f = ((sx13) * (x12) + (sy13) * (x12) + (sx21) * (x13) + (sy21) * (x13))
/ (2 * ((y31) * (x12) - (y21) * (x13)));
float g = ((sx13) * (y12) + (sy13) * (y12) + (sx21) * (y13) + (sy21) * (y13))
/ (2 * ((x31) * (y12) - (x21) * (y13)));
float c = -pow(x1, 2) - pow(y1, 2) - 2 * g * x1 - 2 * f * y1;
float h = -g;
float k = -f;
float r = sqrt(h * h + k * k - c);
cout << "Centre is at (" << h << ", " << k << ")" << endl;
cout << "Radius is " << r;
}
int main() {
findCircle(22.83, 2.07, 14.39, 30.24, 33.65, 17.31);
return 0;
}
Dart
import 'dart:math';
void findCircle(
double x1, double y1, double x2, double y2, double x3, double y3) {
double x12 = x1 - x2;
double x13 = x1 - x3;
double y12 = y1 - y2;
double y13 = y1 - y3;
double y31 = y3 - y1;
double y21 = y2 - y1;
double x31 = x3 - x1;
double x21 = x2 - x1;
double sx13 = x1 * x1 - x3 * x3;
double sy13 = y1 * y1 - y3 * y3;
double sx21 = x2 * x2 - x1 * x1;
double sy21 = y2 * y2 - y1 * y1;
double f = (sx13 * x12 + sy13 * x12 + sx21 * x13 + sy21 * x13) /
(2 * (y31 * x12 - y21 * x13));
double g = (sx13 * y12 + sy13 * y12 + sx21 * y13 + sy21 * y13) /
(2 * (x31 * y12 - x21 * y13));
double c = -pow(x1, 2) - pow(y1, 2) - 2 * g * x1 - 2 * f * y1;
double h = -g;
double k = -f;
double r = sqrt(h * h + k * k - c);
print("Centre is at ($h, $k)");
print("\nCheck radius as the distance between the centre and the first point: $r");
}
void main() {
findCircle(22.83, 2.07, 14.39, 30.24, 33.65, 17.31);
}
- Output:
Centre is at (18.978515660148815, 16.265410797715866) =={{header|F_Sharp|F#}}== <syntaxhighlight lang="fsharp"> // Centre and radius of a circle passing through 3 points in a plane. Nigel Galloway: February 20th., 2024 let c (a,b) (c,d) (e,f)=(0.5*((a*a+b*b)*(f-d)+(c*c+d*d)*(b-f)+(e*e+f*f)*(d-b))/(a*(f-d)+c*(b-f)+e*(d-b)), 0.5*((a*a+b*b)*(e-c)+(c*c+d*d)*(a-e)+(e*e+f*f)*(c-a))/(b*(e-c)+d*(a-e)+f*(c-a))) let d n g = let n,g=fst n-fst g,snd n-snd g in sqrt(n*n+g*g)/2.0 let circ P1 P2 P3 = let c=c P1 P2 P3 in (c,d c P1) let centre,radius=circ (22.83, 2.07) (14.39, 30.24) (33.65, 17.31) printfn $"Centre = %A{centre}, radius = %f{radius}" </syntaxhighlight> {{out}} <pre> Centre = (18.97851566, 16.2654108), radius = 7.354312
FreeBASIC
Sub findCircle(x1 As Double, y1 As Double, x2 As Double, y2 As Double, x3 As Double, y3 As Double)
Dim x12 As Double = x1 - x2
Dim x13 As Double = x1 - x3
Dim y12 As Double = y1 - y2
Dim y13 As Double = y1 - y3
Dim y31 As Double = y3 - y1
Dim y21 As Double = y2 - y1
Dim x31 As Double = x3 - x1
Dim x21 As Double = x2 - x1
Dim sx13 As Double = x1 * x1 - x3 * x3
Dim sy13 As Double = y1 * y1 - y3 * y3
Dim sx21 As Double = x2 * x2 - x1 * x1
Dim sy21 As Double = y2 * y2 - y1 * y1
Dim f As Double = (sx13 * x12 + sy13 * x12 + sx21 * x13 + sy21 * x13) / (y31 * x12 - y21 * x13) / 2
Dim g As Double = (sx13 * y12 + sy13 * y12 + sx21 * y13 + sy21 * y13) / (x31 * y12 - x21 * y13) / 2
Dim c As Double = -x1 * x1 - y1 * y1 - 2 * g * x1 - 2 * f * y1
Dim h As Double = -g
Dim k As Double = -f
Dim r As Double = 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
findCircle(22.83, 2.07, 14.39, 30.24, 33.65, 17.31)
Sleep
- Output:
Centre is at (18.97851566014882, 16.26541079771587) Radius is 14.70862397833418 Check radius as the distance between the centre and the first point: 14.70862397833418
Phix
with javascript_semantics
function circle(sequence p1, p2, p3)
atom {a,b} = p1, {c,d} = p2, {e,f} = p3,
a2b2 = a*a+b*b, ae = a-e, db = d-b,
c2d2 = c*c+d*d, bf = b-f, ec = e-c,
e2f2 = e*e+f*f, ca = c-a, fd = f-d,
cx = 0.5*(a2b2*fd+c2d2*bf+e2f2*db)/(a*fd+c*bf+e*db),
cy = 0.5*(a2b2*ec+c2d2*ae+e2f2*ca)/(b*ec+d*ae+f*ca),
r = sqrt(power(cx-e,2)+power(cy-f,2))
return {{cx,cy},r}
end function
atom {{cx,cy},r} = circle({22.83,2.07},{14.39,30.24},{33.65,17.31})
printf(1,"Centre = %.8f, %.7f, radius = %.6f\n",{cx,cy,r})
- Output:
Centre = 18.97851566, 16.2654108, radius = 14.708624
Wren
This follows the lines of the C++ code here.
var findCircle = Fn.new { |x1, y1, x2, y2, x3, y3|
var x12 = x1 - x2
var x13 = x1 - x3
var y12 = y1 - y2
var y13 = y1 - y3
var y31 = y3 - y1
var y21 = y2 - y1
var x31 = x3 - x1
var x21 = x2 - x1
var sx13 = x1 * x1 - x3 * x3
var sy13 = y1 * y1 - y3 * y3
var sx21 = x2 * x2 - x1 * x1
var sy21 = y2 * y2 - y1 * y1
var f = sx13 * x12 + sy13 * x12 + sx21 * x13 + sy21 * x13
f = f / (y31 * x12 - y21 * x13) / 2
var g = sx13 * y12 + sy13 * y12 + sx21 * y13 + sy21 * y13
g = g / (x31 * y12 - x21 * y13) / 2
var c = -x1 * x1 - y1 * y1 - 2 * g * x1 - 2 * f * y1
var h = -g
var k = -f
var r = (h * h + k * k - c).sqrt
return [h, k, r]
}
var hkr = findCircle.call(22.83, 2.07, 14.39, 30.24, 33.65, 17.31)
System.print("Centre is at %([hkr[0], hkr[1]])")
System.print("Radius is %(hkr[2])")
System.print("\nCheck radius as the distance between the centre and the first point:")
System.print(((22.83 - hkr[0]).pow(2) + (2.07 - hkr[1]).pow(2)).sqrt)
- Output:
Centre is at [18.978515660149, 16.265410797716] Radius is 14.708623978334 Check radius as the distance between the centre and the first point: 14.708623978334