Bitmap/Bézier curves/Cubic: Difference between revisions

m
syntax highlighting fixup automation
(→‎{{header|Lua}}: added Lua solution)
m (syntax highlighting fixup automation)
Line 7:
{{trans|Python}}
 
<langsyntaxhighlight lang=11l>T Colour
Byte r, g, b
 
Line 96:
V bitmap = Bitmap(17, 17)
bitmap.cubicbezier(16, 1, 1, 4, 3, 16, 15, 11)
bitmap.chardisplay()</langsyntaxhighlight>
 
{{out}}
Line 125:
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
<langsyntaxhighlight lang=Action!>INCLUDE "H6:RGBLINE.ACT" ;from task Bresenham's line algorithm
INCLUDE "H6:REALMATH.ACT"
 
Line 251:
DO UNTIL CH#$FF OD
CH=$FF
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/B%C3%A9zier_curves_cubic.png Screenshot from Atari 8-bit computer]
 
=={{header|Ada}}==
<langsyntaxhighlight lang=ada>procedure Cubic_Bezier
( Picture : in out Image;
P1, P2, P3, P4 : Point;
Line 279:
Line (Picture, Points (I), Points (I + 1), Color);
end loop;
end Cubic_Bezier;</langsyntaxhighlight>
The following test
<langsyntaxhighlight lang=ada> X : Image (1..16, 1..16);
begin
Fill (X, White);
Cubic_Bezier (X, (16, 1), (1, 4), (3, 16), (15, 11), Black);
Print (X);</langsyntaxhighlight>
should produce output:
<pre>
Line 311:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-2.6 algol68g-2.6].}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''.}}
'''File: prelude/Bitmap/Bezier_curves/Cubic.a68'''<langsyntaxhighlight lang=algol68># -*- coding: utf-8 -*- #
 
cubic bezier OF class image :=
Line 336:
END # cubic bezier #;
 
SKIP</langsyntaxhighlight>'''File: test/Bitmap/Bezier_curves/Cubic.a68'''<langsyntaxhighlight lang=algol68>#!/usr/bin/a68g --script #
# -*- coding: utf-8 -*- #
Line 349:
(cubic bezier OF class image)(x, (16, 1), (1, 4), (3, 16), (15, 11), (black OF class image), EMPTY);
(print OF class image) (x)
)</langsyntaxhighlight>'''Output:'''
<pre>
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
Line 372:
{{works with|BBC BASIC for Windows}}
[[Image:beziercubic_bbc.gif|right]]
<langsyntaxhighlight lang=bbcbasic> Width% = 200
Height% = 200
Line 423:
GCOL 1
LINE x%*2,y%*2,x%*2,y%*2
ENDPROC</langsyntaxhighlight>
 
=={{header|C}}==
"Interface" <tt>imglib.h</tt>.
 
<langsyntaxhighlight lang=c>void cubic_bezier(
image img,
unsigned int x1, unsigned int y1,
Line 436:
color_component r,
color_component g,
color_component b );</langsyntaxhighlight>
 
<langsyntaxhighlight lang=c>#include <math.h>
 
/* number of segments for the curve */
Line 491:
}
#undef plot
#undef line</langsyntaxhighlight>
 
=={{header|D}}==
This solution uses two modules, from the Grayscale image and Bresenham's line algorithm Tasks.
<langsyntaxhighlight lang=d>import grayscale_image, bitmap_bresenhams_line_algorithm;
 
struct Pt { int x, y; } // Signed.
Line 528:
Gray.black);
im.textualShow();
}</langsyntaxhighlight>
{{out}}
<pre>.................
Line 549:
 
