I have a couple of short 2D programs that use bitwise and trigonometry quite a lot:
Better Circles:
`Better Circles by OBese87
`19/08/09 - 7/2/2013
set window on
set display mode 1024,768,16
sync on:sync rate 30
ink -2,8
do
if mc=0 then mc=mouseclick() else mc=0-mouseclick()
if mc>0 then mode = (mode|mc) - (mode&mc)
rem adjust radii based on mouse position.
rx = abs(mousex()-512)
ry = abs(mousey()-384)
r = (rx^2+ry^2)^0.5
rem - output -
backdrop()
if r>0
rem Modes: 0-filled ellipse, 1-hollow ellipse, 2-filled circle, 3-hollow circle.
if mode&2
if mode&1 then hcircle(512,384,r) else fcircle(512,384,r)
else
if mode&1 then hellipse(512,384,rx,ry) else fellipse(512,384,rx,ry)
endif
endif
text 20,20,"Move the mouse to adjust radii."
text 20,36,"Left-click to switch between hollow and filled."
text 20,52,"Right-click to switch between circle and ellipse."
set cursor 0,80
print mc
print mode
print screen fps()
sync
loop
end
rem ===============================================================
rem = Functions
rem ===============================================================
rem obese87's filled circle
function fcircle(x,y,r)
dots=r*6.28
t#=360/(dots+.)
quarter = dots/4
for i = 1 to quarter
u=sin(i*t#)*r
v=cos(i*t#)*r
box x-u,y-v,x+u,y+v
next i
endfunction
`//
rem obese87's hollow circle
Function hcircle(x,y,r)
dots=r*6.28 :`pi2 radians (circle circumference)
t#=360/(dots+.) :`degrees per dot
for i = 1 to dots
u=sin(i*t#)*r
v=cos(i*t#)*r
dot x+u,y+v
next i
Endfunction
`//
rem obese87's filled ellipse
function fellipse(x,y,rw,rh)
if rw>rh then rmax=rw else rmax=rh
dots=rmax*6.28
t#=360/(dots+.)
quarter = dots/4
for i = 1 to quarter
u=sin(i*t#)*rw
v=cos(i*t#)*rh
box x-u,y-v,x+u,y+v
next i
endfunction
`//
rem obese87's hollow ellipse
Function hellipse(x,y,rw,rh)
if rw>rh then rmax=rw else rmax=rh
dots=rmax*6.28
t#=360/(dots+.)
for i = 1 to dots
u=sin(i*t#)*rw
v=cos(i*t#)*rh
dot x+u,y+v
next i
Endfunction
`//
function backdrop()
if bitmap exist(1)=0
s=128
create bitmap 1,s,s
for y = 0 to s-1
for x = 0 to s-1
ink ((x&s-1)+(y&s-1))*2,0
dot x,y
next x
next y
set current bitmap 0
ink -2,0
endif
`paste to screen
for y = 0 to screen height()/s -1
for x = 0 to screen width()/s -1
copy bitmap 1,0,0,s-1,s-1, 0,s*x,s*y,s*x+s-1,s*y+s-1
next x
next y
endfunction
`//
Filled Triangles
rem Program Name: Opaque Triangle
rem Author: OBese87
rem Created: 30/11/09
rem Updated: 7/2/2013
rem ==================
set window on
set display mode 1024,768,32
sync on
sync rate 0
rem vertice data array
dim x(3)
dim y(3)
rem load some initial data (leave field 0 empty)
data 200,400,400,100,600,400
for i = 1 to 3
read x(i)
read y(i)
next i
green = 256 * 255
red = 65536 * 255
cyan = 65535
rem = Main ========================================================================================
do
rem drag vertices
if mouseclick()=1
if drag=0
rem check if mouse is on a vertex drag point.
for v = 1 to 3
if abs(mousex()-x(v))<=16
if abs(mousey()-y(v))<=16 then drag=v:exit
endif
next v
else
rem move dragged vertex
x(drag)=mousex()
y(drag)=mousey()
if y(drag)<74 then y(drag)=74
endif
else
drag=0
endif
rem - Output -
backdrop()
rem draw triangle
fill_triangle(x(1),y(1),x(2),y(2),x(3),y(3))
rem draw draggable vertices
for i=1 to 3
if i=drag then ink green,0 else ink red,0
box x(i)-4,y(i)-4,x(i)+4,y(i)+4
next i
rem user instructions
ink -2,0
text 0,0,"Triangles are drawn using the BOX command so the function is relatively fast."
text 0,16,"Coordinates are sorted before drawing; this allows for dynamic rotating triangles that are always drawn perfectly."
text 0,32,"I hope you will use this function, I can see so many possibilities for it."
text 0,48,"Click and drag a vertex to move it around."
sync
loop
end
`//
rem = Functions ===================================================================================
function fill_triangle(ax,ay,bx,by,cx,cy)
rem sort parameters
if ax > cx
ax=ax+cx : cx=ax-cx : ax=ax-cx
ay=ay+cy : cy=ay-cy : ay=ay-cy
endif
if ax > bx
ax=ax+bx : bx=ax-bx : ax=ax-bx
ay=ay+by : by=ay-by : ay=ay-by
endif
if bx > cx
bx=bx+cx : cx=bx-cx : bx=bx-cx
by=by+cy : cy=by-cy : by=by-cy
endif
rem create triangle
for x = ax to cx
rem Split the triangle into two halves
if x < bx
rem Since x<bx we know bx>ax.
y1 = ay+(by-ay)*((x-ax)/(bx-ax+.0))
ink 65535,0
endif
if x > bx
y1 = by+(cy-by)*((x-bx)/(cx-bx+.0))
ink -2,0
endif
if x = bx then y1 = by :`avoid divide by zero.
y2 = ay+(cy-ay)*((x-ax)/(cx-ax+.0))
rem sort y for box drawing
if y1>y2 then y1=y1+y2 : y2=y1-y2 : y1=y1-y2
box x,y1,x,y2
next x
endfunction
`//
function backdrop()
if bitmap exist(1)=0
s=128
create bitmap 1,s,s
for y = 0 to s-1
for x = 0 to s-1
ink ((x&s-1)+(y&s-1))*2,0
dot x,y
next x
next y
set current bitmap 0
ink -2,0
endif
`paste to screen
for y = 0 to screen height()/s -1
for x = 0 to screen width()/s -1
copy bitmap 1,0,0,s-1,s-1, 0,s*x,s*y,s*x+s-1,s*y+s-1
next x
next y
endfunction
`//
Use them in your examples if you like. They were written in DBC so may not be optimized for DBP.
^ That's what she said.