Lingo functions for working with rects

Date:    Thu, 17 Aug 1995 22:37:26 +0000
From:    Doug Anarino <direct@INTER.DEMON.CO.UK>
Subject: Helpful Rect Functions
I really hate working with rects. I always wind up spending hours staring at wonky sprites flying every which way and scratching my head. Shockwave has forced me to spend a day hammering out some generic functions. Prehaps you'll find some useful.


This takes 2 rects and centers the first over the last
on centerrects arect,zrect
  put getat(zrect,3)-getat(zrect,1) into ah
  put getat(zrect,4)-getat(zrect,2) into av
  put getat(arect,3)-getat(arect,1) into zh
  put getat(arect,4)-getat(arect,2) into zv
  put -((ah-zh)/2) into h
  put -((av-zv)/2) into v
  return inflate(zrect,h,v)
end

This blends between 2 rects over multiple calls. Step is which step you're on, limit is the total number of steps
on blendrects step,limit,srect,erect
  if step=1 then return srect
  if step=limit then return erect
  put abs(getat(srect,1)-getat(erect,1))/limit*step into h1
  put abs(getat(srect,2)-getat(erect,2))/limit*step into v1
  put getat(srect,3)-abs(getat(srect,3)-getat(erect,3))/limit*step into h2
  put getat(srect,4)-abs(getat(srect,4)-getat(erect,4))/limit*step into v2
  return rect(h1,v1,h2,v2)
end

This one's complex but very useful. It takes a rect and divides it into however many cells are created by your rows and columns values. It then returns the rect that contains the cells between cell number startIndex and cell number endIndex.

To see what this could do for you make a new frame with a QuickDraw shape in channel 1. Take the rest of this message and stick it in the sprite script - click it!

on splicerect arect, rows, cols, startIndex, endIndex
  if voidp(endindex) then put startindex into endindex
  put abs(getat(arect,3) - getat(arect,1))*1.0000000000/rows into hu
  put abs(getat(arect,4) - getat(arect,2))*1.0000000000/cols into vu
  put (hu * (((startindex-1) mod rows))) + getat(arect,1) into h1
  put (vu * ((startindex-1)/rows)) + getat(arect,2) into v1
  put (hu * (((endindex-1) mod rows))+hu) + getat(arect,1) into h2
  put (vu * (((endindex-1)/rows))+vu) + getat(arect,2) into v2
  return rect(h1,v1,h2,v2)
end

This is just a simple spritebox trap that takes a rect datatype
on myspritebox channel,r
  spritebox channel,getat(r,1),getat(r,2),getat(r,3),getat(r,4)
end

This returns the current stage area as a local rect
on stagerect
  return rect(0,0,the stageright-the stageleft,the stagebottom-the stagetop)
end

This calls the others:
on mousedown
  puppetsprite 1,true
  put 1 into nextRect
  put 1 into direction
  repeat while the mousedown
    myspritebox 1,splicerect(stagerect(),8,8,nextRect)
    put nextRect + direction into nextRect
    if nextRect = 65 or nextRect = 0 then put -(direction) into direction
    updatestage
  end repeat
  puppetsprite 1,false
end