Bitmap/Bézier curves/Quadratic: Difference between revisions
Content added Content deleted
(Added BBC BASIC) |
(Added solution for MATLAB) |
||
Line 338: | Line 338: | ||
Graphics[{BSplineCurve[pts], Green, Line[pts], Red, Point[pts]}]</lang> |
Graphics[{BSplineCurve[pts], Green, Line[pts], Red, Point[pts]}]</lang> |
||
[[File:MmaQuadraticBezier.png]] |
[[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]]. |
|||
<lang MATLAB> |
|||
function bezierQuad(obj,pixel_0,pixel_1,pixel_2,color,varargin) |
|||
if( isempty(varargin) ) |
|||
resolution = 20; |
|||
else |
|||
resolution = varargin{1}; |
|||
end |
|||
%Calculate time axis |
|||
time = (0:1/resolution:1)'; |
|||
timeMinus = 1-time; |
|||
%The formula for the curve is expanded for clarity, the lack of |
|||
%loops is because its calculation has been vectorized |
|||
curve = (timeMinus.^2)*pixel_0; %First term of polynomial |
|||
curve = curve + (2.*time.*timeMinus)*pixel_1; %second term of polynomial |
|||
curve = curve + (time.^2)*pixel_2; %third term of polynomial |
|||
curve = round(curve); %round each of the points to the nearest integer |
|||
%connect each of the points in the curve with a line using the |
|||
%Bresenham Line algorithm |
|||
for i = (1:length(curve)-1) |
|||
obj.bresenhamLine(curve(i,:),curve(i+1,:),color); |
|||
end |
|||
assignin('caller',inputname(1),obj); %saves the changes to the object |
|||
end |
|||
</lang> |
|||
Sample usage: |
|||
This will generate the image example for the Go solution. |
|||
<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) |
|||
</lang> |
|||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||