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

Content added Content deleted
(→‎{{header|Lua}}: added Lua solution)
m (syntax highlighting fixup automation)
Line 7: Line 7:
{{trans|Python}}
{{trans|Python}}


<lang 11l>T Colour
<syntaxhighlight lang=11l>T Colour
Byte r, g, b
Byte r, g, b


Line 96: Line 96:
V bitmap = Bitmap(17, 17)
V bitmap = Bitmap(17, 17)
bitmap.cubicbezier(16, 1, 1, 4, 3, 16, 15, 11)
bitmap.cubicbezier(16, 1, 1, 4, 3, 16, 15, 11)
bitmap.chardisplay()</lang>
bitmap.chardisplay()</syntaxhighlight>


{{out}}
{{out}}
Line 125: Line 125:
{{libheader|Action! Tool Kit}}
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
{{libheader|Action! Real Math}}
<lang Action!>INCLUDE "H6:RGBLINE.ACT" ;from task Bresenham's line algorithm
<syntaxhighlight lang=Action!>INCLUDE "H6:RGBLINE.ACT" ;from task Bresenham's line algorithm
INCLUDE "H6:REALMATH.ACT"
INCLUDE "H6:REALMATH.ACT"


Line 251: Line 251:
DO UNTIL CH#$FF OD
DO UNTIL CH#$FF OD
CH=$FF
CH=$FF
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/B%C3%A9zier_curves_cubic.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/B%C3%A9zier_curves_cubic.png Screenshot from Atari 8-bit computer]


