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

m
syntax highlighting fixup automation
(→‎{{header|Lua}}: added Lua solution)
m (syntax highlighting fixup automation)
Line 8:
{{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 119:
DO UNTIL CH#$FF OD
CH=$FF
RETURN</langsyntaxhighlight>
{{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}}==
<langsyntaxhighlight lang=ada>procedure Quadratic_Bezier
( Picture : in out Image;
P1, P2, P3 : Point;
Line 146:
Line (Picture, Points (I), Points (I + 1), Color);
end loop;
end Quadratic_Bezier;</langsyntaxhighlight>
The following test
<langsyntaxhighlight lang=ada> X : Image (1..16, 1..16);
begin
Fill (X, White);
Quadratic_Bezier (X, (8, 2), (13, 8), (2, 15), Black);
Print (X);</langsyntaxhighlight>
should produce;
<pre>
Line 176:
{{works with|BBC BASIC for Windows}}
[[Image:bezierquad_bbc.gif|right]]
<langsyntaxhighlight lang=bbcbasic> Width% = 200
Height% = 200
Line 226:
GCOL 1
LINE x%*2,y%*2,x%*2,y%*2
ENDPROC</langsyntaxhighlight>
 
=={{header|C}}==
Line 232:
Interface (to be added to all other to make the final <tt>imglib.h</tt>):
 
<langsyntaxhighlight lang=c>void quad_bezier(
image img,
unsigned int x1, unsigned int y1,
Line 239:
color_component r,
color_component g,
color_component b );</langsyntaxhighlight>
 
Implementation:
 
<langsyntaxhighlight lang=c>#include <math.h>
 
/* number of segments for the curve */
Line 292:
}
#undef plot
#undef line</langsyntaxhighlight>
 
=={{header|Commodore Basic}}==
<langsyntaxhighlight lang=basic>
10 rem bezier curve algorihm
20 rem translated from purebasic
Line 352:
3160 next i
3170 return
</syntaxhighlight>
</lang>
[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.
<langsyntaxhighlight lang=d>import grayscale_image, bitmap_bresenhams_line_algorithm;
 
struct Pt { int x, y; } // Signed.
Line 385:
im.quadraticBezier(Pt(1,10), Pt(25,27), Pt(15,2), Gray.black);
im.textualShow();
}</langsyntaxhighlight>
{{out}}
<pre>....................
Line 411:
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
<langsyntaxhighlight lang=factor>USING: arrays kernel locals math math.functions
rosettacode.raster.storage sequences ;
IN: rosettacode.raster.line
Line 434:
points-to-lines
{R,G,B} swap image draw-lines ;
</syntaxhighlight>
</lang>
 
=={{header|FBSL}}==
Line 440:
 
'''Translation of BBC BASIC using pure FBSL's built-in graphics functions:'''
<langsyntaxhighlight lang=qbasic>#DEFINE WM_LBUTTONDOWN 513
#DEFINE WM_CLOSE 16
 
Line 495:
WEND
END SUB
END SUB</langsyntaxhighlight>
'''Output:''' [[File:FBSLBezierQuad.PNG]]
 
Line 503:
(This subroutine must be inside the <code>RCImagePrimitive</code> module, see [[Bresenham's line algorithm#Fortran|here]])
 
<langsyntaxhighlight lang=fortran>subroutine quad_bezier(img, p1, p2, p3, color)
type(rgbimage), intent(inout) :: img
type(point), intent(in) :: p1, p2, p3
Line 528:
end do
 
end subroutine quad_bezier</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
{{trans|BBC BASIC}}
<langsyntaxhighlight lang=freebasic>' version 01-11-2016
' compile with: fbc -s console
 
Line 594:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
 
=={{header|Go}}==
{{trans|C}}
<langsyntaxhighlight lang=go>package raster
 
const b2Seg = 20
Line 624:
func (b *Bitmap) Bézier2Rgb(x1, y1, x2, y2, x3, y3 int, c Rgb) {
b.Bézier2(x1, y1, x2, y2, x3, y3, c.Pixel())
}</langsyntaxhighlight>
Demonstration program:
[[File:GoBez2.png|thumb|right]]
<langsyntaxhighlight lang=go>package main
 
import (
Line 641:
fmt.Println(err)
}
}</langsyntaxhighlight>
 
=={{header|Haskell}}==
<langsyntaxhighlight lang=haskell>{-# LANGUAGE
FlexibleInstances, TypeSynonymInstances,
ViewPatterns #-}
Line 690:
pt n p = pmap (*n) p
f (curvePoint -> p1) (curvePoint -> p2) =
line i (toPixel p1) (toPixel p2) c</langsyntaxhighlight>
 
=={{header|J}}==
Line 700:
=={{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 774:
ImageIO.write(image, "jpg", qbFile)
}
}</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.quadraticbezier = function(self, x1, y1, x2, y2, x3, y3, nseg)
nseg = nseg or 10
local prevx, prevy, currx, curry
Line 795:
bitmap:clear()
bitmap:quadraticbezier( 1,1, 30,37, 59,1 )
bitmap:render({[0x000000]='.', [0xFFFFFFFF]='X'})</langsyntaxhighlight>
{{out}}
<pre>.............................................................
Line 820:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight lang=Mathematica>pts = {{0, 0}, {1, -1}, {2, 1}};
Graphics[{BSplineCurve[pts], Green, Line[pts], Red, Point[pts]}]</langsyntaxhighlight>
Second solution using built-in function BezierCurve.
<langsyntaxhighlight lang=Mathematica>pts = {{0, 0}, {1, -1}, {2, 1}};
Graphics[{BezierCurve[pts], Green, Line[pts], Red, Point[pts]}]</langsyntaxhighlight>
[[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]].
<langsyntaxhighlight lang=MATLAB>
function bezierQuad(obj,pixel_0,pixel_1,pixel_2,color,varargin)
 
Line 859:
end
</syntaxhighlight>
</lang>
 
Sample usage:
This will generate the image example for the Go solution.
<langsyntaxhighlight lang=MATLAB>
>> img = Bitmap(400,300);
>> img.fill([223 255 239]);
>> img.bezierQuad([20 150],[500 -100],[300 280],[63 143 239],21);
>> 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 900:
img.fill(White)
img.drawQuadraticBezier((1, 7), (7, 12), (14, 1), Black)
img.print</langsyntaxhighlight>
 
{{out}}
Line 918:
=={{header|OCaml}}==
 
<langsyntaxhighlight lang=ocaml>let quad_bezier ~img ~color
~p1:(_x1, _y1)
~p2:(_x2, _y2)
Line 956:
in
by_pair pts (fun p0 p1 -> line ~p0 ~p1);
;;</langsyntaxhighlight>
 
=={{header|Phix}}==
Line 962:
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
<langsyntaxhighlight lang=Phix>-- demo\rosetta\Bitmap_BezierQuadratic.exw
include ppm.e -- black, green, red, white, new_image(), write_ppm(), bresLine() -- (covers above requirements)
 
Line 989:
img[100][200] = red
img[200][1] = red
write_ppm("BezierQ.ppm",img)</langsyntaxhighlight>
 
=={{header|PicoLisp}}==
This uses the 'brez' line drawing function from
[[Bitmap/Bresenham's line algorithm#PicoLisp]].
<langsyntaxhighlight lang=PicoLisp>(scl 6)
 
(de quadBezier (Img N X1 Y1 X2 Y2 X3 Y3)
Line 1,004:
(setq DY (- (+ (*/ A Y1 1.0) (*/ B Y2 1.0) (*/ C Y3 1.0)) 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
(quadBezier Img 12 20 100 300 -80 260 180)
(out "img.pbm" # Write to bitmap file
Line 1,013:
(mapc prinl Img) ) )
 
(call 'display "img.pbm")</langsyntaxhighlight>
 
=={{header|PureBasic}}==
<langsyntaxhighlight lang=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,051:
event = WaitWindowEvent()
Until event = #PB_Event_CloseWindow
</syntaxhighlight>
</lang>
 
=={{header|Python}}==
Line 1,060:
 
=={{header|Racket}}==
<langsyntaxhighlight lang=racket>
#lang racket
(require racket/draw)
Line 1,087:
(draw-lines dc (bezier-points '(16 1) '(1 4) '(3 16)))
bm
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 1,094:
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,192:
@points.map: { $b.dot( $_, color(255,0,0), 3 )}
 
$*OUT.write: $b.P6;</langsyntaxhighlight>
 
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).
Line 1,206:
{{TI-image-task}}
 
<langsyntaxhighlight lang=ti89b>Define cubic(p1,p2,p3,segs) = Prgm
Local i,t,u,prev,pt
0 → pt
Line 1,218:
EndIf
EndFor
EndPrgm</langsyntaxhighlight>
 
=={{header|Vedit macro language}}==
Line 1,226:
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.
 
<langsyntaxhighlight lang=vedit>// Daw a Cubic bezier curve
// #20, #30 = Start point
// #21, #31 = Control point 1
Line 1,258:
Call("DRAW_LINE")
}
return</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,337:
 
static draw(alpha) {}
}</langsyntaxhighlight>
 
=={{header|XPL0}}==
[[File:QuadXPL0.png|right]]
<langsyntaxhighlight lang=XPL0>include c:\cxpl\codes; \intrinsic 'code' declarations
 
proc Bezier(P0, P1, P2); \Draw quadratic Bezier curve
Line 1,367:
if ChIn(1) then []; \wait for keystroke
SetVid(3); \restore normal text display
]</langsyntaxhighlight>
 
=={{header|zkl}}==
Line 1,373:
 
Add this to the PPM class:
<langsyntaxhighlight 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,381:
__sSet(rgb,x,y);
});
}</langsyntaxhighlight>
Doesn't use line segments, they don't seem like an improvement.
<langsyntaxhighlight lang=zkl>bitmap:=PPM(200,200,0xff|ff|ff);
bitmap.qBezier(10,100, 250,270, 150,20, 0);
bitmap.write(File("foo.ppm","wb"));</langsyntaxhighlight>
{{out}}
Same as the BBC BASIC image:[[Image:bezierquad_bbc.gif]]
10,343

edits