Curve that touches three points: Difference between revisions
m
syntax highlighting fixup automation
Walterpachl (talk | contribs) m (→{{header|ooRexx}}: add version to compute the circle (and many other things)) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 9:
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
<
TYPE Point=[INT x,y]
Line 130:
DO UNTIL CH#$FF OD
CH=$FF
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Curve_that_touches_three_points.png Screenshot from Atari 8-bit computer]
Line 136:
=={{header|Ada}}==
Find center and radius of circle that touches the 3 points. Solve with simple linear algebra. In this case no division by zero.
<
with Ada.Numerics.Generic_Elementary_Functions;
Line 197:
Put ("Center : "); Put ("("); Put (Xc); Put (", "); Put (Yc); Put (")"); New_Line;
Put ("Radius : "); Put (R); New_Line;
end Three_Point_Circle;</
{{out}}
<pre>
Line 205:
=={{header|AutoHotkey}}==
<
x1:=p1.1, y1:=p1.2, x2:=p2.1, y2:=p2.2, x3:=p3.1, y3:=p3.2
m:=x1-x2, n:=x3-x2, m:= ((m*n)<0?-1:1) * m
Line 212:
c:=y1 - a*x1**2 - b*x1
return [a,b,c]
}</
Examples:<
v := QuadraticCurve(p1,p2,p3)
a := v.1, b:= v.2, c:= v.3
Line 220:
res .= "[" x ", " y "]`n"
}
MsgBox % "Y = " a " X^2 " (b>0?"+":"") b " X " (c>0?"+":"") c " `n" res</
; for plotting, use code from [https://rosettacode.org/wiki/Plot_coordinate_pairs#AutoHotkey RosettaCode: Plot Coordinate Pairs]
Outputs:<pre>Y = -0.021111 X^2 +4.433333 X -32.222222
Line 235:
The resulting 'curve' is then saved to a .png file where it can be viewed with a utility such as EOG.
<
import "github.com/fogleman/gg"
Line 274:
dc.Stroke()
dc.SavePNG("quadratic_curve.png")
}</
=={{header|J}}==
Line 328:
=={{header|Julia}}==
To make things more specific, find the circle determined by the points. The curve is then the arc between the 3 points.
<
struct Point; x::Float64; y::Float64; end
Line 356:
lines!(scene, x, b .- y0, color = :red)
scatter!(scene, [p.x for p in allp], [p.y for p in allp], markersize = r / 10)
</
<pre>
The circle with center at x = 105.0, y = 81.31578947368422 and radius 118.78948534384199.
Line 366:
===bezier interpolation of 3 points===
<syntaxhighlight lang="scheme">
p(t) = 1*p0(1-t)2 + 2*p1(1-t)t + 1*p2t2
Line 378:
{* 1 {A.get 1 :p2} :t :t}} }}
-> interpol
</syntaxhighlight>
===two useful functions===
<syntaxhighlight lang="scheme">
{def middle
{lambda {:p1 :p2} // compute the middle point of p1 and p2
Line 396:
{- {* 2 {A.get 1 :p2}} {A.get 1 :p1} } }}}
-> symetric
</syntaxhighlight>
===computing the curve===
<syntaxhighlight lang="scheme">
{def curve
{lambda {:pol :n}
Line 408:
{S.serie -1 {+ :n 1}} }}}
-> curve
</syntaxhighlight>
===drawing a point===
<syntaxhighlight lang="scheme">
{def dot
{lambda {:pt}
Line 419:
stroke="#0ff" fill="transparent" stroke-width="2"}}}}
-> dot
</syntaxhighlight>
===defining points===
<syntaxhighlight lang="scheme">
{def P0 {A.new 150 180}} -> P0
{def P1 {A.new 300 250}} -> P1
Line 436:
{def P21 {middle {P2} {P1}}} -> P21
{def P12 {symetric {P21} {P0}}} -> P12
</syntaxhighlight>
===drawing points and curves===
<syntaxhighlight lang="scheme">
{svg {@ width="500" height="500" style="background:#444;"}
{polyline {@ points="{curve {A.new {P0} {P20} {P2}} 20}"
Line 455:
{dot {P21}} {dot {P12}}
}
</syntaxhighlight>
See the result in http://lambdaway.free.fr/lambdawalks/?view=bezier_3
Line 462:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
===Built-in===
<
cs = Circumsphere[pts]
Graphics[{PointSize[Large], Point[pts], cs}]</
{{out}}
Outputs the circle:
Line 470:
and a graphical representation of the input points and the circle.
===Alternate implementation===
<
createCircle[{{x1_, y1_}, {x2_, y2_}, {x3_, y3_}}] :=
With[{a = Det[({{x1, y1, 1}, {x2, y2, 1}, {x3, y3, 1}})],
Line 481:
Circle[{-(d/(2 a)), -(e/(2 a))}, Sqrt[(d^2 + e^2)/(4 a^2) - f/a]]]
cs = createCircle[pts]
Graphics[{PointSize[Large], Point[pts], cs}]</
{{out}}
Outputs the circle:
Line 490:
{{trans|Go}}
{{libheader|imageman}}
<
type
Line 520:
let color = ColorRGBF([float32 0, 0, 0]) # Black.
img.drawPolyline(closed = false, color, P.points(N))
img.savePNG("curve.png", compression = 9)</
=={{header|ooRexx}}==
===Version 1===
<
* Compute the polynome satisfying three given Points
**********************************************************************/
Line 613:
End
Select
When j=m Then Call ex '
When j>m Then Call ex 'Gleichungen sind linear abhängig'
Otherwise Nop
Line 647:
Exit
dbg: Return</
{{out}}
<pre>y=-0.021111111111*x**2+4.43333333333*x-32.2222222222
Line 655:
200 / 10.0000008 / 10</pre>
===Version 2 using fraction arithmetic===
<
* Compute the polynome satisfying three given Points
**********************************************************************/
Line 758:
End
Select
When j=m Then Call ex '
When j>m Then Call ex 'Gleichungen sind linear abhängig'
Otherwise Nop
Line 922:
s=left(s,p-1)||new||substr(s,p+length(old))
End
Return s</
{{out}}
<pre>y=-(19/900)*x**2+(133/30)*x-(290/9)
Line 930:
</pre>
===Version 3 computing the circumcircle (among many other things)===
<
* Triangle computes data about a given triangle
* The circumcircle is what we need here
Line 1,294:
Exit
::requires rxMath library</
{{out}}
<pre>Triangle ABC:
Line 1,341:
=={{header|Perl}}==
Hilbert '''curve''' task code repeated here, with the addition that the 3 task-required points are marked. Mostly satisfies the letter-of-the-law of task specification while (all in good fun) subverting the spirit of the thing.
<
use List::Util qw(max min);
Line 1,384:
open $fh, '>', 'curve-3-points.svg';
print $fh $svg->xmlify(-namespace=>'svg');
close $fh;</
[https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/curve-3-points.svg Hilbert curve passing through 3 defined points] (offsite image)
Line 1,392:
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/Quadratic.htm here].
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 1,472:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</
=={{header|Raku}}==
Line 1,481:
Saved as a png for wide viewing support. Note that png coordinate systems have 0,0 in the upper left corner.
<syntaxhighlight lang="raku"
# the points of interest
Line 1,570:
$png.set($x, $y, |@rgb) if ( $X - $x + ($Y - $y) × i ).abs <= $radius;
}
}</
See [https://github.com/thundergnat/rc/blob/master/img/Curve-3-points-perl6.png Curve-3-points-perl6.png] (offsite .png image)
Line 1,576:
{{trans|Go}}
{{libheader|DOME}}
<
import "dome" for Window
Line 1,621:
}
}
}</
=={{header|zkl}}==
Line 1,627:
Uses Image Magick and
the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl
<
var p=L(L(10.0, 10.0), L(100.0, 200.0), L(200.0, 10.0)); // (x,y)
Line 1,661:
}
img.writeJPGFile("quadraticCurve.zkl.jpg");
}();</
{{out}}
Image at [http://www.zenkinetic.com/Images/RosettaCode/quadraticCurve.zkl.jpg quadratic curve]
|