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

Add J
(add Ruby)
(Add J)
Line 219:
 
end subroutine cubic_bezier</lang>
 
=={{header|J}}==
'''Solution:'''
<lang j>require 'numeric'
 
bik=: 2 : '((*&(u!v))@(^&u * ^&(v-u)@-.))'
basiscoeffs=: <: 4 : 'x bik y t. i.>:y'"0~ i.
linearcomb=: basiscoeffs@#@[
evalBernstein=: ([ +/ .* linearcomb) p. ] NB. evaluate Bernstein Polynomial (general)
 
NB.*getBezierPoints v Returns points for bezier curve given control points (y)
NB. eg: [xincrement] getBezierPoints controlpoints
NB. y is: y0 x0, y1 x1, y2 x2 ...
getBezierPoints=: verb define
2 getBezierPoints y
:
ctrlpts=. (/: {:"1) _2]\ y NB. sort ctrlpts for increasing x
xvals=. ({: ,~ {. + +:@:i.@<.@-:@-~/) ({:"1) 0 _1{ctrlpts
tvals=. ((] - {.) % ({: - {.)) xvals
xvals ,.~ ({."1 ctrlpts) evalBernstein tvals
)
 
NB.*drawBezier v Draws bezier curve defined by (x) on image (y)
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 ]</lang>
 
'''Example usage:'''
<lang j> myimg=: 0 0 255 makeRGB 800 1200
]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
viewRGB (randomctrlpts; 255 255 0) drawBezier myimg</lang>
 
=={{header|OCaml}}==
892

edits