B-spline: Difference between revisions

135 bytes added ,  3 months ago
→‎{{header|ALGOL 68}}: Use spaces instead of tabs, tweak
m (syntax highlighting fixup automation)
(→‎{{header|ALGOL 68}}: Use spaces instead of tabs, tweak)
 
(2 intermediate revisions by 2 users not shown)
Line 25:
* [https://www.cl.cam.ac.uk/teaching/2000/AGraphHCI/SMEG/node4.html B-splines]
<br><br>
 
 
=={{header|ALGOL 68}}==
{{Trans|Lua}}Which is {{Trans|Wren}}Suppresses unused parts of the plot.
<syntaxhighlight lang="algol68">BEGIN # construct a B-Spline #
BEGIN # construct a B-Spline #
 
# mode to hold a B Spline #
Line 41 ⟶ 40:
[ lb : ub ]INT range;
FOR n FROM lb TO ub DO range[ n ] := n OD;
range
END # uniform knot vector # ;
 
PROC calculate bspline = ( REF BSPLINE bs, INT i, k, x )REAL:
IF k = 1
THEN ABS ( ( t OF bs )[ i ] <= x AND x < ( t OF bs )[ i + 1 ] )
ELSE
PROC helper = ( REF BSPLINE bs, INT i, k, x )REAL:
IF ( t OF bs )[ i + k ] /= ( t OF bs )[ i ]
Line 67 ⟶ 66:
REAL sum x := 0;
REAL sum y := 0;
FOR i TO n OF bs DO
REAL f = calculate bspline( bs, i, k OF bs, x );
sum x +:= f * ( control points OF bs )[ i, 1 ];
sum y +:= f * ( control points OF bs )[ i, 2 ]
OD;
points[ x, 1 ] := round( sum x );
points[ x, 2 ] := round( sum y )
OD;
Line 82 ⟶ 81:
INT x := x0;
INT y := y0;
INT dx := ABS ( x2 - x );
INT dy := ABS ( y2 - y );
INT n = 1 + dx + dy;
INT dir x = IF x2 > x THEN 1 ELSE -1 FI;
INT dir y = IF y2 > y THEN 1 ELSE -1 FI;
INT err := dx - dy;
dx *:= 2;
dy *:= 2;
Line 100 ⟶ 99:
 
PROC plot line = ( REF[,]BOOL plot, INT x1, y1, x2, y2 )VOID:
raytrace( x1, y1, x2, y2, plot
, ( REF[,]BOOL plot, INT x, INT y )VOID: IF x >= 0
AND y >= 0
Line 106 ⟶ 105:
AND y < 2 UPB plot
THEN plot[ x + 1, y + 1 ] := TRUE
FI
);
 
PROC plot bspline = ( REF BSPLINE bs, REF[,]BOOL plot, REAL scale x, scale y )VOID:
IF k OF bs > n OF bs OR k OF bs < 1 THEN
print( ( "k (= ", whole( k OF bs, 0 ), ") can't be more than ", whole( n OF bs, 0 ), " or less than 1." ) );
print( ( " or less than 1.", newline ) );
stop
ELSE
Line 124:
)
OD
FI # plot bspline # ;
 
# print the plot - outputs @ or blank depending on whether the point is plotted or not #
Line 165:
);
INT k = 4; # Polynomial degree is one less than this i.e. cubic. #
BSPLINE bs := ( control points
:= BSPLINE( control points
, UPB control points
, k
Line 178 ⟶ 177:
plot bspline( bs, plot, scale x, scale y );
print plot( plot )
END
END</syntaxhighlight>
{{out}}
leading blank lines removed...
<pre>
 
@@@@
@@@@
Line 225 ⟶ 226:
=={{header|Julia}}==
Choose BSpline D of 2, ie degree 1.
<syntaxhighlight lang="julia">using Graphics, Plots
 
Point(t::Tuple) = Vec2(Float64(t[1]), Float64(t[2]))
Line 232 ⟶ 233:
plt = plot(map(a -> a.x, controlpoints), map(a -> a.y, controlpoints))
savefig(plt, "BSplineplot.png")</syntaxhighlight>
 
=={{header|Lua}}==
 
{{trans|Wren}}
 
<syntaxhighlight lang=Lua"lua">local function Range(from, to)
local range = {}
for n = from, to do table.insert(range, n) end
Line 406:
@@@
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica"mathematica">Graphics[
BSplineCurve[{{171, 171}, {185, 111}, {202, 109}, {202, 189}, {328,
160}, {208, 254}, {241, 330}, {164, 252}, {69, 278}, {139,
Line 415 ⟶ 414:
{{out}}
Outputs a graphical representation of a B-spline.
 
=={{header|Perl}}==
{{trans|Raku}}
<syntaxhighlight lang="perl">use strict;
use warnings;
use Class::Struct;
Line 475 ⟶ 473:
$surf->write_to_png($OUTPUT);</syntaxhighlight>
Output: [https://raw.githubusercontent.com/SqrtNegInf/Rosettacode-Perl-Smoke/master/ref/b-spline.png b-spline.png] (offsite image)
 
=={{header|Phix}}==
{{trans|Wren}}
Line 481 ⟶ 478:
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/bspline.htm here].
<!--<syntaxhighlight lang=Phix"phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\B-spline.exw
Line 569 ⟶ 566:
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
 
=={{header|Processing}}==
<syntaxhighlight lang="java">
//Aamrun, 26th June 2022
 
Line 599 ⟶ 595:
 
</syntaxhighlight>
 
=={{header|Raku}}==
A minimal translation of [https://www.cypherpunk.at/download/bspline/bspline_1.1.tbz2 this C program], by [https://github.com/rahra Bernhard R. Fischer].
<syntaxhighlight lang=perl6"raku" line># 20211112 Raku programming solution
 
use Cairo;
Line 676 ⟶ 671:
 
Output: [https://drive.google.com/file/d/1dInRJeecA18meybDF2D0usEaWMGFqoBj/view (Offsite image file) ]
 
=={{header|Wren}}==
{{libheader|DOME}}
Line 682 ⟶ 676:
 
If one uses a value for k of 1, then the script will simply plot the control points as in the Julia example.
<syntaxhighlight lang=ecmascript"wren">import "dome" for Window, Process
import "graphics" for Canvas, Color
 
3,038

edits