|
Post by Rod on Apr 5, 2020 8:57:17 GMT
Does anyone have code for drawing filled polygons? Andy has code in the archives where we can find if a point is in a polygon but that would be longwinded compared to just drawing the polygon.
Say a triangle
x1=350:y1=350 x2=500:y2=350 x3=500:y3=100
|
|
|
Post by tsh73 on Apr 5, 2020 11:55:31 GMT
Search on my HD got me this
nomainwin open "filledTriangle" for graphics_nsb as #main #main, "down; color green" call filledTriangle "#main", 10, 10, 5, 70, 34, 16, 1 #main, "color blue" call filledTriangle "#main", 10, 110, 5, 170, 34, 116, 0.05 #main, "color red" call filledTriangle "#main", 10, 210, 5, 270, 34, 216, 2 #main, "color darkgreen" call filledTriangle "#main", 100, 10, 70, 110, 130, 110, 1 'call filledTriangle "#main", 200, 100, 300, 70, 300, 130, 1 'This doesn't work: last two points must not have the same x position #main, "trapclose [quit]" wait
[quit] close #main end
sub filledTriangle handle$, X0, Y0, X1, Y1, X2, Y2, density a = (Y1 - Y2) / (X1 - X2) b = Y1 - a * X1 if X1 < X2 then itX = X1 : endX = X2 else itX = X2 : endX = X1 while itX <= endX #handle$, "line "; X0; " "; Y0; " "; int(itX); " "; int(a*itX + b) itX = itX + density wend end sub
I remember seeing demo there poligin is divided into trangles and triangles fast filled I even used triangle fast fill on my torch demo but I cannot find any of these yet
|
|
|
Post by tsh73 on Apr 5, 2020 11:58:17 GMT
in one of other my programs
===================================================== ' Title: Filled Triangle Sub 'Programmer: JB port by Andy Amaya ' Date: 2010.10.27 'http://justbasic.conforums.com/index.cgi?board=shared&action=display&num=1307312447 Sub fillTriangle h$,x1, y1, x2, y2, x3, y3 'triangle coordinates must be ordered: where x1 < x2 < x3 If x2 < x1 Then x = x2 : y = y2 : x2 = x1 : y2 = y1 : x1 = x : y1 = y 'swap x1, y1, with x3, y3 If x3 < x1 Then x = x3 : y = y3 : x3 = x1 : y3 = y1 : x1 = x : y1 = y 'swap x2, y2 with x3, y3 If x3 < x2 Then x = x3 : y = y3 : x3 = x2 : y3 = y2 : x2 = x : y2 = y If x1 <> x3 Then slope1 = (y3-y1)/(x3-x1) 'draw the first half of the triangle length = x2 - x1 If length <> 0 Then slope2 = (y2-y1)/(x2-x1) For x = 0 To length #h$ "Line ";Int(x+x1);" ";Int(x*slope1+y1);" ";Int(x+x1);" ";Int(x*slope2+y1) Next End If 'draw the second half of the triangle y = length*slope1+y1 : length = x3-x2 If length <> 0 Then slope3 = (y3-y2)/(x3-x2) For x = 0 To length #h$ "Line ";Int(x+x2);" ";Int(x*slope1+y);" ";Int(x+x2);" ";Int(x*slope3+y2) Next End If End Sub
|
|
|
Post by B+ on Apr 5, 2020 18:29:44 GMT
I found your lovely torch in my files:
'triangle torch 'tsh73 May 2017 nomainwin
UpperLeftX=1 UpperLeftY=1
open "flame" for graphics_nsb_nf as #gr print #gr, "trapclose [quit]" print #gr, "home ; down ; posxy cx cy" print #gr, "trapclose [quit]" width = 2*cx : height = 2*cy cxx=cx print #gr, "when leftButtonUp [quit]"
'torch handle #gr "fill black" r=30 for x=0-r to r y=sqr(r^2-x^2)/5 c=((1-(x/r)^2)*150+50)/1.5 #gr "color ";c;" ";c;" ";c #gr "place ";cx+x/1.5;" ";cy+y #gr "goto ";cx+x/6;" ";cy+y+130 next
for x=0-r to r y=sqr(r^2-x^2)/5 c=(1-(x/r)^2)*150+50 #gr "color ";c;" ";c;" ";c #gr "place ";cx+x;" ";cy+y #gr "go -30"
next #gr "getbmp torch 0 0 ";width;" ";height
timer 100, [nxt] while 1 scan #gr "discard" '#gr "fill black" #gr "drawbmp torch 0 0" for i = 1 to 7 cx = cxx+randR(-7,7) x1=cx-randR(0,20) y1=cy-randR(50,100) x2=cx+randR(0,20) 'y2=cy-randR(0,100) y2=(cy+y1)/2 if rnd(0)<.5 then tmp=y1:y1=y2:y2=tmp end if #gr "color ";randR(128,255);" ";randR(0,128);" 64" #gr "place ";cx;" ";cy #gr "goto ";x1;" ";y1 #gr "goto ";x2;" ";y2 #gr "goto ";cx;" ";cy for x=x1 to x2 step .5 y = y1+(y2-y1)*(x-x1)/(x2-x1) #gr "line ";cx;" ";cy;" ";x;" ";y next next wait [nxt] wend
wait
[quit] timer 0 close #gr end
function randR(min, max) randR=min+(max-min)*rnd(0) end function
|
|
|
Post by B+ on Apr 5, 2020 19:52:53 GMT
Here is alternate to Andy's:
SUB filledTriangle handle$, X0, Y0, X1, Y1, X2, Y2 if X1 <> X2 then a = (Y1 - Y2) / (X1 - X2) b = Y1 - a * X1 IF X1 < X2 THEN itX = X1: endX = X2 ELSE itX = X2: endX = X1 WHILE itX <= endX #handle$ "line "; X0; " "; Y0; " "; int(itX); " "; int(a*itX + b) itX = itX + .25 WEND end if if X1 <> X0 then a = (Y1 - Y0) / (X1 - X0) b = Y1 - a * X1 IF X1 < X0 THEN itX = X1: endX = X0 ELSE itX = X0: endX = X1 WHILE itX <= endX #handle$ "line "; X2; " "; Y2; " "; int(itX); " "; int(a*itX + b) itX = itX + .25 WEND end if if X0 <> X2 then a = (Y0 - Y2) / (X0 - X2) b = Y0 - a * X0 IF X0 < X2 THEN itX = X0: endX = X2 ELSE itX = X2: endX = X0 WHILE itX <= endX #handle$ "line "; X1; " "; Y1; " "; int(itX); " "; int(a*itX + b) itX = itX + .25 WEND end if END SUB
Tested in modified tsh73 codes above:
'triangle torch mod B+ 2020-04-05 to test filledTriangle Sub 'tsh73 May 2017
nomainwin
UpperLeftX=1 UpperLeftY=1
open "flame" for graphics_nsb_nf as #gr print #gr, "trapclose [quit]" print #gr, "home ; down ; posxy cx cy" print #gr, "trapclose [quit]" width = 2*cx : height = 2*cy cxx=cx print #gr, "when leftButtonUp [quit]"
'torch handle #gr "fill black" r=30 for x=0-r to r y=sqr(r^2-x^2)/5 c=((1-(x/r)^2)*150+50)/1.5 #gr "color ";c;" ";c;" ";c #gr "place ";cx+x/1.5;" ";cy+y #gr "goto ";cx+x/6;" ";cy+y+130 next
for x=0-r to r y=sqr(r^2-x^2)/5 c=(1-(x/r)^2)*150+50 #gr "color ";c;" ";c;" ";c #gr "place ";cx+x;" ";cy+y #gr "go -30"
next #gr "getbmp torch 0 0 ";width;" ";height
timer 100, [nxt] while 1 scan #gr "discard" '#gr "fill black" #gr "drawbmp torch 0 0" for i = 1 to 7 cx = cxx+randR(-7,7) x1=cx-randR(0,20) y1=cy-randR(50,100) x2=cx+randR(0,20) 'y2=cy-randR(0,100) y2=(cy+y1)/2 if rnd(0)<.5 then tmp=y1:y1=y2:y2=tmp end if #gr "color ";randR(128,255);" ";randR(0,128);" 64" call filledTriangle "#gr", cx, cy, x1, y1, x2, y2 next wait [nxt] wend
wait
[quit] timer 0 close #gr end
function randR(min, max) randR=min+(max-min)*rnd(0) end function
SUB filledTriangle handle$, X0, Y0, X1, Y1, X2, Y2 if X1 <> X2 then a = (Y1 - Y2) / (X1 - X2) b = Y1 - a * X1 IF X1 < X2 THEN itX = X1: endX = X2 ELSE itX = X2: endX = X1 WHILE itX <= endX #handle$ "line "; X0; " "; Y0; " "; int(itX); " "; int(a*itX + b) itX = itX + .25 WEND end if if X1 <> X0 then a = (Y1 - Y0) / (X1 - X0) b = Y1 - a * X1 IF X1 < X0 THEN itX = X1: endX = X0 ELSE itX = X0: endX = X1 WHILE itX <= endX #handle$ "line "; X2; " "; Y2; " "; int(itX); " "; int(a*itX + b) itX = itX + .25 WEND end if if X0 <> X2 then a = (Y0 - Y2) / (X0 - X2) b = Y0 - a * X0 IF X0 < X2 THEN itX = X0: endX = X2 ELSE itX = X2: endX = X0 WHILE itX <= endX #handle$ "line "; X1; " "; Y1; " "; int(itX); " "; int(a*itX + b) itX = itX + .25 WEND end if END SUB
nomainwin open "filledTriangle" for graphics_nsb as #main #main, "down; color green" call filledTriangle "#main", 10, 10, 5, 70, 34, 16 #main, "color blue" call filledTriangle "#main", 10, 110, 5, 170, 34, 116 #main, "color red" call filledTriangle "#main", 10, 210, 5, 270, 34, 216 #main, "color darkgreen" call filledTriangle "#main", 100, 10, 70, 110, 130, 110 call filledTriangle "#main", 200, 100, 300, 70, 300, 130 ' B+ FIXED This doesn't work: last two points must not have the same x position #main, "trapclose [quit]" wait
[quit] close #main end
SUB filledTriangle handle$, X0, Y0, X1, Y1, X2, Y2 if X1 <> X2 then a = (Y1 - Y2) / (X1 - X2) b = Y1 - a * X1 IF X1 < X2 THEN itX = X1: endX = X2 ELSE itX = X2: endX = X1 WHILE itX <= endX #handle$ "line "; X0; " "; Y0; " "; int(itX); " "; int(a*itX + b) itX = itX + .25 WEND end if if X1 <> X0 then a = (Y1 - Y0) / (X1 - X0) b = Y1 - a * X1 IF X1 < X0 THEN itX = X1: endX = X0 ELSE itX = X0: endX = X1 WHILE itX <= endX #handle$ "line "; X2; " "; Y2; " "; int(itX); " "; int(a*itX + b) itX = itX + .25 WEND end if if X0 <> X2 then a = (Y0 - Y2) / (X0 - X2) b = Y0 - a * X0 IF X0 < X2 THEN itX = X0: endX = X2 ELSE itX = X2: endX = X0 WHILE itX <= endX #handle$ "line "; X1; " "; Y1; " "; int(itX); " "; int(a*itX + b) itX = itX + .25 WEND end if END SUB
|
|
|
Post by B+ on Apr 5, 2020 20:17:52 GMT
Oh my, Andy's fillTriangle is hands down better!
nomainwin global XMAX, YMAX XMAX = 800 : YMAX = 600 WindowWidth = XMAX + 8 WindowHeight = YMAX + 32 open "filledTriangle" for graphics_nsb as #main #main "trapclose quit" #main "down" while 1 scan #main "fill white"
#main "color ";rnd(0)*255;" ";rnd(0)*255;" ";rnd(0)*255
' test sub below 'call filledTriangle "#main", rnd(0)*XMAX, rnd(0)*YMAX,rnd(0)*XMAX, rnd(0)*YMAX,rnd(0)*XMAX, rnd(0)*YMAX
'try Andy's fill>>> Oh way better!!!!!!!!!!!!!!!! call fillTriangle "#main", rnd(0)*XMAX, rnd(0)*YMAX,rnd(0)*XMAX, rnd(0)*YMAX,rnd(0)*XMAX, rnd(0)*YMAX
call pause 300 wend wait
sub quit h$ close #main end end sub
SUB filledTriangle handle$, X0, Y0, X1, Y1, X2, Y2 if X1 <> X2 then a = (Y1 - Y2) / (X1 - X2) b = Y1 - a * X1 IF X1 < X2 THEN itX = X1: endX = X2 ELSE itX = X2: endX = X1 WHILE itX <= endX #handle$ "line "; X0; " "; Y0; " "; int(itX); " "; int(a*itX + b) itX = itX + .25 WEND end if if X1 <> X0 then a = (Y1 - Y0) / (X1 - X0) b = Y1 - a * X1 IF X1 < X0 THEN itX = X1: endX = X0 ELSE itX = X0: endX = X1 WHILE itX <= endX #handle$ "line "; X2; " "; Y2; " "; int(itX); " "; int(a*itX + b) itX = itX + .25 WEND end if if X0 <> X2 then a = (Y0 - Y2) / (X0 - X2) b = Y0 - a * X0 IF X0 < X2 THEN itX = X0: endX = X2 ELSE itX = X2: endX = X0 WHILE itX <= endX #handle$ "line "; X1; " "; Y1; " "; int(itX); " "; int(a*itX + b) itX = itX + .25 WEND end if END SUB
sub pause mil 'tsh version has scan built-in t0 = time$("ms") while time$("ms") < t0 + mil : scan : wend end sub
' Title: Filled Triangle Sub 'Programmer: JB port by Andy Amaya ' Date: 2010.10.27 'http://justbasic.conforums.com/index.cgi?board=shared&action=display&num=1307312447 Sub fillTriangle h$,x1, y1, x2, y2, x3, y3 'triangle coordinates must be ordered: where x1 < x2 < x3 If x2 < x1 Then x = x2 : y = y2 : x2 = x1 : y2 = y1 : x1 = x : y1 = y 'swap x1, y1, with x3, y3 If x3 < x1 Then x = x3 : y = y3 : x3 = x1 : y3 = y1 : x1 = x : y1 = y 'swap x2, y2 with x3, y3 If x3 < x2 Then x = x3 : y = y3 : x3 = x2 : y3 = y2 : x2 = x : y2 = y If x1 <> x3 Then slope1 = (y3-y1)/(x3-x1) 'draw the first half of the triangle length = x2 - x1 If length <> 0 Then slope2 = (y2-y1)/(x2-x1) For x = 0 To length #h$ "Line ";Int(x+x1);" ";Int(x*slope1+y1);" ";Int(x+x1);" ";Int(x*slope2+y1) Next End If 'draw the second half of the triangle y = length*slope1+y1 : length = x3-x2 If length <> 0 Then slope3 = (y3-y2)/(x3-x2) For x = 0 To length #h$ "Line ";Int(x+x2);" ";Int(x*slope1+y);" ";Int(x+x2);" ";Int(x*slope3+y2) Next End If End Sub
|
|
|
Post by bluatigro on Apr 6, 2020 6:21:23 GMT
this is my triangle
sub tri x1 , y1 , x2 , y2 , x3 , y3 , clr$ #m "color " ; clr$ #m "backcolor " ; clr$ if y1 = y2 then y1 = y1 - 1e-10 if y2 = y3 then y3 = y3 + 1e-10 if y1 > y3 then call swap y1 , y3 call swap x1 , x3 end if if y1 > y2 then call swap y1 , y2 call swap x1 , x3 end if if y2 > y3 then call swap y2 , y3 call swap x2 , y3 end if for i = y1 to y3 a = x1 + ( x3 - x1 ) * (i-y1) / ( y3 - y1 ) if i < y2 then b = x1 + ( x2 - x1 ) * (i-y1) / ( y2 - y1 ) else b = x2 + ( x3 - x2 ) * (i-y2) / ( y3 - y2 ) end if #m.g "down" #m.g "line " ; a ; " " ; i _ ; " " ; b ; " " ; i #m.g "up" next i #m.g "flush" end sub
|
|
|
Post by bluatigro on Apr 6, 2020 6:23:20 GMT
sub swap byref a , byref b h = a a = b b = h end sub
|
|
|
Post by Rod on Apr 6, 2020 8:51:23 GMT
Thank you all, good stuff, perhaps Carl can code something native for the next refresh.
|
|
|
Post by honkytonk on Apr 6, 2020 12:34:29 GMT
|
|
|
Post by B+ on Apr 6, 2020 14:47:54 GMT
What do you mean "detects the limits of a polygon"? Find the box it fits in? ie get the max/min of x and y?
|
|
|
Post by Rod on Apr 6, 2020 16:54:01 GMT
I see a flood fill routine in sprite factory but we have moved on from that. We are now drawing the polygon rather than pixel by pixel testing if we are in it. Much much faster.
|
|
|
Post by honkytonk on Apr 7, 2020 7:40:34 GMT
I see a flood fill routine in sprite factory but we have moved on from that. We are now drawing the polygon rather than pixel by pixel testing if we are in it. Much much faster. Indeed, but is the quick method valid for any polygons as is the slow flood method ?
|
|
|
Post by Rod on Apr 7, 2020 10:31:26 GMT
True, however every polygon is just a collection of triangles. The challenge would be to take Andy’s routine and combine it with a routine to find all triangles in a polygon.
|
|
|
Post by B+ on Apr 7, 2020 15:41:40 GMT
True, however every polygon is just a collection of triangles. The challenge would be to take Andy’s routine and combine it with a routine to find all triangles in a polygon. I have this worked out in theory as long as there are not 2 different points on any of 360 degrees) angle from an "origin" of polygon but why?
|
|