=={{header|Ada}}==
=={{header|Ada}}==
<lang ada>procedure Cubic_Bezier
<syntaxhighlight lang=ada>procedure Cubic_Bezier
( Picture : in out Image;
( Picture : in out Image;
P1, P2, P3, P4 : Point;
P1, P2, P3, P4 : Point;
Line 279: Line 279:
Line (Picture, Points (I), Points (I + 1), Color);
Line (Picture, Points (I), Points (I + 1), Color);
end loop;
end loop;
end Cubic_Bezier;</lang>
end Cubic_Bezier;</syntaxhighlight>
The following test
The following test
<lang ada> X : Image (1..16, 1..16);
<syntaxhighlight lang=ada> X : Image (1..16, 1..16);
begin
begin
Fill (X, White);
Fill (X, White);
Cubic_Bezier (X, (16, 1), (1, 4), (3, 16), (15, 11), Black);
Cubic_Bezier (X, (16, 1), (1, 4), (3, 16), (15, 11), Black);
Print (X);</lang>
Print (X);</syntaxhighlight>
should produce output:
should produce output:
<pre>
<pre>
Line 311: Line 311:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-2.6 algol68g-2.6].}}
{{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''.}}
{{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'''<lang algol68># -*- coding: utf-8 -*- #
'''File: prelude/Bitmap/Bezier_curves/Cubic.a68'''<syntaxhighlight lang=algol68># -*- coding: utf-8 -*- #


cubic bezier OF class image :=
cubic bezier OF class image :=
Line 336: Line 336:
END # cubic bezier #;
END # cubic bezier #;


SKIP</lang>'''File: test/Bitmap/Bezier_curves/Cubic.a68'''<lang algol68>#!/usr/bin/a68g --script #
SKIP</syntaxhighlight>'''File: test/Bitmap/Bezier_curves/Cubic.a68'''<syntaxhighlight lang=algol68>#!/usr/bin/a68g --script #
# -*- coding: utf-8 -*- #
# -*- coding: utf-8 -*- #
Line 349: Line 349:
(cubic bezier OF class image)(x, (16, 1), (1, 4), (3, 16), (15, 11), (black OF class image), EMPTY);
(cubic bezier OF class image)(x, (16, 1), (1, 4), (3, 16), (15, 11), (black OF class image), EMPTY);
(print OF class image) (x)
(print OF class image) (x)
)</lang>'''Output:'''
)</syntaxhighlight>'''Output:'''
<pre>
<pre>
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
Line 372: Line 372:
{{works with|BBC BASIC for Windows}}
{{works with|BBC BASIC for Windows}}
[[Image:beziercubic_bbc.gif|right]]
[[Image:beziercubic_bbc.gif|right]]
<lang bbcbasic> Width% = 200
<syntaxhighlight lang=bbcbasic> Width% = 200
Height% = 200
Height% = 200
Line 423: Line 423:
GCOL 1
GCOL 1
LINE x%*2,y%*2,x%*2,y%*2
LINE x%*2,y%*2,x%*2,y%*2
ENDPROC</lang>
ENDPROC</syntaxhighlight>


=={{header|C}}==
=={{header|C}}==
"Interface" <tt>imglib.h</tt>.
"Interface" <tt>imglib.h</tt>.


<lang c>void cubic_bezier(
<syntaxhighlight lang=c>void cubic_bezier(
image img,
image img,
unsigned int x1, unsigned int y1,
unsigned int x1, unsigned int y1,
Line 436: Line 436:
color_component r,
color_component r,
color_component g,
color_component g,
color_component b );</lang>
color_component b );</syntaxhighlight>


<lang c>#include <math.h>
<syntaxhighlight lang=c>#include <math.h>


/* number of segments for the curve */
/* number of segments for the curve */
Line 491: Line 491:
}
}
#undef plot
#undef plot
#undef line</lang>
#undef line</syntaxhighlight>


=={{header|D}}==
=={{header|D}}==
This solution uses two modules, from the Grayscale image and Bresenham's line algorithm Tasks.
This solution uses two modules, from the Grayscale image and Bresenham's line algorithm Tasks.
<lang d>import grayscale_image, bitmap_bresenhams_line_algorithm;
<syntaxhighlight lang=d>import grayscale_image, bitmap_bresenhams_line_algorithm;


struct Pt { int x, y; } // Signed.
struct Pt { int x, y; } // Signed.
Line 528: Line 528:
Gray.black);
Gray.black);
im.textualShow();
im.textualShow();
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>.................
<pre>.................
Line 549: Line 549:


=={{header|F Sharp|F#}}==
=={{header|F Sharp|F#}}==
<syntaxhighlight lang=f#>
<lang f#>
/// Uses Vector<float> from Microsoft.FSharp.Math (in F# PowerPack)
/// Uses Vector<float> from Microsoft.FSharp.Math (in F# PowerPack)
module CubicBezier
module CubicBezier
Line 567: Line 567:
vector [x; y])
vector [x; y])


</syntaxhighlight>
</lang>
<syntaxhighlight lang=f#>
<lang f#>
// For rendering..
// For rendering..
let drawPoints points (canvas:System.Windows.Controls.Canvas) =
let drawPoints points (canvas:System.Windows.Controls.Canvas) =
Line 582: Line 582:


points |> List.fold renderPoint points.Head
points |> List.fold renderPoint points.Head
</syntaxhighlight>
</lang>


=={{header|Factor}}==
=={{header|Factor}}==
The points should probably be in a sequence...
The points should probably be in a sequence...
<lang factor>USING: arrays kernel locals math math.functions
<syntaxhighlight lang=factor>USING: arrays kernel locals math math.functions
rosettacode.raster.storage sequences ;
rosettacode.raster.storage sequences ;
IN: rosettacode.raster.line
IN: rosettacode.raster.line
Line 610: Line 610:
100 t-interval P0 P1 P2 P3 (cubic-bezier) map
100 t-interval P0 P1 P2 P3 (cubic-bezier) map
points-to-lines
points-to-lines
{R,G,B} swap image draw-lines ;</lang>
{R,G,B} swap image draw-lines ;</syntaxhighlight>


=={{header|FBSL}}==
=={{header|FBSL}}==
Line 616: Line 616:


'''Translation of BBC BASIC using pure FBSL's built-in graphics functions:'''
'''Translation of BBC BASIC using pure FBSL's built-in graphics functions:'''
<lang qbasic>#DEFINE WM_LBUTTONDOWN 513
<syntaxhighlight lang=qbasic>#DEFINE WM_LBUTTONDOWN 513
#DEFINE WM_CLOSE 16
#DEFINE WM_CLOSE 16


Line 672: Line 672:
WEND
WEND
END SUB
END SUB
END SUB</lang>
END SUB</syntaxhighlight>
'''Output:''' [[File:FBSLBezierCube.PNG]]
'''Output:''' [[File:FBSLBezierCube.PNG]]


Line 680: Line 680:
This subroutine should go inside the <code>RCImagePrimitive</code> module (see [[Bresenham's line algorithm]])
This subroutine should go inside the <code>RCImagePrimitive</code> module (see [[Bresenham's line algorithm]])


<lang fortran>subroutine cubic_bezier(img, p1, p2, p3, p4, color)
<syntaxhighlight lang=fortran>subroutine cubic_bezier(img, p1, p2, p3, p4, color)
type(rgbimage), intent(inout) :: img
type(rgbimage), intent(inout) :: img
type(point), intent(in) :: p1, p2, p3, p4
type(point), intent(in) :: p1, p2, p3, p4
Line 706: Line 706:
end do
end do


end subroutine cubic_bezier</lang>
end subroutine cubic_bezier</syntaxhighlight>


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
{{trans|BBC BASIC}}
{{trans|BBC BASIC}}
<lang freebasic>' version 01-11-2016
<syntaxhighlight lang=freebasic>' version 01-11-2016
' compile with: fbc -s console
' compile with: fbc -s console


Line 774: Line 774:
Print : Print "hit any key to end program"
Print : Print "hit any key to end program"
Sleep
Sleep
End</lang>
End</syntaxhighlight>


=={{header|Go}}==
=={{header|Go}}==
{{trans|C}}
{{trans|C}}
<lang go>package raster
<syntaxhighlight lang=go>package raster


const b3Seg = 30
const b3Seg = 30
Line 806: Line 806:
func (b *Bitmap) Bézier3Rgb(x1, y1, x2, y2, x3, y3, x4, y4 int, c Rgb) {
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())
b.Bézier3(x1, y1, x2, y2, x3, y3, x4, y4, c.Pixel())
}</lang>
}</syntaxhighlight>
Demonstration program:
Demonstration program:
[[File:GoBez3.png|thumb|right]]
[[File:GoBez3.png|thumb|right]]
<lang go>package main
<syntaxhighlight lang=go>package main


import (
import (
Line 823: Line 823:
fmt.Println(err)
fmt.Println(err)
}
}
}</lang>
}</syntaxhighlight>


=={{header|J}}==
=={{header|J}}==
Line 829: Line 829:
See the [[J:Essays/Bernstein Polynomials|Bernstein Polynomials essay]] on the [[J:|J Wiki]].<br>
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]].
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]].
<lang j>require 'numeric'
<syntaxhighlight lang=j>require 'numeric'


