All OS Filled Polygon
Posted: Mon Jul 12, 2010 5:42 pm
I found myself in need of drawing polygons on an image so I coded one, it hasn't been optimized or anything but it's all I need at the moment. Hope someone finds it useful..
EDIT!
Turns out I posted the wrong code (Convex polygon fill) so heres the one I was supposed to post:-)
EDIT!
Turns out I posted the wrong code (Convex polygon fill) so heres the one I was supposed to post:-)
Code: Select all
Structure pippoint
x.l
y.l
EndStructure
Procedure FilledPolygon(List points.pippoint(),color)
NewList nodes.i()
;Find least and most y values to restrict area to transverse when filling..
ResetList(points())
miny=OutputHeight()-1
maxy=0
While NextElement(points())
If points()\y<miny:miny=points()\y:EndIf
If points()\y>maxy:maxy=points()\y:EndIf
Wend
For cy=miny To maxy
x.f=cx
y.f=cy
oddnodes=#False
ClearList(nodes())
LastElement(points())
x1.f=points()\x
y1.f=points()\y
ResetList(points())
While NextElement(points())
x2.f=points()\x
y2.f=points()\y
If ((y2<Y) And y1>=Y) Or (y1<Y And y2>=Y)
AddElement(nodes())
nodes()=(x2+(Y-y2) / (y1-y2) * (x1-x2))
EndIf
x1=x2
y1=y2
Wend
SortList(nodes(),#PB_Sort_Ascending)
ResetList(nodes())
While NextElement(nodes())
x1=nodes()
NextElement(nodes())
x2=nodes()
LineXY(x1,Y,x2,y,color)
Wend
Next
FirstElement(Points())
oldx=Points()\x
oldy=points()\y
While NextElement(points())
LineXY(oldx,oldy,Points()\x,Points()\y,color)
oldx=points()\x
oldy=Points()\y
Wend
EndProcedure
NewList mpp.pippoint()
OpenWindow(0, 0, 0, 640, 480, "Filled Polygon", #PB_Window_SystemMenu)
CreateImage(0, 640, 480)
StartDrawing(ImageOutput(0))
Box(0, 0, 640, 480, 0)
StopDrawing()
ImageGadget(0, 0, 0, 640, 480, ImageID(0))
Exit = #False
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_Gadget
If EventGadget() = 0
Select EventType()
Case #PB_EventType_RightClick
If ListSize(mpp()) > 2
StartDrawing(ImageOutput(0))
filledpolygon(mpp(),RGB(Random(255),Random(255),Random(255)))
StopDrawing()
SetGadgetState(0, ImageID(0))
ClearList(mpp())
EndIf
Case #PB_EventType_LeftClick
x2 = WindowMouseX(0)
y2 = WindowMouseY(0)
If ListSize(mpp()) > 0
SelectElement(mpp(), ListSize(mpp()) - 1)
StartDrawing(ImageOutput(0))
LineXY(mpp()\x, mpp()\y, x2, y2, $FFFFFF)
StopDrawing()
SetGadgetState(0, ImageID(0))
EndIf
AddElement(mpp())
mpp()\x = x2
mpp()\y = y2
EndSelect
EndIf
Case #PB_Event_CloseWindow
Exit = #True
EndSelect
Until Exit