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.
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
If you like piece of my code, go ahead and use it. I had my share of fun creating it - now it's free.
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
If you like piece of my code, go ahead and use it. I had my share of fun creating it - now it's free.
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
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.
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 ?
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.
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?