Curve that touches three points: Difference between revisions
Content deleted Content added
Added Algol 68 |
|||
(8 intermediate revisions by 7 users not shown) | |||
Line 202:
Center : (105.0, 81.3)
Radius : 118.8
</pre>
=={{header|ALGOL 68}}==
{{Trans|Ada}}
As with the Ada and FreeBASIC translation of it, finds the centre and radius of a circle through the points.
<syntaxhighlight lang="algol68">
BEGIN # draw a curve that passes through 3 points - translated from the Ada #
# sample - finds the centre and radius of a circle through the points #
REAL x1 = 10; # Point P1 #
REAL y1 = 10;
REAL x2 = 100 # Point P2 #;
REAL y2 = 200;
REAL x3 = 200; # Point P3 #
REAL y3 = 10;
REAL x4 = ( x1 + x2 ) / 2; # Point P4 - midpoint between P1 and P2 #
REAL y4 = ( y1 + y2 ) / 2;
REAL s4 = ( y2 - y1 ) / ( x2 - x1 ); # Slope P1-P2 #
REAL a4 = -1 / s4; # Slope P4-Centre #
# Y4 = A4 * X4 + B4 <=> B4 = Y4 - A4 * X4 #
REAL b4 = y4 - a4 * x4;
REAL x5 = ( x2 + x3 ) / 2; # Point P5 - midpoint between P2 and P3 #
REAL y5 = ( y2 + y3 ) / 2;
REAL s5 = ( y3 - y2 ) / ( x3 - x2 ); # Slope P2-P3 #
REAL a5 = -1.0 / s5; # Slope P5-Centre #
# Y5 = A5 * X5 + B5 <=> B5 = Y5 - A5 * X5 #
REAL b5 = y5 - a5 * x5;
# Find centre #
# Y = A4 * X + B4 -- Line 1 #
# Y = A5 * X + B5 -- Line 2 #
# Solve for X: #
# A4 * X + B4 = A5 * X + B5 #
# A4 * X - A5 * X = B5 - B4 #
# X * (A4 - A5) = B5 - B4 #
# X = (B5 - B4) / (A4 - A5) #
REAL xc = ( b5 - b4 ) / ( a4 - a5 );
REAL yc = a4 * xc + b4;
# Radius #
REAL r = sqrt( ( x1 - xc ) ^ 2 + ( y1 - yc ) ^ 2 );
print( ( "Centre : (", fixed( xc, -6, 1 ), ", ", fixed( yc, -6, 1 ), " )", newline ) );
print( ( "Radius : ", fixed( r, -6, 1 ), newline ) )
END
</syntaxhighlight>
{{out}}
<pre>
Centre : ( 105.0, 81.3 )
Radius : 118.8
</pre>
Line 226 ⟶ 280:
[100, 200.000000]
[200, 10.000000]</pre>
=={{header|F_Sharp|F#}}==
This task uses [[Lagrange_Interpolation#F#]]
<syntaxhighlight lang="fsharp">
// Curve that touches three points. Nigel Galloway: September 13th., 2023
open Plotly.NET
let points=let a=LIF([10;100;200],[10;200;10]).Expression in [10.0..200.0]|>List.map(fun n->(n,(Evaluate.evaluate (Map.ofList ["x",n]) a).RealValue))
Chart.Point(points)|>Chart.show
</syntaxhighlight>
{{out}}
[[File:C3p.png]]
=={{header|FreeBASIC}}==
{{trans|Ada}}
<syntaxhighlight lang="vb">' Point P1
Dim As Double X1 = 10.0
Dim As Double Y1 = 10.0
' Point P2
Dim As Double X2 = 100.0
Dim As Double Y2 = 200.0
' Point P3
Dim As Double X3 = 200.0
Dim As Double Y3 = 10.0
' Point P4 - midpoint between P1 and P2
Dim As Double X4 = (X1 + X2) / 2.0
Dim As Double Y4 = (Y1 + Y2) / 2.0
Dim As Double S4 = (Y2 - Y1) / (X2 - X1) ' Slope P1-P2
Dim As Double A4 = -1.0 / S4 ' Slope P4-Center
' Y4 = A4 * X4 + B4 <=> B4 = Y4 - A4 * X4
Dim As Double B4 = Y4 - A4 * X4
' Point P5 - midpoint between P2 and P3
Dim As Double X5 = (X2 + X3) / 2.0
Dim As Double Y5 = (Y2 + Y3) / 2.0
Dim As Double S5 = (Y3 - Y2) / (X3 - X2) ' Slope P2-P3
Dim As Double A5 = -1.0 / S5 ' Slope P5-Center
' Y5 = A5 * X5 + B5 <=> B5 = Y5 - A5 * X5
Dim As Double B5 = Y5 - A5 * X5
' Find center
' Y = A4 * X + B4 ' Line 1
' Y = A5 * X + B5 ' Line 2
' Solve for X:
' A4 * X + B4 = A5 * X + B5
' A4 * X - A5 * X = B5 - B4
' X * (A4 - A5) = B5 - B4
' X = (B5 - B4) / (A4 - A5)
Dim As Double Xc = (B5 - B4) / (A4 - A5)
Dim As Double Yc = A4 * Xc + B4
' Radius
Dim As Double R = Sqr((X1 - Xc) ^ 2 + (Y1 - Yc) ^ 2)
Print Using "Center : (###.#, ###.#)"; Xc; Yc
Print Using "Radius : ###.#"; R
Sleep</syntaxhighlight>
=={{header|Go}}==
Line 1,387 ⟶ 1,500:
print $fh $svg->xmlify(-namespace=>'svg');
close $fh;</syntaxhighlight>
=={{header|Phix}}==
Line 1,475 ⟶ 1,587:
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
=={{header|PHP}}==
Finds a circle that passes through those three points. Adapted from AutoHotkey.
<syntaxhighlight lang="PHP">
function circle_3_points($x1, $y1, $x2, $y2, $x3, $y3) {
$x4 = ($x1 + $x2) / 2.0;
$y4 = ($y1 + $y2) / 2.0;
$s4 = ($y2 - $y1) / ($x2 - $x1);
$a4 = -1.0 / $s4;
$b4 = $y4 - $a4 * $x4;
$x5 = ($x2 + $x3) / 2.0;
$y5 = ($y2 + $y3) / 2.0;
$s5 = ($y3 - $y2) / ($x3 - $x2);
$a5 = -1.0 / $s5;
$b5 = $y5 - $a5 * $x5;
$xc = ($b5 - $b4) / ($a4 - $a5);
$yc = $a4 * $xc + $b4;
$r = √(²($x1 - $xc) + ²($y1 - $yc));
return [$xc, $yc, $r];
}
</syntaxhighlight>
=={{header|Raku}}==
Line 1,574 ⟶ 1,708:
}</syntaxhighlight>
See [https://github.com/thundergnat/rc/blob/master/img/Curve-3-points-perl6.png Curve-3-points-perl6.png] (offsite .png image)
=={{header|RPL}}==
HP-49+ RPL has a built-in function named <code>LAGRANGE</code> that returns the curve equation from the 3 points, but let's consider it as belonging to a library.
{{works with|HP|48G}}
[[File:Parabol.png|thumb|right|alt=HP-48G emulator screenshot|HP-48G emulator screenshot]]
« → a b c
« { 0 0 0 }
c a - C→R SWAP / c b - RE /
b a - C→R SWAP / c b - RE / -
1 SWAP PUT
b a - C→R SWAP / OVER 1 GET b a + RE * -
2 SWAP PUT
a IM OVER 1 GET a RE SQ * - OVER 2 GET a RE * -
3 SWAP PUT
{ 'X^2' 'X' 1 } * ∑LIST
» » '<span style="color:blue">PAREQ</span>' STO
« # 131d # 64d PDIM 0 210 DUP2 XRNG YRNG
FUNCTION
(10,10) (100,200) (200,10)
3 DUPN <span style="color:blue">PAREQ</span> STEQ
ERASE DRAW
1 3 '''START''' PIXOFF '''NEXT''' <span style="color:grey">''@ switch off the 3 points on the curve''</span>
{ } PVIEW
{ PPAR EQ } PURGE
» '<span style="color:blue">TASK</span>' STO
=={{header|Wren}}==
{{trans|Go}}
{{libheader|DOME}}
<syntaxhighlight lang="
import "dome" for Window
Line 1,624 ⟶ 1,784:
}
}</syntaxhighlight>
=={{header|XPL0}}==
[[File:XPL0_Curve.gif|200px|thumb|right]]
<syntaxhighlight lang "XPL0">def X0=10., Y0=10., X1=100., Y1=200., X2=200., Y2=10.;
func real Lagrange(X);
real X;
return (X-X1) * (X-X2) / (X0-X1) / (X0-X2) * Y0 +
(X-X0) * (X-X2) / (X1-X0) / (X1-X2) * Y1 +
(X-X0) * (X-X1) / (X2-X0) / (X2-X1) * Y2;
def Offset = 205;
real X;
[SetVid($13); \VGA 320x200x8 graphics
X:= X0;
Move(fix(X), Offset-fix(Y0));
repeat X:= X + 1.;
Line(fix(X), Offset-fix(Lagrange(X)), 3\cyan\);
until X >= X2;
Point(fix(X0), Offset-fix(Y0), 14\yellow\);
Point(fix(X1), Offset-fix(Y1), 14);
Point(fix(X2), Offset-fix(Y2), 14);
]</syntaxhighlight>
=={{header|zkl}}==
|