|
Post by tenochtitlanuk on Oct 22, 2020 9:52:45 GMT -5
I've posted before and linked to my website- and these tasks are still empty on Rosetta Code. I just realised I had not linked to the further example at link which are related but not on the index of my site...
The Rosetta Code tasks include- ' create a ppm file ' fill an image with a plain RGB color, ' set a given pixel with a color, ' get the color of a pixel. ' Bresenham draw line ' Convert image to grayscale; ' Compute the histogram ' Find the median: defined as the luminance such that the image has an approximately ' equal number of pixels with lesser and greater luminance. ' Replace each pixel of luminance lesser than the median to black, and others to white.
Along with the Bresenham one, which gives a particularly fun real-time display and is also already on my site, and lena, I've essentially covered all these. Fun..
|
|
|
Post by tenochtitlanuk on Oct 27, 2020 16:19:37 GMT -5
Here's the Bezier quadratic curve- two end points and one control point, joining the control points using Bresenham's algorithm. It produces a BMP file and I will add the production of the P6 ppm file and add to my web pages. The colour changes were added just to make it easier to see the segments... nomainwin
' x y dim Px( 100), Py( 100) ' up to 100 line sections..
WindowWidth = 660 WindowHeight = 680
open "display" for graphics_nsb as #wg
#wg "trapclose quit"
#wg "size 2"
for x =0 to 600 step 100 #wg "up ; goto "; x +20; " 620" #wg "down ; goto "; x +20; " 20" #wg "size 1" next x
#wg "size 2" for y =0 to 600 step 100 #wg "up ; goto 20 "; 620 -y #wg "down ; goto 620 "; 620 -y #wg "size 1" next y
#wg "down ; size 2"
call bezierquad 10, 100, 250, 270, 150, 20, 40, 0, 0, 0
#wg "getbmp scr 0 0 660 660" bmpsave "scr", "BezierQuadraticCurve.bmp"
wait ' _________________________________________________________
' P0 P1 P2 pointNum R G B sub bezierquad x1, y1, x2, y2, x3, y3, n, r, g, b
#wg "size 10 ; color red" #wg "set "; 20 +2 *x1; " "; 620 -2 *y1 #wg "set "; 20 +2 *x2; " "; 620 -2 *y2 #wg "set "; 20 +2 *x3; " "; 620 -2 *y3 #wg "size 2"
for i = 0 to n t =i /n t1 =1 -t a =t1^2 b =2 *t *t1 c =t^2 Px( i) =int( a *x1 +b *x2 +c *x3 +0.5) Py( i) =int( a *y1 +b *y2 +c *y3 +0.5) next i
for i =0 to n -1 call bresenham Px( i), Py( i), Px( i +1), Py( i +1), r, g, b R =int( i /( n -1) *255) #wg "color "; R; " "; 80; " "; 255 -R next i
end sub
sub bresenham x1, y1, x2, y2, r, g, b dx =abs( x2 -x1): sx =sgn( x2 -x1) dy =abs( y2 -y1): sy =sgn( y2 -y1) if dx <dy then e =dx /2 else e =dy /2
do call setpixel x1, y1, r, g, b if x1 =x2 then if y1 =y2 then exit do
if dx >dy then x1 =x1 +sx: e =e -dy: if e <0 then e =e +dx: y1 =y1 +sy else y1 =y1 +sy: e =e -dx: if e <0 then e =e +dy: x1 =x1 +sx end if loop until 0
end sub
sub setpixel x, y, r, g, b '#wg "color "; str$( r); " "; str$( g); " "; str$( b) #wg "set "; 20 +x *2; " "; 620 -y *2 end sub
sub quit h$ close #h$ end end sub
function sgn( x) if x>=0 then sgn =1 else sgn =0 -1 end function
|
|
|
Post by tenochtitlanuk on Oct 27, 2020 16:41:10 GMT -5
... and the Bezier Cubic one.. nomainwin
' x y dim Px( 100), Py( 100) ' up to 100 line sections..
WindowWidth = 660 WindowHeight = 680
open "display" for graphics_nsb as #wg
#wg "trapclose quit"
#wg "size 2"
for x =0 to 600 step 100 #wg "up ; goto "; x +20; " 620" #wg "down ; goto "; x +20; " 20" #wg "size 1" next x
#wg "size 2" for y =0 to 600 step 100 #wg "up ; goto 20 "; 620 -y #wg "down ; goto 620 "; 620 -y #wg "size 1" next y
#wg "down ; size 2"
call beziercubic 160,150, 10,120, 30,0, 150,50, 20, 0,0,0
#wg "getbmp scr 0 0 660 660" bmpsave "scr", "BezierCubicCurve.bmp"
wait ' _________________________________________________________
' P0 P1 P2 P3 pointNum R G B sub beziercubic x1, y1, x2, y2, x3, y3, x4, y4, n, r, g, b
#wg "size 10 ; color red" #wg "set "; 20 +2 *x1; " "; 620 -2 *y1 #wg "set "; 20 +2 *x2; " "; 620 -2 *y2 #wg "set "; 20 +2 *x3; " "; 620 -2 *y3 #wg "set "; 20 +2 *x4; " "; 620 -2 *y4 #wg "size 2"
for i = 0 to n t =i /n t1 =1 -t a =t1^3 b =3 *t *t1^2 c =3 *t^2 *t1 d =t^3 Px( i) =int( a *x1 +b *x2 +c *x3 +d *x4 +0.5) Py( i) =int( a *y1 +b *y2 +c *y3 +d *y4 +0.5) next i
for i =0 to n -1 R =int( i /( n -1) *255) #wg "color "; R; " "; 80; " "; 255 -R call bresenham Px( i), Py( i), Px( i +1), Py( i +1), r, g, b 'R =int( i /( n -1) *255) '#wg "color "; R; " "; 80; " "; 255 -R next i
end sub
sub bresenham x1, y1, x2, y2, r, g, b dx =abs( x2 -x1): sx =sgn( x2 -x1) dy =abs( y2 -y1): sy =sgn( y2 -y1) if dx <dy then e =dx /2 else e =dy /2
do call setpixel x1, y1, r, g, b if x1 =x2 then if y1 =y2 then exit do
if dx >dy then x1 =x1 +sx: e =e -dy: if e <0 then e =e +dx: y1 =y1 +sy else y1 =y1 +sy: e =e -dx: if e <0 then e =e +dy: x1 =x1 +sx end if loop until 0
end sub
sub setpixel x, y, r, g, b '#wg "color "; str$( r); " "; str$( g); " "; str$( b) #wg "set "; 20 +x *2; " "; 620 -y *2 end sub
sub quit h$ close #h$ end end sub
function sgn( x) if x>=0 then sgn =1 else sgn =0 -1 end function
|
|