I'm mucking around with a rotating ellipse snippet I had posted sometime earlier:
http://www.thegamecreators.com/?m=forum_view&t=17979&b=6 when I came across a bit of a puzzle.
sync on : sync rate 0
xr = 50
yr = 100
sd = 360
do
cls
mx = mousex() : my = mousey()
xd = mx - 320 : yd = my - 240
mouseangle# = 180.0 - atanfull(xd,yd)
if mouseclick() = 1 then xr = sqrt((xd*xd)+(yd*yd))
if mouseclick() = 2 then yr = sqrt((xd*xd)+(yd*yd))
if upkey() or downkey()
if upkey() and (sd < 360) then sd = sd + 5
if downkey() and (sd > 5) then sd = sd - 5
` 1/10th of a sec delay
t = timer() + 100
repeat : until timer() >= t
endif
tim = timer()
RotateEllipse(320,240,xr,yr,mouseangle#,sd)
tim = timer()-tim
text 0,0, "X radius (left button) : " + str$(xr)
text 0,16,"Y radius (right button): " + str$(yr)
text 0,32,"Rotation (move mouse) : " + str$(mouseangle#)
text 0,48,"Sides (up & down keys) : " + str$(sd)
text 0,64,"Draw Time: " + str$(tim) + "/1000th sec"
sync
loop
` ------------------------------------------------------------------------
function RotateEllipse(centerx#,centery#,xradius#,yradius#,rotation#,sides)
st# = 360.0 / abs(sides)
a# = 0.0
for s = 0 to sides
` first calculate point without rotation
px# = xradius# * sin(a#)
py# = yradius# * cos(a#)
` now rotate the point
ang# = (180.0 - atanfull(px#,py#)) - rotation#
ra# = sqrt((px#*px#) + (py#*py#))
px# = ra# * sin(ang#) + centerx#
py# = ra# * cos(ang#) + centery#
` draw
if s > 0
line lastpx#,lastpy#,px#,py#
` MyLine(lastpx#,lastpy#,px#,py#)
endif
lastpx# = px# : lastpy# = py#
a# = a# + st#
next s
endfunction
function MyLine(x1,y1,x2,y2)
dx = abs(x2 - x1) : dy = abs(y2 - y1)
if dx >= dy
numpixels = dx + 1
d = (2 * dy) - dx
dinc1 = dy << 1
dinc2 = (dy - dx) << 1
xinc1 = 1 : xinc2 = 1
yinc1 = 0 : yinc2 = 1
else
numpixels = dy + 1
d = (2 * dx) - dy
dinc1 = dx << 1
dinc2 = (dx - dy) << 1
xinc1 = 0 : xinc2 = 1
yinc1 = 1 : yinc2 = 1
endif
if x1 > x2 : xinc1 = -xinc1 : xinc2 = -xinc2 : endif
if y1 > y2 : yinc1 = -yinc1 : yinc2 = -yinc2 : endif
for i = 1 to numpixels
box x1,y1,x1+1,y1+1
if d < 0
inc d,dinc1 : inc x1,xinc1 : inc y1,yinc1
else
inc d,dinc2 : inc x1,xinc2 : inc y1,yinc2
endif
next i
endfunction
It's not bad, fairly nippy but I thought I could make it go faster by adding my own line drawing routine in there. Go to line 58 and rem out the
Line and unrem the
MyLine() call on line 59.
It runs 3-4 times faster for me. How about you?
If it is running faster, then why does the following say it does not?
sync on : sync rate 0
` draw using Line command
randomize 12345 : ` same random pattern for both tests
ink rgb(255,0,0),0
time1 = timer()
for i = 1 to 1000
line rnd(639),rnd(479),rnd(639),rnd(479)
next i
time1 = timer() - time1
ink rgb(255,255,255),0
print "Line command:" + str$(time1)
print : print "Press any key"
sync : sync : wait key
` draw using MyLine()
cls
randomize 12345 : ` same random pattern for both tests
ink rgb(0,0,255),0
time2 = timer()
for i = 1 to 1000
Myline(rnd(639),rnd(479),rnd(639),rnd(479))
next i
time2 = timer() - time2
ink rgb(255,255,255),0
print "Line command:" + str$(time1)
print "MyLine function: " + str$(time2)
print : print "Press any key"
sync : sync : wait key
end
` ------------------------------------------------------------------------
function MyLine(x1,y1,x2,y2)
dx = abs(x2 - x1) : dy = abs(y2 - y1)
if dx >= dy
numpixels = dx + 1
d = (2 * dy) - dx
dinc1 = dy << 1
dinc2 = (dy - dx) << 1
xinc1 = 1 : xinc2 = 1
yinc1 = 0 : yinc2 = 1
else
numpixels = dy + 1
d = (2 * dx) - dy
dinc1 = dx << 1
dinc2 = (dx - dy) << 1
xinc1 = 0 : xinc2 = 1
yinc1 = 1 : yinc2 = 1
endif
if x1 > x2 : xinc1 = -xinc1 : xinc2 = -xinc2 : endif
if y1 > y2 : yinc1 = -yinc1 : yinc2 = -yinc2 : endif
for i = 1 to numpixels
box x1,y1,x1+1,y1+1
if d < 0
inc d,dinc1 : inc x1,xinc1 : inc y1,yinc1
else
inc d,dinc2 : inc x1,xinc2 : inc y1,yinc2
endif
next i
endfunction
Or have I completly overlooked the obvious?
Well I do have a cold and a headache and I'm not thinking straight so I'm not sure.
Programming anything is an art, and you can't rush art.
Unless your name is Bob Ross, then you can do it in thirty minutes.