Bitmap/Bézier curves/Cubic: Difference between revisions
Content added Content deleted
(→{{header|Lua}}: added Lua solution) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 7: | Line 7: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<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()</ |
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}} |
||
< |
<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</ |
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}}== |
||
< |
<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;</ |
end Cubic_Bezier;</syntaxhighlight> |
||
The following test |
The following test |
||
< |
<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);</ |
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'''< |
'''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</ |
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) |
||
)</ |
)</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]] |
||
< |
<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</ |
ENDPROC</syntaxhighlight> |
||
=={{header|C}}== |
=={{header|C}}== |
||
"Interface" <tt>imglib.h</tt>. |
"Interface" <tt>imglib.h</tt>. |
||
< |
<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 );</ |
color_component b );</syntaxhighlight> |
||
< |
<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</ |
#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. |
||
< |
<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(); |
||
}</ |
}</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... |
||
< |
<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 ;</ |
{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:''' |
||
< |
<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</ |
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]]) |
||
< |
<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</ |
end subroutine cubic_bezier</syntaxhighlight> |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
{{trans|BBC BASIC}} |
{{trans|BBC BASIC}} |
||
< |
<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</ |
End</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<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()) |
||
}</ |
}</syntaxhighlight> |
||
Demonstration program: |
Demonstration program: |
||
[[File:GoBez3.png|thumb|right]] |
[[File:GoBez3.png|thumb|right]] |
||
< |
<syntaxhighlight lang=go>package main |
||
import ( |
import ( |
||
Line 823: | Line 823: | ||
fmt.Println(err) |
fmt.Println(err) |
||
} |
} |
||
}</ |
}</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]]. |
||
< |
<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 ]</ |
drawBezier=: (1&{:: ;~ 2 ]\ [: roundint@getBezierPoints"1 (0&{::))@[ drawLines ]</syntaxhighlight> |
||
'''Example usage:''' |
'''Example usage:''' |
||
< |
<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</ |
viewRGB (randomctrlpts; 255 255 0) drawBezier myimg NB. display image with bezier line</syntaxhighlight> |
||
=={{header|JavaScript}}== |
=={{header|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}} |
||
< |
<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)</ |
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. |
||
< |
<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) |
||
} |
} |
||
}</ |
}</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: |
||
< |
<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'})</ |
bitmap:render({[0x000000]='.', [0xFFFFFFFF]='X'})</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>............................................................. |
<pre>............................................................. |
||
Line 1,061: | Line 1,061: | ||
=={{header|Lambdatalk}}== |
=={{header|Lambdatalk}}== |
||
< |
<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}}== |
||
< |
<syntaxhighlight lang=Mathematica>points= {{0, 0}, {1, 1}, {2, -1}, {3, 0}}; |
||
Graphics[{BSplineCurve[points], Green, Line[points], Red, Point[points]}]</ |
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]]. |
||
< |
<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. |
||
< |
<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. |
||
< |
<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</ |
img.print</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,242: | Line 1,242: | ||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
< |
<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); |
||
;;</ |
;;</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 |
||
< |
<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)</ |
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. |
||
< |
<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]]. |
||
< |
<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) ) ) ) )</ |
(inc 'Y DY) ) ) ) )</syntaxhighlight> |
||
Test: |
Test: |
||
< |
<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")</ |
(call 'display "img.pbm")</syntaxhighlight> |
||
=={{header|Processing}}== |
=={{header|Processing}}== |
||
< |
<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) |
||
*/</ |
*/</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}}== |
||
< |
<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</ |
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: |
||
< |
<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: | ||
| | |
| | |
||
+-----------------+ |
+-----------------+ |
||
'''</ |
'''</syntaxhighlight> |
||
=={{header|R}}== |
=={{header|R}}== |
||
< |
<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")</ |
points(bezierCurve(x,y,20), type="l", col="red")</syntaxhighlight> |
||
=={{header|Racket}}== |
=={{header|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 |
<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;</ |
$*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 |
||
< |
<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)</ |
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>) |
||
< |
<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</ |
pack .new .img .exit -side top</syntaxhighlight> |
||
Results in: |
Results in: |
||
Line 1,872: | Line 1,872: | ||
{{TI-image-task}} |
{{TI-image-task}} |
||
< |
<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</ |
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. |
||
< |
<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) {} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
[[File:CubicXPL0.png|right]] |
[[File:CubicXPL0.png|right]] |
||
< |
<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 |
||
]</ |
]</syntaxhighlight> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
Line 2,012: | Line 2,012: | ||
Add this to the PPM class: |
Add this to the PPM class: |
||
< |
<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); |
||
}); |
}); |
||
}</ |
}</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. |
||
< |
<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"));</ |
bitmap.write(File("foo.ppm","wb"));</syntaxhighlight> |
||
{{omit from|AWK}} |
{{omit from|AWK}} |