gah! How to clip rotated rectangles.

BlitzMax Forums/BlitzMax Programming/gah! How to clip rotated rectangles.

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

I decided to take some time out for coding anyways, and this is my code converted to Bmax and where it checks the source image for out of bounds, so that the image doesn't repeat itself on the destination image like it would in a rotozoomer.

This example is hardcoded for a 256x256 image, like the BlitzPlus code was. I don't have time to make it more generic, but with the addition of some local variables that should be doable.

SuperStrict

Graphics 640,480

Local oldImage:TImage = LoadImage("tiger.png")

' generate precalc sin/cos arrays
Global sn:Float[] = New Float[360]
Global cs:Float[] = New Float[360]
For Local i:Int = 0 To 359
	sn[i] = Sin(i)
	cs[i] = Cos(i)
Next 

Local angle:Int = 0

While Not KeyHit(KEY_ESCAPE)
	Cls
	Local newImage:Timage = rotateImageClipped(oldImage,angle)
	DrawImage(oldImage,10,100)
	DrawImage(newImage,276,100)
	Flip

	angle :+ 1
	If angle > 359 Then angle :- 360

Wend
End

Function rotateImageClipped:TImage(img:TImage,angle:Int)
	Local sourcePmap:TPixmap = LockImage(img)						' lock source image and create pixmap to get data from
	Local width:Int = sourcePmap.width								' find width of image
	Local height:Int = sourcePmap.height							' find height of image
	Local destPmap:TPixmap = CreatePixmap(width,height,PF_RGBA8888)	' create destination pixmap where the rotated image is stored
	ClearPixels(destPmap)											' clear pixmap
	' loop through destPmap pixels and copy from source image
	For Local y:Int = -127 To 128
		For Local x:Int = -127 To 128
			Local tx:Int = x * cs[angle] - y * sn[angle] + 128
			Local ty:Int = y * cs[angle] + x * sn[angle] + 128
			' test to see if within bounds of source image
			If tx > -1 And tx < 256 And ty > -1 And ty < 256 Then
				WritePixel(destPmap,x+127,y+127,ReadPixel(sourcePmap,tx,ty))
			End If
		Next
	Next
	UnlockImage(img)	' unlock source image
	Return LoadImage(destPmap)	' return the newly created image
End Function