bik=: 2 : '((*&(u!v))@(^&u * ^&(v-u)@-.))'
bik=: 2 : '((*&(u!v))@(^&u * ^&(v-u)@-.))'
Line 849: Line 849:
NB. eg: (42 40 10 30 186 269 26 187;255 0 0) drawBezier myimg
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)
NB. x is: 2-item list of boxed (controlpoints) ; (color)
drawBezier=: (1&{:: ;~ 2 ]\ [: roundint@getBezierPoints"1 (0&{::))@[ drawLines ]</lang>
drawBezier=: (1&{:: ;~ 2 ]\ [: roundint@getBezierPoints"1 (0&{::))@[ drawLines ]</syntaxhighlight>


'''Example usage:'''
'''Example usage:'''
<lang j>myimg=: 0 0 255 makeRGB 300 300
<syntaxhighlight lang=j>myimg=: 0 0 255 makeRGB 300 300
]randomctrlpts=: ,3 2 ?@$ }:$ myimg NB. 3 control points - quadratic
]randomctrlpts=: ,3 2 ?@$ }:$ myimg NB. 3 control points - quadratic
]randomctrlpts=: ,4 2 ?@$ }:$ myimg NB. 4 control points - cubic
]randomctrlpts=: ,4 2 ?@$ }:$ myimg NB. 4 control points - cubic
myimg=: ((2 ,.~ _2]\randomctrlpts);255 0 255) drawCircles myimg NB. draw control points
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</lang>
viewRGB (randomctrlpts; 255 255 0) drawBezier myimg NB. display image with bezier line</syntaxhighlight>