=={{header|F Sharp|F#}}==
<syntaxhighlight lang=f#>
<lang f#>
/// Uses Vector<float> from Microsoft.FSharp.Math (in F# PowerPack)
module CubicBezier
Line 567:
vector [x; y])
 
</syntaxhighlight>
</lang>
<syntaxhighlight lang=f#>
<lang f#>
// For rendering..
let drawPoints points (canvas:System.Windows.Controls.Canvas) =
Line 582:
 
points |> List.fold renderPoint points.Head
</syntaxhighlight>
</lang>
 
=={{header|Factor}}==
The points should probably be in a sequence...
<langsyntaxhighlight lang=factor>USING: arrays kernel locals math math.functions
rosettacode.raster.storage sequences ;
IN: rosettacode.raster.line
Line 610:
100 t-interval P0 P1 P2 P3 (cubic-bezier) map
points-to-lines
{R,G,B} swap image draw-lines ;</langsyntaxhighlight>
 
=={{header|FBSL}}==
Line 616:
 
'''Translation of BBC BASIC using pure FBSL's built-in graphics functions:'''
<langsyntaxhighlight lang=qbasic>#DEFINE WM_LBUTTONDOWN 513
#DEFINE WM_CLOSE 16
 
Line 672:
WEND
END SUB
END SUB</langsyntaxhighlight>
'''Output:''' [[File:FBSLBezierCube.PNG]]
 
Line 680:
This subroutine should go inside the <code>RCImagePrimitive</code> module (see [[Bresenham's line algorithm]])
 
<langsyntaxhighlight lang=fortran>subroutine cubic_bezier(img, p1, p2, p3, p4, color)
type(rgbimage), intent(inout) :: img
type(point), intent(in) :: p1, p2, p3, p4
Line 706:
end do
 
end subroutine cubic_bezier</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
{{trans|BBC BASIC}}
<langsyntaxhighlight lang=freebasic>' version 01-11-2016
' compile with: fbc -s console
 
Line 774:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
 
=={{header|Go}}==
{{trans|C}}
<langsyntaxhighlight lang=go>package raster
 
const b3Seg = 30
Line 806:
func (b *Bitmap) Bézier3Rgb(x1, y1, x2, y2, x3, y3, x4, y4 int, c Rgb) {
b.Bézier3(x1, y1, x2, y2, x3, y3, x4, y4, c.Pixel())
}</langsyntaxhighlight>
Demonstration program:
[[File:GoBez3.png|thumb|right]]
<langsyntaxhighlight lang=go>package main
 
import (
Line 823:
fmt.Println(err)
}
}</langsyntaxhighlight>
 
=={{header|J}}==
Line 829:
See the [[J:Essays/Bernstein Polynomials|Bernstein Polynomials essay]] on the [[J:|J Wiki]].<br>
Uses code from [[Basic_bitmap_storage#J|Basic bitmap storage]], [[Bresenham's_line_algorithm#J|Bresenham's line algorithm]] and [[Midpoint_circle_algorithm#J|Midpoint circle algorithm]].
<langsyntaxhighlight lang=j>require 'numeric'
 
bik=: 2 : '((*&(u!v))@(^&u * ^&(v-u)@-.))'
Line 849:
NB. eg: (42 40 10 30 186 269 26 187;255 0 0) drawBezier myimg
NB. x is: 2-item list of boxed (controlpoints) ; (color)
drawBezier=: (1&{:: ;~ 2 ]\ [: roundint@getBezierPoints"1 (0&{::))@[ drawLines ]</langsyntaxhighlight>
 
'''Example usage:'''
<langsyntaxhighlight lang=j>myimg=: 0 0 255 makeRGB 300 300
]randomctrlpts=: ,3 2 ?@$ }:$ myimg NB. 3 control points - quadratic
]randomctrlpts=: ,4 2 ?@$ }:$ myimg NB. 4 control points - cubic
myimg=: ((2 ,.~ _2]\randomctrlpts);255 0 255) drawCircles myimg NB. draw control points
viewRGB (randomctrlpts; 255 255 0) drawBezier myimg NB. display image with bezier line</langsyntaxhighlight>
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang=javascript>
function draw() {
var canvas = document.getElementById("container");
Line 910:
context.fill();
}
</syntaxhighlight>
</lang>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang=julia>using Images
 
function cubicbezier!(xy::Matrix,
Line 934:
 
xy = [16 1; 1 4; 3 16; 15 11]
cubicbezier!(xy)</langsyntaxhighlight>
 
=={{header|Kotlin}}==
This incorporates code from other relevant tasks in order to provide a runnable example.
<langsyntaxhighlight lang=scala>// Version 1.2.40
 
import java.awt.Color
Line 1,014:
ImageIO.write(image, "jpg", cbFile)
}
}</langsyntaxhighlight>
 
=={{header|Lua}}==
Starting with the code from [[Bitmap/Bresenham's line algorithm#Lua|Bitmap/Bresenham's line algorithm]], then extending:
<langsyntaxhighlight lang=lua>Bitmap.cubicbezier = function(self, x1, y1, x2, y2, x3, y3, x4, y4, nseg)
nseg = nseg or 10
local prevx, prevy, currx, curry
Line 1,036:
bitmap:clear()
bitmap:cubicbezier( 1,1, 15,41, 45,-20, 59,19 )
bitmap:render({[0x000000]='.', [0xFFFFFFFF]='X'})</langsyntaxhighlight>
{{out}}
<pre>.............................................................
Line 1,061:
 
=={{header|Lambdatalk}}==
<langsyntaxhighlight lang=scheme>
Drawing a cubic bezier curve out of any SVG or CANVAS frame.
1) interpolating 4 points
Line 1,136:
The result can be seen in http://lambdaway.free.fr/lambdawalks/?view=bezier
 
</syntaxhighlight>
</lang>
 
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight lang=Mathematica>points= {{0, 0}, {1, 1}, {2, -1}, {3, 0}};
Graphics[{BSplineCurve[points], Green, Line[points], Red, Point[points]}]</langsyntaxhighlight>
[[File:MmaCubicBezier.png]]
 
