pixler/include/tools.bm

224 lines
6.7 KiB
Text
Raw Normal View History

2026-05-12 11:47:31 +02:00
sub thickbox(sx,sy,ex,ey,col as long)
2026-05-20 10:39:08 +02:00
thickline sx,sy,ex,sy,col
thickline ex,sy,ex,ey,col
thickline ex,ey,sx,ey,col
thickline sx,ey,sx,sy,col
2026-05-12 11:47:31 +02:00
end sub
2026-04-30 13:16:00 +02:00
2026-05-12 11:47:31 +02:00
sub filledbox(sx,sy,ex,ey,col as long)
line(sx,sy)-(ex,ey),col,bf
end sub
2026-04-30 13:16:00 +02:00
2026-05-20 10:39:08 +02:00
sub filledpolygon (points() as long,col as long)
dim i as integer,j as integer
dim x1 as single,y1 as single,x2 as single,y2 as single
dim intersectx as single
2026-04-30 13:16:00 +02:00
' get the number of points from the upper bound of the array
' divide by 2 since we have x,y pairs
2026-05-20 10:39:08 +02:00
dim numpoints as integer
numpoints=(ubound(points)+1) \ 2
2026-04-30 13:16:00 +02:00
' loop through each scanline (rows of pixels)
dim intersections(100) as single
2026-05-20 10:39:08 +02:00
dim numintersections as integer
for y=0 to _height ' adjust for screen height
numintersections=0
2026-04-30 13:16:00 +02:00
' check for intersections between the polygon edges and this scanline
2026-05-20 10:39:08 +02:00
for i=0 to numpoints-1
x1=points(i*2)
y1=points(i*2+1)
x2=points(((i+1) mod numpoints)*2)
y2=points(((i+1) mod numpoints)*2+1)
2026-04-30 13:16:00 +02:00
' check if the scanline intersects with the edge of the polygon
2026-05-20 10:39:08 +02:00
if ((y1>y and y2<=y) or (y2>y and y1<=y)) then
2026-04-30 13:16:00 +02:00
' calculate intersection point with the scanline
2026-05-20 10:39:08 +02:00
intersectx=x1+(y-y1)*(x2-x1)/(y2-y1)
intersections(numintersections)=intersectx
numintersections=numintersections+1
2026-04-30 13:16:00 +02:00
end if
next i
' sort the intersections (sort by x-coordinates)
2026-05-20 10:39:08 +02:00
for i=0 to numintersections-1
for j=i+1 to numintersections-1
if intersections(i)>intersections(j) then
swap intersections(i),intersections(j)
2026-04-30 13:16:00 +02:00
end if
next j
next i
' fill the area between pairs of intersections
2026-05-20 10:39:08 +02:00
for i=0 to numintersections-1 step 2
2026-04-30 13:16:00 +02:00
line(intersections(i),y)-(intersections(i+1),y),col
next i
next y
end sub
2026-05-11 10:29:29 +02:00
sub thickpixel(x,y,col as long)
if state.brushsize=1 then
pset(x,y),col
else
2026-05-20 10:39:08 +02:00
line(x-0.5*state.brushsize,y-0.5*state.brushsize)-(x+0.5*state.brushsize,y+0.5*state.brushsize),col,bf
2026-05-11 10:29:29 +02:00
end if
end sub
2026-05-20 10:39:08 +02:00
sub thickline(x1,y1,x2,y2,col as long)
2026-04-30 13:16:00 +02:00
if state.brushsize=1 then
line(x1,y1)-(x2,y2),col
else
dim tempimg as long
dim od as long
2026-05-11 10:29:29 +02:00
tempimg=_newimage(1,1,32)
2026-05-20 10:39:08 +02:00
od=_dest
2026-04-30 13:16:00 +02:00
_dest tempimg
pset(0,0),col
_dest od
2026-05-20 10:39:08 +02:00
a=_atan2(y2-y1,x2-x1)
a=a+_pi/2
x0=0.5*state.brushsize*cos(a)
y0=0.5*state.brushsize*sin(a)
_maptriangle _seamless(0,0)-(0,0)-(0,0),tempimg to (x1-x0,y1-y0)-(x1+x0,y1+y0)-(x2+x0,y2+y0),,_smooth
_maptriangle _seamless(0,0)-(0,0)-(0,0),tempimg to (x1-x0,y1-y0)-(x2+x0,y2+y0)-(x2-x0,y2-y0),,_smooth
2026-04-30 13:16:00 +02:00
_freeimage tempimg
end if
end sub
2026-05-20 10:39:08 +02:00
sub polygon (pa() as long,col as long)
for i=2 to ubound(pa) step 2
thickline pa(i-2),pa(i-1),pa(i),pa(i+1),col
next i
thickline pa(ubound(pa)-1),pa(ubound(pa)),pa(0),pa(1),col
end sub
2026-05-01 13:58:36 +02:00
2026-05-20 10:39:08 +02:00
sub thickcircle(x,y,r,col as long)
if state.brushsize<=1 then
circle (x,y),r,col
2026-04-30 13:16:00 +02:00
else
2026-05-20 10:39:08 +02:00
dim rp as single,rm as single,rp2 as single,rm2 as single
dim rpi2 as single,rmi2 as single,sp as single,sm as single
2026-04-30 13:16:00 +02:00
dim i as single
2026-05-20 10:39:08 +02:00
rp=r+state.brushsize/2
rm=r-state.brushsize/2
2026-04-30 13:16:00 +02:00
' If the brush is thicker than the circle, it's just a filled circle
2026-05-20 10:39:08 +02:00
if rm<0 then
filledcircle x,y,rp,col
2026-04-30 13:16:00 +02:00
exit sub
end if
2026-05-20 10:39:08 +02:00
rp2=rp ^ 2
rm2=rm ^ 2
2026-04-30 13:16:00 +02:00
' Outer edges (Top/Bottom caps)
2026-05-20 10:39:08 +02:00
for i=-rp to -rm step .2
rpi2=rp2-i ^ 2
if rpi2<0 then rpi2=0 ' Safety Gate
sp=sqr(rpi2)
line (x+i,y-sp)-(x+i,y+sp),col,bf
2026-04-30 13:16:00 +02:00
next
' Side rings (where the hole in the middle exists)
2026-05-20 10:39:08 +02:00
for i=-rm to rm step .2
rpi2=rp2-i ^ 2
rmi2=rm2-i ^ 2
if rpi2<0 then rpi2=0 ' Safety Gate
if rmi2<0 then rmi2=0 ' Safety Gate
sp=sqr(rpi2)
sm=sqr(rmi2)
2026-04-30 13:16:00 +02:00
' Draw the top and bottom segments only
2026-05-20 10:39:08 +02:00
line (x+i,y+sm)-(x+i,y+sp),col,bf
line (x+i,y-sm)-(x+i,y-sp),col,bf
2026-04-30 13:16:00 +02:00
next
' Outer edges (Right cap)
2026-05-20 10:39:08 +02:00
for i=rm to rp step .2
rpi2=rp2-i ^ 2
if rpi2<0 then rpi2=0 ' Safety Gate
sp=sqr(rpi2)
line (x+i,y-sp)-(x+i,y+sp),col,bf
2026-04-30 13:16:00 +02:00
next
2026-05-20 10:39:08 +02:00
end if
2026-04-30 13:16:00 +02:00
end sub
sub filledcircle(x,y,r,col as long)
2026-05-20 10:39:08 +02:00
dim __radius as integer,radiuserror as integer
dim tx as integer,ty as integer
__radius=abs(r)-1
radiuserror=-__radius
tx=__radius
ty=0
line (x-tx,y)-(x+tx,y),col
while tx>ty
radiuserror=radiuserror+ty*2+1
if radiuserror>=0 then
if tx<>ty+1 then
line (x-ty,y-tx)-(x+ty,y-tx),col
line (x-ty,y+tx)-(x+ty,y+tx),col
end if
tx=tx-1
radiuserror=radiuserror-tx*2
end if
ty=ty+1
line (x-tx,y-ty)-(x+tx,y-ty),col
line (x-tx,y+ty)-(x+tx,y+ty),col
wend
end sub
sub floodfill (startx,starty,fillcolor~&)
' We use a simple array as a stack for (x, y) pairs
' For large images, you may need to increase this size
dim stackx(2000) as integer
dim stacky(2000) as integer
targetcolor~&=point(startx,starty)
if targetcolor~&=fillcolor~& then exit sub
stackptr=1
stackx(stackptr)=startx
stacky(stackptr)=starty
while stackptr>0
curx=stackx(stackptr)
cury=stacky(stackptr)
stackptr=stackptr-1
' Move to the left edge of the span
x=curx
while point(x,cury)=targetcolor~& and x>=0
x=x-1
wend
x=x+1
spanabove=0
spanbelow=0
' Process the span moving right
while point(x,cury)=targetcolor~& and x<_width
pset (x,cury),fillcolor~&
' Check row above
if cury>0 then
if spanabove=0 and point(x,cury-1)=targetcolor~& then
stackptr=stackptr+1
stackx(stackptr)=x
stacky(stackptr)=cury-1
spanabove=1
elseif spanabove=1 and point(x,cury-1)<>targetcolor~& then
spanabove=0
2026-04-30 13:16:00 +02:00
end if
end if
2026-05-20 10:39:08 +02:00
' Check row below
if cury<_height-1 then
if spanbelow=0 and point(x,cury+1)=targetcolor~& then
stackptr=stackptr+1
stackx(stackptr)=x
stacky(stackptr)=cury+1
spanbelow=1
elseif spanbelow=1 and point(x,cury+1)<>targetcolor~& then
spanbelow=0
end if
end if
x=x+1
2026-04-30 13:16:00 +02:00
wend
2026-05-20 10:39:08 +02:00
wend
2026-04-30 13:16:00 +02:00
end sub
2026-05-01 13:58:36 +02:00