B-spline: Difference between revisions

129 bytes added ,  2 months ago
→‎{{header|ALGOL 68}}: Use spaces instead of tabs, tweak
m (→‎{{header|Wren}}: Changed to Wren S/H)
(→‎{{header|ALGOL 68}}: Use spaces instead of tabs, tweak)
 
Line 27:
=={{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 39 ⟶ 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 65 ⟶ 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 80 ⟶ 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 98 ⟶ 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 104 ⟶ 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 122 ⟶ 124:
)
OD
FI # plot bspline # ;
 
# print the plot - outputs @ or blank depending on whether the point is plotted or not #
Line 163 ⟶ 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 176 ⟶ 177:
plot bspline( bs, plot, scale x, scale y );
print plot( plot )
END
END</syntaxhighlight>
{{out}}
leading blank lines removed...
<pre>
 
@@@@
@@@@
Line 220 ⟶ 223:
 
</pre>
 
=={{header|Julia}}==
Choose BSpline D of 2, ie degree 1.
3,026

edits