|
Post by tsh73 on Apr 9, 2019 9:04:29 GMT
I cannot understand what you say. Code I posted works for me like this: I draw "0", press Try it fills outside 0 if there is a gaps it fills inside too I press "by array", draw over initial figure several times trying to patch all gaps press Try if fills outside leaving central area.
|
|
|
Post by tsh73 on Apr 9, 2019 9:46:43 GMT
Code which connects points so paint would not spill.
nomainwin global larg, haut haut=200: larg=150 WindowWidth = 305: WindowHeight = 370 UpperLeftX = 300: UpperLeftY = 20 STATICTEXT #w.pp, "N Data", 5, 10, 70, 25 TEXTBOX #w.p, 85, 10, 60, 25 BUTTON #w.new, "New ", [new], UL, 155, 10, 40, 20 BUTTON #w.sto, "Stop ", [stop], UL, 210, 10, 40, 20 BUTTON #w.go, "by array ", [drawback], UL, 10, 40, 80, 20 STATICTEXT #w.tx, "Clic and move for polygon", 10, haut +80, 230, 25 STATICTEXT #w.txa, "And Try", 10, haut +100, 100, 25 BUTTON #w.try, "Try ", [palpe], UL, 100, 40, 80, 25 GRAPHICBOX #w.m 10, 70, larg, haut OPEN "Test" FOR window_nf AS #w #w, "TRAPCLOSE [closetest]" lastX=-1 lastY=-1 [new] #w.p, "" ' p=0: pl=0: x$="": y$="" dim matr(larg+1, haut+1) #w.m "down;fill darkgreen; color white;size 1" #w.m, "when leftButtonMove [keepdraw]" #w.m, "when leftButtonDown [startdraw]" '#w.m, "size 2" 'leaves gaps!!! #w.tx, "!font courrier 14": #w.txa, "!font courrier 14" #w.pp, "!font courrier 12": #w.try, "!font courrier 12 bold" wait
[startdraw] x=MouseX: y= MouseY lastX=x lastY=y wait
[keepdraw] x=MouseX: y= MouseY if x<0 or x>larg or y <0 or y > haut then wait 'or it falls out of matrix dx = x-lastX dy = y-lastY if dx = 0 and dy = 0 then wait 'no move, or breaks with /0 if abs(dx)>abs(dy) then 'go by x x1=lastX:x2=x if x2<x1 then x2=lastX:x1=x for xx=x1 to x2 yy=int(0.5+lastY+(y-lastY)/(x-lastX)*(xx-lastX)) matr(xx,yy)=1 #w.m, "set ";xx;" ";yy 'datas in box sure next else 'go by y y1=lastY:y2=y if y2<y1 then y2=lastY:y1=y for yy=y1 to y2 xx=int(0.5+lastX+(x-lastX)/(y-lastY)*(yy-lastY)) matr(xx,yy)=1 #w.m, "set ";xx;" ";yy 'datas in box sure next end if lastX=x lastY=y
'end if ' matr(x,y)=1 ' #w.m, "set ";x;" ";y 'datas in box sure p=p +1: #w.p, str$(p) wait [drawback] '#w.m "down;fill darkred; color yellow;size 2" #w.m "cls" #w.m "down;fill darkred; color yellow" 'no size 2 for y=1 to haut for x=1 to larg if matr(x,y)= 1 then #w.m, "set ";x;" ";y end if if matr(x,y)= 2 then matr(x,y)= 0 'so we can refill it next x next y wait
sub FloodFill x,y 'simple recursive implementation 'along Wikipedia 'https://en.wikipedia.org/wiki/Flood_fill 'Stack-based recursive implementation (four-way) SCAN 'global larg, haut - w, h if x <0 or x > larg or y <0 or y >haut then exit sub if matr(x,y) <> 0 then exit sub 'else set colored matr(x,y) =2 'fills matr array - breaks it? #w.m, "set ";x;" ";y call FloodFill x+1,y call FloodFill x,y+1 call FloodFill x-1,y call FloodFill x,y-1
end sub '-------------------- [palpe] #w.m, "when mouseMove" #w.m "down;fill darkred" 'recursive flood fill #w.m, "down;color cyan;size 1" call FloodFill 1,1
wait
[pause] timer 2, [temps] wait [temps] timer 0 return [closetest] CLOSE #w END
|
|
|
Post by Rod on Apr 9, 2019 12:35:16 GMT
Here is a different way to follow the mouse and fill in the gaps/array between points. oldx must be set to -1 before each new drawing. Ah, beaten by Anatoly.
nomainwin global larg, haut haut=200: larg=150 WindowWidth = 305: WindowHeight = 370 UpperLeftX = 300: UpperLeftY = 20 STATICTEXT #w.pp, "N Data", 5, 10, 70, 25 TEXTBOX #w.p, 85, 10, 60, 25 BUTTON #w.new, "New ", [new], UL, 155, 10, 40, 20 BUTTON #w.sto, "Stop ", [stop], UL, 210, 10, 40, 20 BUTTON #w.go, "by array ", [drawback], UL, 10, 40, 80, 20 STATICTEXT #w.tx, "Clic and move for polygon", 10, haut +80, 230, 25 STATICTEXT #w.txa, "And Try", 10, haut +100, 100, 25 BUTTON #w.try, "Try ", [palpe], UL, 100, 40, 80, 25 GRAPHICBOX #w.m 10, 70, larg, haut OPEN "Test" FOR window_nf AS #w #w, "TRAPCLOSE [closetest]" [new] #w.p, "" ' p=0: pl=0: x$="": y$="" dim matr(larg+1, haut+1) oldx=-1 #w.m "down;fill darkgreen; color white;size 1" #w.m, "when leftButtonMove [keepdraw]" '#w.m, "size 2" 'leaves gaps!!! wait [keepdraw] x=MouseX: y= MouseY if x<0 or x>larg or y <0 or y > haut then wait xd=(x-oldx) yd=(y-oldy) if abs(xd)>abs(yd)then n=abs(xd) xd=xd/n yd=yd/n else n=abs(yd) xd=xd/n yd=yd/n end if print n,xd,yd xx=oldx yy=oldy for z=1 to n xx=xx+xd yy=yy+yd if oldx<>-1 then matr(xx,yy)=1 #w.m "set ";xx;" ";yy 'print oldx,oldy,x,y,n,xd,yd,xx,yy else matr(x,y)=1 #w.m "set ";x;" ";y end if next oldx=x oldy=y
wait [drawback] '#w.m "down;fill darkred; color yellow;size 2" #w.m "cls" #w.m "down;fill darkred; color yellow" 'no size 2 for y=1 to haut for x=1 to larg if matr(x,y)= 1 then #w.m, "set ";x;" ";y end if if matr(x,y)= 2 then matr(x,y)= 0 'so we can refill it next x next y wait
sub FloodFill x,y 'simple recursive implementation 'along Wikipedia 'https://en.wikipedia.org/wiki/Flood_fill 'Stack-based recursive implementation (four-way) SCAN 'global larg, haut - w, h if x <0 or x > larg or y <0 or y >haut then exit sub if matr(x,y) <> 0 then exit sub 'else set colored matr(x,y) =2 'fills matr array - breaks it? #w.m, "set ";x;" ";y call FloodFill x+1,y call FloodFill x,y+1 call FloodFill x-1,y call FloodFill x,y-1
end sub '-------------------- [palpe] #w.m, "when mouseMove" #w.m "down;fill darkred" 'recursive flood fill #w.m, "down;color cyan;size 1" call FloodFill 1,1
wait
[pause] timer 2, [temps] wait [temps] timer 0 return [closetest] CLOSE #w END
|
|
|
Post by honkytonk on Apr 10, 2019 17:40:34 GMT
it works ! congratulations for tsh73 and Rod. I will be able to continue my sprite factory. Thank you so much.
|
|
|
Post by Rod on Apr 10, 2019 17:51:36 GMT
Oh, if it is sprite masking you might be better off searching out Anatolys getpixelcolor alternative for Just BASIC. It reads a .bmp structure and allows pixel perfect drawing and masking. Combine that with some of the rotation code that has been published and you can programmatically rotate and mask sprite images.
My above routine is too clumsy for sprite drawing.
|
|
|
Post by honkytonk on Apr 11, 2019 6:35:27 GMT
Oh, if it is sprite masking you might be better off searching out Anatolys getpixelcolor alternative for Just BASIC. It reads a .bmp structure and allows pixel perfect drawing and masking. Combine that with some of the rotation code that has been published and you can programmatically rotate and mask sprite images. My above routine is too clumsy for sprite drawing. Can we put the instantaneous values of x and y in the matr() array of the polygon? And test only by <> 0; to keep the image.bmp portion (piece) ? What am I trying to do. As you know, a lot of things have been cut off from the old forum, and I'm desperate to find that wonder you're talking about. I'm going to take her.
|
|
|
Post by Rod on Apr 11, 2019 7:46:41 GMT
Anatoly has posted Just BASIC code that reads the .bmp array directly. This allows you to read and replace pixel info. I will search for the link. jbfilesarchive.com/phpBB3/download/file.php?id=682 Perhaps Anatoly has a simpler example? The following code is for Liberty BASIC but you could replace the API code with Anatoly's .bmp code abd produce the same result. Or move up to Liberty BASIC. This code works best on small sprite images. It creates a sequence of rotated and masked sprites. nomainwin WindowWidth=800 WindowHeight=600 UpperLeftX=(DisplayWidth-WindowWidth)/2 UpperLeftY=(DisplayHeight-WindowHeight)/2 menu #1, "&File", "&Open Sprite",[openSprite],_ "&Save As...",[saveAs],|,"E&xit",[quit] open "Rotate and Mask Sprite" for graphics_nsb as #1 #1 "down ; trapclose [quit]"
hWindow=hwnd(#1) CallDll #user32, "GetDC",_ hWindow as ulong,_ hDC as ulong wait
[quit] CallDll #user32, "ReleaseDC",_ hWindow as ulong,_ hDC as ulong,_ r as long close #1 end
[openSprite] if hBitmap<>0 then unloadbmp ("bm") #1 "cls" end if
filedialog "Open Sprite","*.bmp",bitmap$ if bitmap$="" then wait loadbmp "bm" , bitmap$ hBitmap=hbmp("bm") bmpheight=HeightBitmap(bitmap$) bmpwidth=WidthBitmap(bitmap$) midx=int(bmpwidth/2) midy=int(bmpheight/2)+bmpheight radi=int(bmpwidth/2) #1 "fill black" #1 "drawbmp bm 0 ";bmpheight for angle= 30 to 330 step 30 newx=newx+bmpwidth call rotate angle,newx,midx,midy,radi, hWindow, hDC next call MakeMask bmpwidth, bmpheight, hWindow, hDC wait
[saveAs] #1 "getbmp SpriteMask 0 0 ";bmpwidth;" ";2*bmpheight filedialog "Save As... ","*.bmp",savefile$ if savefile$="" then wait newx=0 for a=0 to 330 step 30 #1 "getbmp SpriteMask ";newx;" 0 ";bmpwidth;" ";2*bmpheight bmpsave "SpriteMask",left$(savefile$,len(savefile$)-4)+str$(a)+".bas" newx=newx+bmpwidth next wait
'************FUNCTIONS****************** function WidthBitmap(name$) open name$ for input as #pic pic$=input$(#pic,29) close #pic WidthBitmap = asc(mid$(pic$,19,1)) + _ (asc(mid$(pic$,20,1)) * 256) end function
function HeightBitmap(name$)
open name$ for input as #pic pic$=input$(#pic,29) close #pic HeightBitmap = asc(mid$(pic$,23,1)) + _ (asc(mid$(pic$,24,1)) * 256) end function
sub rotate angle, newx, midx, midy, radi, hWindow, hDC for d = 0 to radi for n= 1 to 360 step .5 scan x=int(midx-(d*sin(n/57.29577951))) y=int(midy-(d*cos(n/57.29577951))) CallDll #gdi32, "GetPixel",_ hDC as ulong,_ x as long,_ y as long,_ pColor as long a=n+angle mod 360 x=int(midx-(d*sin(a/57.29577951))) y=int(midy-(d*cos(a/57.29577951))) x=x+newx CallDll #gdi32, "SetPixel",_ hDC as ulong,_ x as long,_ y as long,_ pColor as long,_ r as long next n next d end sub
sub MakeMask wide, high, hWnd,hDC white=(255*256*256)+(255*256)+255 black=0 for x = 0 to 12*wide-1 for y = high to 2*high-1 CallDll #gdi32, "GetPixel",_ hDC as ulong,_ x as long,_ y as long,_ pColor as long if pColor=black then newColor=white else newColor=black end if j=y-high CallDll #gdi32, "SetPixel",_ hDC as ulong,_ x as long, _ j as long, _ newColor as long, _ r as long next y next x end sub
|
|
|
Post by tsh73 on Apr 11, 2019 8:57:47 GMT
|
|
|
Post by honkytonk on Apr 11, 2019 15:38:23 GMT
I looked at your proposals, and I still have this question: If we guarantee the continuity of the polygon's trace, and inscribe the polygon in the total matrix It seems logical that by testing the "x" line by line the first encounter of the trace is necessarily an entry into the polygon, the next can only be an exit; if we add a procedure for the horizontal parts of the trace, why can not we manage with state witnesses (flags) (in-out)?
|
|
|
Post by Rod on Apr 11, 2019 16:07:28 GMT
Perhaps we are at cross purposes. You speak of sprites. These are multi colored small images that are pixel perfect. Any masking needs to be pixel by pixel. So guaranteed continuity of a line is at odds with sprites. So perhaps you need to better explain what you mean by sprites. Line drawn sprites to me would be clunky.
|
|
|
Post by honkytonk on Apr 11, 2019 17:43:18 GMT
Perhaps we are at cross purposes. You speak of sprites. These are multi colored small images that are pixel perfect. Any masking needs to be pixel by pixel. So guaranteed continuity of a line is at odds with sprites. So perhaps you need to better explain what you mean by sprites. Line drawn sprites to me would be clunky. Oh ok, this requires making the line more fleshy. Thank you ! But pixels are on the line, one ligne, and the pixel, there is or there is not A sprite is a polygone 2D Image on black-back whith their masq duplicate in black on white
|
|
|
Post by Rod on Apr 11, 2019 18:11:17 GMT
Look at my avatar, the macaroni man at bottom left is a sprite. Not one part of that, or indeed any of my avatar could be replicated with line drawing aside from the rays in the background. But they are drawn over.
Show us one of your sprites. If they are single color shape they might be better drawn using the shape tools.
|
|
|
Post by tsh73 on Apr 11, 2019 19:15:15 GMT
You gave it a try, didn't you? Probably it's not as easy as it seems See line 3 (horisontal 11111 one) In first picture, after line of 1's we still in the same area But in second - in another area
000000000000000000 000000000000000000 000001111111100000 000111000000111000 000001100011000000 000000011100000000
000000000000100000 000000000001110000 000001111111100000 000111000000000000 000001111111000000 000000000000100000
|
|
|
Post by honkytonk on Apr 12, 2019 7:35:37 GMT
@: tsh73: If we keep the last turn (up or down) And we test next turn We know if we are in or out, no ? I work it.
|
|
|
Post by honkytonk on Apr 12, 2019 15:39:33 GMT
@: tsh73: Thanks to your algorithm and its clever: "2", I managed to make a sprite and its mask, adding that at the end of [palpe]. Thank you very much.
'fill white for mask for y=1 to haut/2 for x=1 to larg #w.m, "down;color white;size 1" #w.m, "set ";x;" ";y next x next y 'draw mask for y=haut/2 to haut for x=1 to larg #w.m, "down;color black;size 1" if matr(x,y) <> 2 then #w.m, "set ";x;" ";y-100 end if next x next y wait
|
|