Curve that touches three points: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|ooRexx}}: add version to compute the circle (and many other things))
m (syntax highlighting fixup automation)
Line 9:
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "H6:REALMATH.ACT"
 
TYPE Point=[INT x,y]
Line 130:
DO UNTIL CH#$FF OD
CH=$FF
RETURN</langsyntaxhighlight>
{{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.
<langsyntaxhighlight Adalang="ada">with Ada.Text_Io;
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;</langsyntaxhighlight>
{{out}}
<pre>
Line 205:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">QuadraticCurve(p1,p2,p3){ ; Y = aX^2 + bX + c
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]
}</langsyntaxhighlight>
Examples:<langsyntaxhighlight AutoHotkeylang="autohotkey">P1 := [10,10], P2 := [100,200], P3 := [200,10]
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</langsyntaxhighlight>
; 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.
<langsyntaxhighlight lang="go">package main
 
import "github.com/fogleman/gg"
Line 274:
dc.Stroke()
dc.SavePNG("quadratic_curve.png")
}</langsyntaxhighlight>
 
=={{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.
<langsyntaxhighlight lang="julia">using Makie
 
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)
</langsyntaxhighlight>{{out}}
<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">
<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>
</lang>
 
===two useful functions===
 
<syntaxhighlight lang="scheme">
<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>
</lang>
 
===computing the curve===
 
<syntaxhighlight lang="scheme">
<lang Scheme>
{def curve
{lambda {:pol :n}
Line 408:
{S.serie -1 {+ :n 1}} }}}
-> curve
</syntaxhighlight>
</lang>
 
===drawing a point===
 
<syntaxhighlight lang="scheme">
<lang Scheme>
{def dot
{lambda {:pt}
Line 419:
stroke="#0ff" fill="transparent" stroke-width="2"}}}}
-> dot
</syntaxhighlight>
</lang>
 
===defining points===
 
<syntaxhighlight lang="scheme">
<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>
</lang>
 
===drawing points and curves===
 
<syntaxhighlight lang="scheme">
<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>
</lang>
 
See the result in http://lambdaway.free.fr/lambdawalks/?view=bezier_3
Line 462:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
===Built-in===
<langsyntaxhighlight Mathematicalang="mathematica">pts = {{10, 10}, {100, 200}, {200, 10}};
cs = Circumsphere[pts]
Graphics[{PointSize[Large], Point[pts], cs}]</langsyntaxhighlight>
{{out}}
Outputs the circle:
Line 470:
and a graphical representation of the input points and the circle.
===Alternate implementation===
<langsyntaxhighlight Mathematicalang="mathematica">pts = {{10, 10}, {100, 200}, {200, 10}};
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}]</langsyntaxhighlight>
{{out}}
Outputs the circle:
Line 490:
{{trans|Go}}
{{libheader|imageman}}
<langsyntaxhighlight Nimlang="nim">import 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)</langsyntaxhighlight>
 
=={{header|ooRexx}}==
===Version 1===
<langsyntaxhighlight lang="oorexx">/* REXX ***************************************************************
* Compute the polynome satisfying three given Points
**********************************************************************/
Line 613:
End
Select
When j=m Then Call ex 'WidersprüchlichesWidersprü�chliches Gleichungssystem'
When j>m Then Call ex 'Gleichungen sind linear abhängig'
Otherwise Nop
Line 647:
Exit
 
dbg: Return</langsyntaxhighlight>
{{out}}
<pre>y=-0.021111111111*x**2+4.43333333333*x-32.2222222222
Line 655:
200 / 10.0000008 / 10</pre>
===Version 2 using fraction arithmetic===
<langsyntaxhighlight lang="oorexx">/* REXX ***************************************************************
* Compute the polynome satisfying three given Points
**********************************************************************/
Line 758:
End
Select
When j=m Then Call ex 'WidersprüchlichesWidersprü�chliches Gleichungssystem'
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</langsyntaxhighlight>
{{out}}
<pre>y=-(19/900)*x**2+(133/30)*x-(290/9)
Line 930:
</pre>
===Version 3 computing the circumcircle (among many other things)===
<langsyntaxhighlight lang="oorexx">/* REXX ****************************************************************
* Triangle computes data about a given triangle
* The circumcircle is what we need here
Line 1,294:
Exit
 
::requires rxMath library</langsyntaxhighlight>
{{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.
<langsyntaxhighlight lang="perl">use SVG;
use List::Util qw(max min);
 
Line 1,384:
open $fh, '>', 'curve-3-points.svg';
print $fh $svg->xmlify(-namespace=>'svg');
close $fh;</langsyntaxhighlight>
[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].
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
<!--</langsyntaxhighlight>-->
 
=={{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" perl6line>use Image::PNG::Portable;
 
# the points of interest
Line 1,570:
$png.set($x, $y, |@rgb) if ( $X - $x + ($Y - $y) × i ).abs <= $radius;
}
}</langsyntaxhighlight>
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}}
<langsyntaxhighlight lang="ecmascript">import "graphics" for Canvas, Color, Point
import "dome" for Window
 
Line 1,621:
}
}
}</langsyntaxhighlight>
 
=={{header|zkl}}==
Line 1,627:
Uses Image Magick and
the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl
<langsyntaxhighlight lang="zkl">const X=0, Y=1; // p.X == p[X]
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");
}();</langsyntaxhighlight>
{{out}}
Image at [http://www.zenkinetic.com/Images/RosettaCode/quadraticCurve.zkl.jpg quadratic curve]
10,333

edits