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

m
Automated syntax highlighting fixup (second round - minor fixes)
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 3:
Using the data storage type defined [[Basic_bitmap_storage|on this page]] for raster images, and the <tt>draw_line</tt> function defined in [[Bresenham's_line_algorithm|this one]], draw a ''quadratic bezier curve''
([[wp:Bezier_curves#Quadratic_B.C3.A9zier_curves|definition on Wikipedia]]).
 
=={{header|Action!}}==
{{libheader|Action! Bitmap tools}}
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
<syntaxhighlight lang=Action"action!">INCLUDE "H6:RGBLINE.ACT" ;from task Bresenham's line algorithm
INCLUDE "H6:REALMATH.ACT"
 
Line 122 ⟶ 121:
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/B%C3%A9zier_curves_quadratic.png Screenshot from Atari 8-bit computer]
 
=={{header|Ada}}==
<syntaxhighlight lang="ada">procedure Quadratic_Bezier
( Picture : in out Image;
P1, P2, P3 : Point;
Line 148 ⟶ 146:
end Quadratic_Bezier;</syntaxhighlight>
The following test
<syntaxhighlight lang="ada"> X : Image (1..16, 1..16);
begin
Fill (X, White);
Line 172 ⟶ 170:
 
</pre>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
[[Image:bezierquad_bbc.gif|right]]
<syntaxhighlight lang="bbcbasic"> Width% = 200
Height% = 200
Line 227 ⟶ 224:
LINE x%*2,y%*2,x%*2,y%*2
ENDPROC</syntaxhighlight>
 
=={{header|C}}==
 
Interface (to be added to all other to make the final <tt>imglib.h</tt>):
 
<syntaxhighlight lang="c">void quad_bezier(
image img,
unsigned int x1, unsigned int y1,
Line 243 ⟶ 239:
Implementation:
 
<syntaxhighlight lang="c">#include <math.h>
 
/* number of segments for the curve */
Line 293 ⟶ 289:
#undef plot
#undef line</syntaxhighlight>
 
=={{header|Commodore Basic}}==
<syntaxhighlight lang="basic">
10 rem bezier curve algorihm
20 rem translated from purebasic
Line 354 ⟶ 349:
</syntaxhighlight>
[https://www.worldofchris.com/assets/c64-bezier-curve.png Screenshot of Bézier curve on C64]
 
=={{header|D}}==
This solution uses two modules, from the Grayscale image and the Bresenham's line algorithm Tasks.
<syntaxhighlight lang="d">import grayscale_image, bitmap_bresenhams_line_algorithm;
 
struct Pt { int x, y; } // Signed.
Line 407 ⟶ 401:
....................
....................</pre>
 
=={{header|Factor}}==
Some code is shared with the cubic bezier task, but I put it here again to make it simple (hoping the two version don't diverge)
Same remark as with cubic bezier, the points could go into a sequence to simplify stack shuffling
<syntaxhighlight lang="factor">USING: arrays kernel locals math math.functions
rosettacode.raster.storage sequences ;
IN: rosettacode.raster.line
Line 435 ⟶ 428:
{R,G,B} swap image draw-lines ;
</syntaxhighlight>
 
=={{header|FBSL}}==
Windows' graphics origin is located at the bottom-left corner of device bitmap.
 
'''Translation of BBC BASIC using pure FBSL's built-in graphics functions:'''
<syntaxhighlight lang="qbasic">#DEFINE WM_LBUTTONDOWN 513
#DEFINE WM_CLOSE 16
 
Line 497 ⟶ 489:
END SUB</syntaxhighlight>
'''Output:''' [[File:FBSLBezierQuad.PNG]]
 
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
Line 503 ⟶ 494:
(This subroutine must be inside the <code>RCImagePrimitive</code> module, see [[Bresenham's line algorithm#Fortran|here]])
 
<syntaxhighlight lang="fortran">subroutine quad_bezier(img, p1, p2, p3, color)
type(rgbimage), intent(inout) :: img
type(point), intent(in) :: p1, p2, p3
Line 529 ⟶ 520:
 
end subroutine quad_bezier</syntaxhighlight>
 
=={{header|FreeBASIC}}==
{{trans|BBC BASIC}}
<syntaxhighlight lang="freebasic">' version 01-11-2016
' compile with: fbc -s console
 
Line 595 ⟶ 585:
Sleep
End</syntaxhighlight>
 
=={{header|Go}}==
{{trans|C}}
<syntaxhighlight lang="go">package raster
 
const b2Seg = 20
Line 627 ⟶ 616:
Demonstration program:
[[File:GoBez2.png|thumb|right]]
<syntaxhighlight lang="go">package main
 
import (
Line 642 ⟶ 631:
}
}</syntaxhighlight>
 
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">{-# LANGUAGE
FlexibleInstances, TypeSynonymInstances,
ViewPatterns #-}
Line 691 ⟶ 679:
f (curvePoint -> p1) (curvePoint -> p2) =
line i (toPixel p1) (toPixel p2) c</syntaxhighlight>
 
=={{header|J}}==
See [[Cubic bezier curves#J|Cubic bezier curves]] for a generalized solution.
 
=={{header|Julia}}==
See [[Cubic bezier curves#Julia]] for a generalized solution.
 
=={{header|Kotlin}}==
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
Line 777 ⟶ 762:
=={{header|Lua}}==
Starting with the code from [[Bitmap/Bresenham's line algorithm#Lua|Bitmap/Bresenham's line algorithm]], then extending:
<syntaxhighlight lang="lua">Bitmap.quadraticbezier = function(self, x1, y1, x2, y2, x3, y3, nseg)
nseg = nseg or 10
local prevx, prevy, currx, curry
Line 818 ⟶ 803:
...........................XXXXXX............................
.............................................................</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica"mathematica">pts = {{0, 0}, {1, -1}, {2, 1}};
Graphics[{BSplineCurve[pts], Green, Line[pts], Red, Point[pts]}]</syntaxhighlight>
Second solution using built-in function BezierCurve.
<syntaxhighlight lang=Mathematica"mathematica">pts = {{0, 0}, {1, -1}, {2, 1}};
Graphics[{BezierCurve[pts], Green, Line[pts], Red, Point[pts]}]</syntaxhighlight>
[[File:MmaQuadraticBezier.png]]
 
=={{header|MATLAB}}==
Note: Store this function in a file named "bezierQuad.mat" in the @Bitmap folder for the Bitmap class defined [[Bitmap#MATLAB|here]].
<syntaxhighlight lang=MATLAB"matlab">
function bezierQuad(obj,pixel_0,pixel_1,pixel_2,color,varargin)
 
Line 863 ⟶ 846:
Sample usage:
This will generate the image example for the Go solution.
<syntaxhighlight lang=MATLAB"matlab">
>> img = Bitmap(400,300);
>> img.fill([223 255 239]);
Line 869 ⟶ 852:
>> disp(img)
</syntaxhighlight>
 
=={{header|Nim}}==
{{trans|Ada}}
We use module “bitmap” for bitmap management and module “bresenham” to draw segments.
<syntaxhighlight lang=Nim"nim">import bitmap
import bresenham
import lenientops
Line 915 ⟶ 897:
................
................</pre>
 
=={{header|OCaml}}==
 
<syntaxhighlight lang="ocaml">let quad_bezier ~img ~color
~p1:(_x1, _y1)
~p2:(_x2, _y2)
Line 957 ⟶ 938:
by_pair pts (fun p0 p1 -> line ~p0 ~p1);
;;</syntaxhighlight>
 
=={{header|Phix}}==
Output similar to [[Bitmap/Bézier_curves/Quadratic#Mathematica|Mathematica]]<br>
Requires new_image() from [[Bitmap#Phix|Bitmap]], bresLine() from [[Bitmap/Bresenham's_line_algorithm#Phix|Bresenham's_line_algorithm]], and write_ppm() from [[Bitmap/Write_a_PPM_file#Phix|Write_a_PPM_file]]. <br>
Results may be verified with demo\rosetta\viewppm.exw
<syntaxhighlight lang=Phix"phix">-- demo\rosetta\Bitmap_BezierQuadratic.exw
include ppm.e -- black, green, red, white, new_image(), write_ppm(), bresLine() -- (covers above requirements)
 
Line 990 ⟶ 970:
img[200][1] = red
write_ppm("BezierQ.ppm",img)</syntaxhighlight>
 
=={{header|PicoLisp}}==
This uses the 'brez' line drawing function from
[[Bitmap/Bresenham's line algorithm#PicoLisp]].
<syntaxhighlight lang=PicoLisp"picolisp">(scl 6)
 
(de quadBezier (Img N X1 Y1 X2 Y2 X3 Y3)
Line 1,006 ⟶ 985:
(inc 'Y DY) ) ) ) )</syntaxhighlight>
Test:
<syntaxhighlight lang=PicoLisp"picolisp">(let Img (make (do 200 (link (need 300 0)))) # Create image 300 x 200
(quadBezier Img 12 20 100 300 -80 260 180)
(out "img.pbm" # Write to bitmap file
Line 1,014 ⟶ 993:
 
(call 'display "img.pbm")</syntaxhighlight>
 
=={{header|PureBasic}}==
<syntaxhighlight lang=PureBasic"purebasic">Procedure quad_bezier(img, p1x, p1y, p2x, p2y, p3x, p3y, Color, n_seg)
Protected i
Protected.f T, t1, a, b, c, d
Line 1,052 ⟶ 1,030:
Until event = #PB_Event_CloseWindow
</syntaxhighlight>
 
=={{header|Python}}==
See [[Cubic bezier curves#Python]] for a generalized solution.
 
=={{header|R}}==
See [[Cubic bezier curves#R]] for a generalized solution.
 
=={{header|Racket}}==
<syntaxhighlight lang="racket">
#lang racket
(require racket/draw)
Line 1,088 ⟶ 1,063:
bm
</syntaxhighlight>
 
=={{header|Raku}}==
(formerly Perl 6)
Line 1,094 ⟶ 1,068:
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" line>class Pixel { has UInt ($.R, $.G, $.B) }
class Bitmap {
has UInt ($.width, $.height);
Line 1,195 ⟶ 1,169:
 
See [https://github.com/thundergnat/rc/blob/master/img/Bezier-quadratic-perl6.png example image here], (converted to a .png as .ppm format is not widely supported).
 
=={{header|Ruby}}==
See [[Cubic bezier curves#Ruby]] for a generalized solution.
 
=={{header|Tcl}}==
See [[Cubic bezier curves#Tcl]] for a generalized solution.
 
=={{header|TI-89 BASIC}}==
 
{{TI-image-task}}
 
<syntaxhighlight lang="ti89b">Define cubic(p1,p2,p3,segs) = Prgm
Local i,t,u,prev,pt
0 → pt
Line 1,219 ⟶ 1,190:
EndFor
EndPrgm</syntaxhighlight>
 
=={{header|Vedit macro language}}==
This implementation uses de Casteljau's algorithm to recursively split the Bezier curve into two smaller segments until the segment is short enough to be approximated with a straight line.
Line 1,226 ⟶ 1,196:
Constant recursion depth is used here. Recursion depth of 5 seems to give accurate enough result in most situations. In real world implementations, some adaptive method is often used to decide when to stop recursion.
 
<syntaxhighlight lang="vedit">// Daw a Cubic bezier curve
// #20, #30 = Start point
// #21, #31 = Control point 1
Line 1,259 ⟶ 1,229:
}
return</syntaxhighlight>
 
=={{header|Wren}}==
{{libheader|DOME}}
Requires version 1.3.0 of DOME or later.
<syntaxhighlight lang="ecmascript">import "graphics" for Canvas, ImageData, Color, Point
import "dome" for Window
 
Line 1,338 ⟶ 1,307:
static draw(alpha) {}
}</syntaxhighlight>
 
=={{header|XPL0}}==
[[File:QuadXPL0.png|right]]
<syntaxhighlight lang=XPL0"xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
 
proc Bezier(P0, P1, P2); \Draw quadratic Bezier curve
Line 1,368 ⟶ 1,336:
SetVid(3); \restore normal text display
]</syntaxhighlight>
 
=={{header|zkl}}==
Uses the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl
 
Add this to the PPM class:
<syntaxhighlight lang="zkl"> fcn qBezier(p0x,p0y, p1x,p1y, p2x,p2y, rgb, numPts=500){
numPts.pump(Void,'wrap(t){ // B(t)
t=t.toFloat()/numPts; t1:=(1.0 - t);
Line 1,383 ⟶ 1,350:
}</syntaxhighlight>
Doesn't use line segments, they don't seem like an improvement.
<syntaxhighlight lang="zkl">bitmap:=PPM(200,200,0xff|ff|ff);
bitmap.qBezier(10,100, 250,270, 150,20, 0);
bitmap.write(File("foo.ppm","wb"));</syntaxhighlight>
{{out}}
Same as the BBC BASIC image:[[Image:bezierquad_bbc.gif]]
 
{{omit from|AWK}}
{{omit from|GUISS}}
10,343

edits