=={{header|MATLAB}}==
Note: Store this function in a file named "bezierCubic.mat" in the @Bitmap folder for the Bitmap class defined [[Bitmap#MATLAB|here]].
<langsyntaxhighlight lang=MATLAB>
function bezierCubic(obj,pixel_0,pixel_1,pixel_2,pixel_3,color,varargin)
 
Line 1,177:
end
</syntaxhighlight>
</lang>
 
Sample usage:
This will generate the image example for the PHP solution.
<langsyntaxhighlight lang=MATLAB>
>> img = Bitmap(200,200);
>> img.fill([255 255 255]);
>> img.bezierCubic([160 10],[10 40],[30 160],[150 110],[255 0 0],110);
>> disp(img)
</syntaxhighlight>
</lang>
 
=={{header|Nim}}==
{{Trans|Ada}}
We use module “bitmap” for bitmap management and module “bresenham” to draw segments.
<langsyntaxhighlight lang=Nim>import bitmap
import bresenham
import lenientops
Line 1,220:
img.fill(White)
img.drawCubicBezier((0, 15), (3, 0), (15, 2), (10, 14), Black)
img.print</langsyntaxhighlight>
 
{{out}}
Line 1,242:
=={{header|OCaml}}==
 
<langsyntaxhighlight lang=ocaml>let cubic_bezier ~img ~color
~p1:(_x1, _y1)
~p2:(_x2, _y2)
Line 1,285:
in
by_pair pts (fun p0 p1 -> line ~p0 ~p1);
;;</langsyntaxhighlight>
 
=={{header|Phix}}==
Line 1,291:
Requires new_image() from [[Bitmap#Phix|Bitmap]], bresLine() from [[Bitmap/Bresenham's_line_algorithm#Phix|Bresenham's_line_algorithm]], write_ppm() from [[Bitmap/Write_a_PPM_file#Phix|Write_a_PPM_file]]. <br>
Results may be verified with demo\rosetta\viewppm.exw
<langsyntaxhighlight lang=Phix>-- demo\rosetta\Bitmap_BezierCubic.exw (runnable version)
include ppm.e -- black, green, red, white, new_image(), write_ppm(), bresLine() -- (covers above requirements)
 
Line 1,321:
img[200][200] = red
img[300][100] = red
write_ppm("Bezier.ppm",img)</langsyntaxhighlight>
 
=={{header|PHP}}==
Line 1,334:
Outputs image to the right directly to browser or stdout.
 
<langsyntaxhighlight lang=php><?
 
$image = imagecreate(200, 200);
Line 1,363:
}
}
</syntaxhighlight>
</lang>
 
=={{header|PicoLisp}}==
This uses the 'brez' line drawing function from
[[Bitmap/Bresenham's line algorithm#PicoLisp]].
<langsyntaxhighlight lang=PicoLisp>(scl 6)
 
(de cubicBezier (Img N X1 Y1 X2 Y2 X3 Y3 X4 Y4)
Line 1,391:
Y ) ) )
(inc 'X DX)
(inc 'Y DY) ) ) ) )</langsyntaxhighlight>
Test:
<langsyntaxhighlight lang=PicoLisp>(let Img (make (do 200 (link (need 300 0)))) # Create image 300 x 200
(cubicBezier Img 24 20 120 540 33 -225 33 285 100)
(out "img.pbm" # Write to bitmap file
Line 1,400:
(mapc prinl Img) ) )
 
(call 'display "img.pbm")</langsyntaxhighlight>
 
=={{header|Processing}}==
<langsyntaxhighlight lang=Prolog>noFill();
bezier(85, 20, 10, 10, 90, 90, 15, 80);
/*
Line 1,409:
Can also be drawn in 3D.
bezier(x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4)
*/</langsyntaxhighlight>'''A working sketch with movable anchor and control points.
 
'''It can be run on line''' :<BR> [https://www.openprocessing.org/sketch/846556/ here.]
<syntaxhighlight lang=text>
float[] x = new float[4];
float[] y = new float[4];
Line 1,485:
}
}
</syntaxhighlight>
</lang>
 
=={{header|PureBasic}}==
<langsyntaxhighlight lang=PureBasic>Procedure cubic_bezier(img, p1x, p1y, p2x, p2y, p3x, p3y, p4x, p4y, Color, n_seg)
Protected i
Protected.f t, t1, a, b, c, d
Line 1,523:
Repeat
event = WaitWindowEvent()
Until event = #PB_Event_CloseWindow</langsyntaxhighlight>
 
=={{header|Python}}==
Line 1,529:
 
Extending the example given [[Bresenham's line algorithm#Python|here]] and using the algorithm from the C solution above:
<langsyntaxhighlight lang=python>def cubicbezier(self, x0, y0, x1, y1, x2, y2, x3, y3, n=20):
pts = []
for i in range(n+1):
Line 1,574:
| |
+-----------------+
'''</langsyntaxhighlight>
 
=={{header|R}}==
<langsyntaxhighlight lang=R># x, y: the x and y coordinates of the hull points
# n: the number of points in the curve.
bezierCurve <- function(x, y, n=10)
Line 1,615:
y <- 1:6
plot(x, y, "o", pch=20)
points(bezierCurve(x,y,20), type="l", col="red")</langsyntaxhighlight>
 
=={{header|Racket}}==
<langsyntaxhighlight lang=racket>
#lang racket
(require racket/draw)
Line 1,645:
(draw-lines dc (bezier-points '(16 1) '(1 4) '(3 16) '(15 11)))
bm
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 1,652:
Uses pieces from [[Bitmap#Raku| Bitmap]], and [[Bitmap/Bresenham's_line_algorithm#Raku| Bresenham's line algorithm]] tasks. They are included here to make a complete, runnable program.
 
<syntaxhighlight lang=raku perl6line>class Pixel { has UInt ($.R, $.G, $.B) }
class Bitmap {
has UInt ($.width, $.height);
Line 1,750:
@points.map: { $b.dot( $_, color(255,0,0), 3 )}
 
$*OUT.write: $b.P6;</langsyntaxhighlight>
 
See [https://github.com/thundergnat/rc/blob/master/img/Bezier-cubic-perl6.png example image here], (converted to a .png as .ppm format is not widely supported).
Line 1,759:
Requires code from the [[Bitmap#Ruby|Bitmap]] and [[Bitmap/Bresenham's line algorithm#Ruby Bresenham's line algorithm]] tasks
 
<langsyntaxhighlight lang=ruby>class Pixmap
def draw_bezier_curve(points, colour)
# ensure the points are increasing along the x-axis
Line 1,799:
]
points.each {|p| bitmap.draw_circle(p, 3, RGBColour::RED)}
bitmap.draw_bezier_curve(points, RGBColour::BLUE)</langsyntaxhighlight>
 
=={{header|Tcl}}==
{{libheader|Tk}}
This solution can be applied to any number of points. Uses code from [[Basic_bitmap_storage#Tcl|Basic bitmap storage]] (<tt>newImage</tt>, <tt>fill</tt>), [[Bresenham's_line_algorithm#Tcl|Bresenham's line algorithm]] (<tt>drawLine</tt>), and [[Midpoint_circle_algorithm#Tcl|Midpoint circle algorithm]] (<tt>drawCircle</tt>)
<langsyntaxhighlight lang=tcl>package require Tcl 8.5
package require Tk
 
Line 1,863:
button .new -command [list newbezier $degree .img] -text New
button .exit -command exit -text Exit
pack .new .img .exit -side top</langsyntaxhighlight>
Results in:
 
Line 1,872:
{{TI-image-task}}
 
<langsyntaxhighlight lang=ti89b>Define cubic(p1,p2,p3,p4,segs) = Prgm
Local i,t,u,prev,pt
0 → pt
Line 1,884:
EndIf
EndFor
EndPrgm</langsyntaxhighlight>
 
=={{header|Wren}}==
{{libheader|DOME}}
Requires version 1.3.0 of DOME or later.
<langsyntaxhighlight lang=ecmascript>import "graphics" for Canvas, ImageData, Color, Point
import "dome" for Window
 
Line 1,965:
 
static draw(alpha) {}
}</langsyntaxhighlight>
 
=={{header|XPL0}}==
[[File:CubicXPL0.png|right]]
<langsyntaxhighlight lang=XPL0>include c:\cxpl\codes; \intrinsic 'code' declarations
 
proc Bezier(P0, P1, P2, P3); \Draw cubic Bezier curve
Line 2,003:
if ChIn(1) then []; \wait for keystroke
SetVid(3); \restore normal text display
]</langsyntaxhighlight>
 
=={{header|zkl}}==
Line 2,012:
 
Add this to the PPM class:
<langsyntaxhighlight lang=zkl> fcn cBezier(p0x,p0y, p1x,p1y, p2x,p2y, p3x,p3y, rgb, numPts=500){
numPts.pump(Void,'wrap(t){ // B(t)
t=t.toFloat()/numPts; t1:=(1.0 - t);
Line 2,020:
__sSet(rgb,x,y);
});
}</langsyntaxhighlight>
Doesn't use line segments, they don't seem like an improvement.
<langsyntaxhighlight lang=zkl>bitmap:=PPM(200,150,0xff|ff|ff);
bitmap.cBezier(0,149, 30,50, 120,130, 160,30, 0);
bitmap.write(File("foo.ppm","wb"));</langsyntaxhighlight>
 
{{omit from|AWK}}
10,333

edits