=={{header|JavaScript}}==
=={{header|JavaScript}}==
<lang javascript>
<syntaxhighlight lang=javascript>
function draw() {
function draw() {
var canvas = document.getElementById("container");
var canvas = document.getElementById("container");
Line 910: Line 910:
context.fill();
context.fill();
}
}
</syntaxhighlight>
</lang>


=={{header|Julia}}==
=={{header|Julia}}==
{{works with|Julia|0.6}}
{{works with|Julia|0.6}}


<lang julia>using Images
<syntaxhighlight lang=julia>using Images


function cubicbezier!(xy::Matrix,
function cubicbezier!(xy::Matrix,
Line 934: Line 934:


xy = [16 1; 1 4; 3 16; 15 11]
xy = [16 1; 1 4; 3 16; 15 11]
cubicbezier!(xy)</lang>
cubicbezier!(xy)</syntaxhighlight>


=={{header|Kotlin}}==
=={{header|Kotlin}}==
This incorporates code from other relevant tasks in order to provide a runnable example.
This incorporates code from other relevant tasks in order to provide a runnable example.
<lang scala>// Version 1.2.40
<syntaxhighlight lang=scala>// Version 1.2.40


import java.awt.Color
import java.awt.Color
Line 1,014: Line 1,014:
ImageIO.write(image, "jpg", cbFile)
ImageIO.write(image, "jpg", cbFile)
}
}
}</lang>
}</syntaxhighlight>


=={{header|Lua}}==
=={{header|Lua}}==
Starting with the code from [[Bitmap/Bresenham's line algorithm#Lua|Bitmap/Bresenham's line algorithm]], then extending:
Starting with the code from [[Bitmap/Bresenham's line algorithm#Lua|Bitmap/Bresenham's line algorithm]], then extending:
<lang lua>Bitmap.cubicbezier = function(self, x1, y1, x2, y2, x3, y3, x4, y4, nseg)
<syntaxhighlight lang=lua>Bitmap.cubicbezier = function(self, x1, y1, x2, y2, x3, y3, x4, y4, nseg)
nseg = nseg or 10
nseg = nseg or 10
local prevx, prevy, currx, curry
local prevx, prevy, currx, curry
Line 1,036: Line 1,036:
bitmap:clear()
bitmap:clear()
bitmap:cubicbezier( 1,1, 15,41, 45,-20, 59,19 )
bitmap:cubicbezier( 1,1, 15,41, 45,-20, 59,19 )
bitmap:render({[0x000000]='.', [0xFFFFFFFF]='X'})</lang>
bitmap:render({[0x000000]='.', [0xFFFFFFFF]='X'})</syntaxhighlight>
{{out}}
{{out}}
<pre>.............................................................
<pre>.............................................................
Line 1,061: Line 1,061:


=={{header|Lambdatalk}}==
=={{header|Lambdatalk}}==
<lang scheme>
<syntaxhighlight lang=scheme>
Drawing a cubic bezier curve out of any SVG or CANVAS frame.
Drawing a cubic bezier curve out of any SVG or CANVAS frame.
1) interpolating 4 points
1) interpolating 4 points
Line 1,136: Line 1,136:
The result can be seen in http://lambdaway.free.fr/lambdawalks/?view=bezier
The result can be seen in http://lambdaway.free.fr/lambdawalks/?view=bezier


