Polygons!
It may not be efficient, but it sure is pretty
The code draws any enclosed polygon using drawLine. There are certainly many possibilities, but for the time being I kept it simple (for instance if you play with how bounds are selected the polygon won't be drawn outside this region. Currently that will never be the case) I put it together for one of my other threads but figured I mind as well share it independently. Perhaps it'll be of some use to someone! Have fun drawing things
Teh Codez:
setSyncRate(0, 0)
setVirtualResolution(480, 480) //my setup.agc is set to 480 by 480
global pNm : pNm = 64
type point_UDT
x#
y#
endtype
dim p[pNm] as point_UDT
do
for i = 0 to pNm-1
p[i].x# = (1+0.2*sin(timer()*8*i))* 100*cos(360.0*i/(pNm-1.0))+240
p[i].y# = (1+0.2*sin(timer()*8*i))* 100*sin(360.0*i/(pNm-1.0))+240
next i
gosub drawPolygon
print(screenFPS())
sync()
loop
drawPolygon:
//calculate bounds
minID = 0
for i = 0 to pNm-1
if p[i].y# < p[minID].y# then minID = i
next i
maxID = 0
for i = 0 to pNm-1
if p[i].y# > p[maxID].y# then maxID = i
next i
minY = p[minID].y#
maxY = p[maxID].y#
minID = 0
for i = 0 to pNm-1
if p[i].x# < p[minID].x# then minID = i
next i
maxID = 0
for i = 0 to pNm-1
if p[i].x# > p[maxID].x# then maxID = i
next i
minX = p[minID].x#
maxX = p[maxID].x#
if maxY - minY < maxX - minX //select most efficient method
gosub drawPolygonY
else
gosub drawPolygonX
endif
return
drawPolygonY:
dim nodeX[pNm]
for pixelY = minY to maxY-1
nodes = 0
j = pNm-1 //build nodes
for i = 0 to pNm-1
if p[i].y# < pixelY and p[j].y# >= pixelY or p[j].y# < pixelY and p[i].y# >= pixelY
nodeX[nodes] = p[i].x#+(pixelY+0.0-p[i].y#)/(p[j].y#-p[i].y#)*(p[j].x#-p[i].x#)
nodes = nodes+1
endif
j = i
next i
i = 0 //sort nodes
while i < nodes-1
if nodeX[i] > nodeX[i+1]
swp = nodeX[i]
nodeX[i] = nodeX[i+1]
nodeX[i+1] = swp
if i <> 0 then i = i-1
else
i = i+1
endif
endwhile
stop = 0 //draw nodes (if in range)
for i = 0 to nodes-1 step 2
if nodeX[i] > maxX then stop = 1
if stop <> 1 //break if following nodes will exceed range
if nodeX[i+1] > minX
if nodeX[i] < minX then nodeX[i] = minX
if nodeX[i+1] > maxX then nodeX[i+1] = maxX
drawLine(nodeX[i], pixelY, nodeX[i+1]-1, pixelY, 255, 255, 255)
endif
endif
next i
next pixelY
return
drawPolygonX:
dim nodeY[pNm]
for pixelX = minX to maxX-1
nodes = 0
j = pNm-1 //build nodes
for i = 0 to pNm-1
if p[i].x# < pixelX and p[j].x# >= pixelX or p[j].x# < pixelX and p[i].x# >= pixelX
nodeY[nodes] = p[i].y#+(pixelX+0.0-p[i].x#)/(p[j].x#-p[i].x#)*(p[j].y#-p[i].y#)
nodes = nodes+1
endif
j = i
next i
i = 0 //sort nodes
while i < nodes-1
if nodeY[i] > nodeY[i+1]
swp = nodeY[i]
nodeY[i] = nodeY[i+1]
nodeY[i+1] = swp
if i <> 0 then i = i-1
else
i = i+1
endif
endwhile
stop = 0 //draw nodes (if in range)
for i = 0 to nodes-1 step 2
if nodeY[i] > maxY then stop = 1
if stop <> 1 //break if following nodes will exceed range
if nodeY[i+1] > minY
if nodeY[i] < minY then nodeX[i] = minY
if nodeY[i+1] > maxY then nodeX[i+1] = maxY
drawLine(pixelX, nodeY[i], pixelX, nodeY[i+1]-1, 255, 255, 255)
endif
endif
next i
next pixelX
return