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

m
Fixed lang tags.
m (Fixed lang tags.)
Line 6:
 
=={{header|Ada}}==
<lang ada>procedure Cubic_Bezier
procedure Cubic_Bezier
( Picture : in out Image;
P1, P2, P3, P4 : Point;
Line 30 ⟶ 29:
Line (Picture, Points (I), Points (I + 1), Color);
end loop;
end Cubic_Bezier;</lang>
</lang>
The following test
<lang ada> X : Image (1..16, 1..16);
X : Image (1..16, 1..16);
begin
Fill (X, White);
Cubic_Bezier (X, (16, 1), (1, 4), (3, 16), (15, 11), Black);
Print (X);</lang>
</lang>
should produce output:
<pre>
Line 65 ⟶ 61:
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}}
<!-- {{does not work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386 - '''pragmat''' '''read''' is not part of algol68rs}} -->
<lang algolalgol68>PRAGMAT READ "Bresenhams_line_algorithm.a68" PRAGMAT;
 
cubic bezier OF class image :=
Line 192 ⟶ 188:
This subroutine should go inside the <code>RCImagePrimitive</code> module (see [[Bresenham's line algorithm]])
 
<lang fortran> subroutine cubic_bezier(img, p1, p2, p3, p4, color)
type(rgbimage), intent(inout) :: img
type(point), intent(in) :: p1, p2, p3, p4
type(rgb), intent(in) :: color
 
integer :: i, j
real :: pts(0:N_SEG,0:1), t, a, b, c, d, x, y
 
do i = 0, N_SEG
t = real(i) / real(N_SEG)
a = (1.0 - t)**3.0
b = 3.0 * t * (1.0 - t)**2.0
c = 3.0 * (1.0 - t) * t**2.0
d = t**3.0
x = a * p1%x + b * p2%x + c * p3%x + d * p4%x
y = a * p1%y + b * p2%y + c * p3%y + d * p4%y
pts(i,0) = x
pts(i,1) = y
end do
 
do i = 0, N_SEG-1
j = i + 1
call draw_line(img, point(pts(i,0), pts(i,1)), &
point(pts(j,0), pts(j,1)), color)
end do
 
end subroutine cubic_bezier</lang>
 
=={{header|J}}==
Line 247 ⟶ 243:
 
'''Example usage:'''
<lang j> myimg=: 0 0 255 makeRGB 300 300
]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 NB. draw control points
viewRGB (randomctrlpts; 255 255 0) drawBezier myimg NB. display image with bezier line</lang>
 
=={{header|OCaml}}==
Line 358 ⟶ 354:
 
=={{header|R}}==
<lang R># x, y: the x and y coordinates of the hull points
<lang R>
# x, y: the x and y coordinates of the hull points
# n: the number of points in the curve.
bezierCurve <- function(x, y, n=10)
Line 397 ⟶ 392:
y <- 1:6
plot(x, y, "o", pch=20)
points(bezierCurve(x,y,20), type="l", col="red")</lang>
</lang>
 
=={{header|Ruby}}==
Line 515 ⟶ 509:
{{TI-image-task}}
 
<prelang style="font-family:'TI Uni'"ti89b>Define cubic(p1,p2,p3,p4,segs) = Prgm
Local i,t,u,prev,pt
0 → pt
Line 527 ⟶ 521:
EndIf
EndFor
EndPrgm</prelang>
Anonymous user