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

Content added Content deleted
m (Fixed lang tags.)
Line 6: Line 6:


=={{header|Ada}}==
=={{header|Ada}}==
<lang ada>
<lang ada>procedure Cubic_Bezier
procedure Cubic_Bezier
( Picture : in out Image;
( Picture : in out Image;
P1, P2, P3, P4 : Point;
P1, P2, P3, P4 : Point;
Line 30: Line 29:
Line (Picture, Points (I), Points (I + 1), Color);
Line (Picture, Points (I), Points (I + 1), Color);
end loop;
end loop;
end Cubic_Bezier;
end Cubic_Bezier;</lang>
</lang>
The following test
The following test
<lang ada>
<lang ada> X : Image (1..16, 1..16);
X : Image (1..16, 1..16);
begin
begin
Fill (X, White);
Fill (X, White);
Cubic_Bezier (X, (16, 1), (1, 4), (3, 16), (15, 11), Black);
Cubic_Bezier (X, (16, 1), (1, 4), (3, 16), (15, 11), Black);
Print (X);
Print (X);</lang>
</lang>
should produce output:
should produce output:
<pre>
<pre>
Line 65: Line 61:
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}}
{{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}} -->
<!-- {{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 algol>PRAGMAT READ "Bresenhams_line_algorithm.a68" PRAGMAT;
<lang algol68>PRAGMAT READ "Bresenhams_line_algorithm.a68" PRAGMAT;


cubic bezier OF class image :=
cubic bezier OF class image :=
Line 192: Line 188:
This subroutine should go inside the <code>RCImagePrimitive</code> module (see [[Bresenham's line algorithm]])
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)
<lang fortran>subroutine cubic_bezier(img, p1, p2, p3, p4, color)
type(rgbimage), intent(inout) :: img
type(rgbimage), intent(inout) :: img
type(point), intent(in) :: p1, p2, p3, p4
type(point), intent(in) :: p1, p2, p3, p4
type(rgb), intent(in) :: color
type(rgb), intent(in) :: color


integer :: i, j
integer :: i, j
real :: pts(0:N_SEG,0:1), t, a, b, c, d, x, y
real :: pts(0:N_SEG,0:1), t, a, b, c, d, x, y


do i = 0, N_SEG
do i = 0, N_SEG
t = real(i) / real(N_SEG)
t = real(i) / real(N_SEG)
a = (1.0 - t)**3.0
a = (1.0 - t)**3.0
b = 3.0 * t * (1.0 - t)**2.0
b = 3.0 * t * (1.0 - t)**2.0
c = 3.0 * (1.0 - t) * t**2.0
c = 3.0 * (1.0 - t) * t**2.0
d = t**3.0
d = t**3.0
x = a * p1%x + b * p2%x + c * p3%x + d * p4%x
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
y = a * p1%y + b * p2%y + c * p3%y + d * p4%y
pts(i,0) = x
pts(i,0) = x
pts(i,1) = y
pts(i,1) = y
end do
end do


do i = 0, N_SEG-1
do i = 0, N_SEG-1
j = i + 1
j = i + 1
call draw_line(img, point(pts(i,0), pts(i,1)), &
call draw_line(img, point(pts(i,0), pts(i,1)), &
point(pts(j,0), pts(j,1)), color)
point(pts(j,0), pts(j,1)), color)
end do
end do


end subroutine cubic_bezier</lang>
end subroutine cubic_bezier</lang>


=={{header|J}}==
=={{header|J}}==
Line 247: Line 243:


'''Example usage:'''
'''Example usage:'''
<lang j> myimg=: 0 0 255 makeRGB 300 300
<lang j>myimg=: 0 0 255 makeRGB 300 300
]randomctrlpts=: ,3 2 ?@$ }:$ myimg NB. 3 control points - quadratic
]randomctrlpts=: ,3 2 ?@$ }:$ myimg NB. 3 control points - quadratic
]randomctrlpts=: ,4 2 ?@$ }:$ myimg NB. 4 control points - cubic
]randomctrlpts=: ,4 2 ?@$ }:$ myimg NB. 4 control points - cubic
myimg=: ((2 ,.~ _2]\randomctrlpts);255 0 255) drawCircles myimg NB. draw control points
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>
viewRGB (randomctrlpts; 255 255 0) drawBezier myimg NB. display image with bezier line</lang>


=={{header|OCaml}}==
=={{header|OCaml}}==
Line 358: Line 354:


=={{header|R}}==
=={{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.
# n: the number of points in the curve.
bezierCurve <- function(x, y, n=10)
bezierCurve <- function(x, y, n=10)
Line 397: Line 392:
y <- 1:6
y <- 1:6
plot(x, y, "o", pch=20)
plot(x, y, "o", pch=20)
points(bezierCurve(x,y,20), type="l", col="red")
points(bezierCurve(x,y,20), type="l", col="red")</lang>
</lang>


=={{header|Ruby}}==
=={{header|Ruby}}==
Line 515: Line 509:
{{TI-image-task}}
{{TI-image-task}}


<pre style="font-family:'TI Uni'">Define cubic(p1,p2,p3,p4,segs) = Prgm
<lang ti89b>Define cubic(p1,p2,p3,p4,segs) = Prgm
Local i,t,u,prev,pt
Local i,t,u,prev,pt
0 → pt
0 → pt
Line 527: Line 521:
EndIf
EndIf
EndFor
EndFor
EndPrgm</pre>
EndPrgm</lang>