</syntaxhighlight>
</lang>




=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<lang Mathematica>points= {{0, 0}, {1, 1}, {2, -1}, {3, 0}};
<syntaxhighlight lang=Mathematica>points= {{0, 0}, {1, 1}, {2, -1}, {3, 0}};
Graphics[{BSplineCurve[points], Green, Line[points], Red, Point[points]}]</lang>
Graphics[{BSplineCurve[points], Green, Line[points], Red, Point[points]}]</syntaxhighlight>
[[File:MmaCubicBezier.png]]
[[File:MmaCubicBezier.png]]


=={{header|MATLAB}}==
=={{header|MATLAB}}==
Note: Store this function in a file named "bezierCubic.mat" in the @Bitmap folder for the Bitmap class defined [[Bitmap#MATLAB|here]].
Note: Store this function in a file named "bezierCubic.mat" in the @Bitmap folder for the Bitmap class defined [[Bitmap#MATLAB|here]].
<lang MATLAB>
<syntaxhighlight lang=MATLAB>
function bezierCubic(obj,pixel_0,pixel_1,pixel_2,pixel_3,color,varargin)
function bezierCubic(obj,pixel_0,pixel_1,pixel_2,pixel_3,color,varargin)


Line 1,177: Line 1,177:
end
end
</syntaxhighlight>
</lang>


Sample usage:
Sample usage:
This will generate the image example for the PHP solution.
This will generate the image example for the PHP solution.
<lang MATLAB>
<syntaxhighlight lang=MATLAB>
>> img = Bitmap(200,200);
>> img = Bitmap(200,200);
>> img.fill([255 255 255]);
>> img.fill([255 255 255]);
>> img.bezierCubic([160 10],[10 40],[30 160],[150 110],[255 0 0],110);
>> img.bezierCubic([160 10],[10 40],[30 160],[150 110],[255 0 0],110);
>> disp(img)
>> disp(img)
</syntaxhighlight>
</lang>


=={{header|Nim}}==
=={{header|Nim}}==
{{Trans|Ada}}
{{Trans|Ada}}
We use module “bitmap” for bitmap management and module “bresenham” to draw segments.
We use module “bitmap” for bitmap management and module “bresenham” to draw segments.
<lang Nim>import bitmap
<syntaxhighlight lang=Nim>import bitmap
import bresenham
import bresenham
import lenientops
import lenientops
Line 1,220: Line 1,220:
img.fill(White)
img.fill(White)
img.drawCubicBezier((0, 15), (3, 0), (15, 2), (10, 14), Black)
img.drawCubicBezier((0, 15), (3, 0), (15, 2), (10, 14), Black)
img.print</lang>
img.print</syntaxhighlight>


{{out}}
{{out}}
Line 1,242: Line 1,242:
=={{header|OCaml}}==
=={{header|OCaml}}==


<lang ocaml>let cubic_bezier ~img ~color
<syntaxhighlight lang=ocaml>let cubic_bezier ~img ~color
~p1:(_x1, _y1)
~p1:(_x1, _y1)
~p2:(_x2, _y2)
~p2:(_x2, _y2)
Line 1,285: Line 1,285:
in
in
by_pair pts (fun p0 p1 -> line ~p0 ~p1);
by_pair pts (fun p0 p1 -> line ~p0 ~p1);
;;</lang>
;;</syntaxhighlight>


=={{header|Phix}}==
=={{header|Phix}}==
Line 1,291: 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>
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
Results may be verified with demo\rosetta\viewppm.exw
<lang Phix>-- demo\rosetta\Bitmap_BezierCubic.exw (runnable version)
<syntaxhighlight lang=Phix>-- demo\rosetta\Bitmap_BezierCubic.exw (runnable version)
include ppm.e -- black, green, red, white, new_image(), write_ppm(), bresLine() -- (covers above requirements)
include ppm.e -- black, green, red, white, new_image(), write_ppm(), bresLine() -- (covers above requirements)


Line 1,321: Line 1,321:
img[200][200] = red
img[200][200] = red
img[300][100] = red
img[300][100] = red
write_ppm("Bezier.ppm",img)</lang>
write_ppm("Bezier.ppm",img)</syntaxhighlight>


=={{header|PHP}}==
=={{header|PHP}}==
Line 1,334: Line 1,334:
Outputs image to the right directly to browser or stdout.
Outputs image to the right directly to browser or stdout.


<lang php><?
<syntaxhighlight lang=php><?


$image = imagecreate(200, 200);
$image = imagecreate(200, 200);
Line 1,363: Line 1,363:
}
}
}
}
</syntaxhighlight>
</lang>


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
This uses the 'brez' line drawing function from
This uses the 'brez' line drawing function from
[[Bitmap/Bresenham's line algorithm#PicoLisp]].
[[Bitmap/Bresenham's line algorithm#PicoLisp]].
<lang PicoLisp>(scl 6)
<syntaxhighlight lang=PicoLisp>(scl 6)


(de cubicBezier (Img N X1 Y1 X2 Y2 X3 Y3 X4 Y4)
(de cubicBezier (Img N X1 Y1 X2 Y2 X3 Y3 X4 Y4)
Line 1,391: Line 1,391:
Y ) ) )
Y ) ) )
(inc 'X DX)
(inc 'X DX)
(inc 'Y DY) ) ) ) )</lang>
(inc 'Y DY) ) ) ) )</syntaxhighlight>
Test:
Test:
<lang PicoLisp>(let Img (make (do 200 (link (need 300 0)))) # Create image 300 x 200
<syntaxhighlight 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)
(cubicBezier Img 24 20 120 540 33 -225 33 285 100)
(out "img.pbm" # Write to bitmap file
(out "img.pbm" # Write to bitmap file
Line 1,400: Line 1,400:
(mapc prinl Img) ) )
(mapc prinl Img) ) )


(call 'display "img.pbm")</lang>
(call 'display "img.pbm")</syntaxhighlight>


=={{header|Processing}}==
=={{header|Processing}}==
<lang Prolog>noFill();
<syntaxhighlight lang=Prolog>noFill();
bezier(85, 20, 10, 10, 90, 90, 15, 80);
bezier(85, 20, 10, 10, 90, 90, 15, 80);
/*
/*
Line 1,409: Line 1,409:
Can also be drawn in 3D.
Can also be drawn in 3D.
bezier(x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4)
bezier(x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4)
*/</lang>'''A working sketch with movable anchor and control points.
*/</syntaxhighlight>'''A working sketch with movable anchor and control points.


'''It can be run on line''' :<BR> [https://www.openprocessing.org/sketch/846556/ here.]
'''It can be run on line''' :<BR> [https://www.openprocessing.org/sketch/846556/ here.]
<lang>
<syntaxhighlight lang=text>
float[] x = new float[4];
float[] x = new float[4];
float[] y = new float[4];
float[] y = new float[4];
Line 1,485: Line 1,485:
}
}
}
}
</syntaxhighlight>
</lang>


=={{header|PureBasic}}==
=={{header|PureBasic}}==
<lang PureBasic>Procedure cubic_bezier(img, p1x, p1y, p2x, p2y, p3x, p3y, p4x, p4y, Color, n_seg)
<syntaxhighlight lang=PureBasic>Procedure cubic_bezier(img, p1x, p1y, p2x, p2y, p3x, p3y, p4x, p4y, Color, n_seg)
Protected i
Protected i
Protected.f t, t1, a, b, c, d
Protected.f t, t1, a, b, c, d
Line 1,523: Line 1,523:
Repeat
Repeat
event = WaitWindowEvent()
event = WaitWindowEvent()
Until event = #PB_Event_CloseWindow</lang>
Until event = #PB_Event_CloseWindow</syntaxhighlight>


=={{header|Python}}==
=={{header|Python}}==
Line 1,529: Line 1,529:


Extending the example given [[Bresenham's line algorithm#Python|here]] and using the algorithm from the C solution above:
Extending the example given [[Bresenham's line algorithm#Python|here]] and using the algorithm from the C solution above:
<lang python>def cubicbezier(self, x0, y0, x1, y1, x2, y2, x3, y3, n=20):
<syntaxhighlight lang=python>def cubicbezier(self, x0, y0, x1, y1, x2, y2, x3, y3, n=20):
pts = []
pts = []
for i in range(n+1):
for i in range(n+1):
Line 1,574: Line 1,574:
| |
| |
+-----------------+
+-----------------+
'''</lang>
'''</syntaxhighlight>


=={{header|R}}==
=={{header|R}}==
<lang R># x, y: the x and y coordinates of the hull points
<syntaxhighlight lang=R># x, y: the x and y coordinates of the hull points
# n: the number of points in the curve.
# n: the number of points in the curve.
bezierCurve <- function(x, y, n=10)
bezierCurve <- function(x, y, n=10)
Line 1,615: Line 1,615:
y <- 1:6
y <- 1:6
plot(x, y, "o", pch=20)
plot(x, y, "o", pch=20)
points(bezierCurve(x,y,20), type="l", col="red")</lang>
points(bezierCurve(x,y,20), type="l", col="red")</syntaxhighlight>


=={{header|Racket}}==
=={{header|Racket}}==
<lang racket>
<syntaxhighlight lang=racket>
#lang racket
#lang racket
(require racket/draw)
(require racket/draw)
Line 1,645: Line 1,645:
(draw-lines dc (bezier-points '(16 1) '(1 4) '(3 16) '(15 11)))
(draw-lines dc (bezier-points '(16 1) '(1 4) '(3 16) '(15 11)))
bm
bm
</syntaxhighlight>
</lang>


=={{header|Raku}}==
=={{header|Raku}}==
Line 1,652: 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.
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.


<lang perl6>class Pixel { has UInt ($.R, $.G, $.B) }
<syntaxhighlight lang=raku line>class Pixel { has UInt ($.R, $.G, $.B) }
class Bitmap {
class Bitmap {
has UInt ($.width, $.height);
has UInt ($.width, $.height);
Line 1,750: Line 1,750:
@points.map: { $b.dot( $_, color(255,0,0), 3 )}
@points.map: { $b.dot( $_, color(255,0,0), 3 )}


$*OUT.write: $b.P6;</lang>
$*OUT.write: $b.P6;</syntaxhighlight>


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).
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: Line 1,759:
Requires code from the [[Bitmap#Ruby|Bitmap]] and [[Bitmap/Bresenham's line algorithm#Ruby Bresenham's line algorithm]] tasks
Requires code from the [[Bitmap#Ruby|Bitmap]] and [[Bitmap/Bresenham's line algorithm#Ruby Bresenham's line algorithm]] tasks


<lang ruby>class Pixmap
<syntaxhighlight lang=ruby>class Pixmap
def draw_bezier_curve(points, colour)
def draw_bezier_curve(points, colour)
# ensure the points are increasing along the x-axis
# ensure the points are increasing along the x-axis
Line 1,799: Line 1,799:
]
]
points.each {|p| bitmap.draw_circle(p, 3, RGBColour::RED)}
points.each {|p| bitmap.draw_circle(p, 3, RGBColour::RED)}
bitmap.draw_bezier_curve(points, RGBColour::BLUE)</lang>
bitmap.draw_bezier_curve(points, RGBColour::BLUE)</syntaxhighlight>


=={{header|Tcl}}==
=={{header|Tcl}}==
{{libheader|Tk}}
{{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>)
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>)
<lang tcl>package require Tcl 8.5
<syntaxhighlight lang=tcl>package require Tcl 8.5
package require Tk
package require Tk


Line 1,863: Line 1,863:
button .new -command [list newbezier $degree .img] -text New
button .new -command [list newbezier $degree .img] -text New
button .exit -command exit -text Exit
button .exit -command exit -text Exit
pack .new .img .exit -side top</lang>
pack .new .img .exit -side top</syntaxhighlight>
Results in:
Results in:


Line 1,872: Line 1,872:
{{TI-image-task}}
{{TI-image-task}}


<lang ti89b>Define cubic(p1,p2,p3,p4,segs) = Prgm
<syntaxhighlight lang=ti89b>Define cubic(p1,p2,p3,p4,segs) = Prgm
Local i,t,u,prev,pt
Local i,t,u,prev,pt
0 → pt
0 → pt
Line 1,884: Line 1,884:
EndIf
EndIf
EndFor
EndFor
EndPrgm</lang>
EndPrgm</syntaxhighlight>


=={{header|Wren}}==
=={{header|Wren}}==
{{libheader|DOME}}
{{libheader|DOME}}
Requires version 1.3.0 of DOME or later.
Requires version 1.3.0 of DOME or later.
<lang ecmascript>import "graphics" for Canvas, ImageData, Color, Point
<syntaxhighlight lang=ecmascript>import "graphics" for Canvas, ImageData, Color, Point
import "dome" for Window
import "dome" for Window


Line 1,965: Line 1,965:


static draw(alpha) {}
static draw(alpha) {}
}</lang>
}</syntaxhighlight>


=={{header|XPL0}}==
=={{header|XPL0}}==
[[File:CubicXPL0.png|right]]
[[File:CubicXPL0.png|right]]
<lang XPL0>include c:\cxpl\codes; \intrinsic 'code' declarations
<syntaxhighlight lang=XPL0>include c:\cxpl\codes; \intrinsic 'code' declarations


proc Bezier(P0, P1, P2, P3); \Draw cubic Bezier curve
proc Bezier(P0, P1, P2, P3); \Draw cubic Bezier curve
Line 2,003: Line 2,003:
if ChIn(1) then []; \wait for keystroke
if ChIn(1) then []; \wait for keystroke
SetVid(3); \restore normal text display
SetVid(3); \restore normal text display
]</lang>
]</syntaxhighlight>


=={{header|zkl}}==
=={{header|zkl}}==
Line 2,012: Line 2,012:


Add this to the PPM class:
Add this to the PPM class:
<lang zkl> fcn cBezier(p0x,p0y, p1x,p1y, p2x,p2y, p3x,p3y, rgb, numPts=500){
<syntaxhighlight lang=zkl> fcn cBezier(p0x,p0y, p1x,p1y, p2x,p2y, p3x,p3y, rgb, numPts=500){
numPts.pump(Void,'wrap(t){ // B(t)
numPts.pump(Void,'wrap(t){ // B(t)
t=t.toFloat()/numPts; t1:=(1.0 - t);
t=t.toFloat()/numPts; t1:=(1.0 - t);
Line 2,020: Line 2,020:
__sSet(rgb,x,y);
__sSet(rgb,x,y);
});
});
}</lang>
}</syntaxhighlight>
Doesn't use line segments, they don't seem like an improvement.
Doesn't use line segments, they don't seem like an improvement.
<lang zkl>bitmap:=PPM(200,150,0xff|ff|ff);
<syntaxhighlight lang=zkl>bitmap:=PPM(200,150,0xff|ff|ff);
bitmap.cBezier(0,149, 30,50, 120,130, 160,30, 0);
bitmap.cBezier(0,149, 30,50, 120,130, 160,30, 0);
bitmap.write(File("foo.ppm","wb"));</lang>
bitmap.write(File("foo.ppm","wb"));</syntaxhighlight>


{{omit from|AWK}}
{{omit from|AWK}}