Reputation: 852
I'm making a 2d physics engine, when ever a object(like a chair) is spawned, it creates a 2d boolean array twice the size of the original image(to allow for rotation) and creates a shape the same as the image(excluding pixels of a oppacity less than 1)
how would I rotate this array x degrees?
rotate the true values in this x degrees, where "this" can be any shape
f=false t=true
f f f f f f f
f f f f f f f
f f T T T f f
f f T T T f f
f f T T T f f
f f f f f f f
f f f f f f f
Upvotes: 3
Views: 1057
Reputation: 10063
This is a basic image rotation algorithm, except that you're just rotating a T/F bitmap, correct?
From my notes:
http://www.leptonica.com/rotation.html
http://datagenetics.com/blog/august32013/index.html
Rotation by shear:
Catmull & Smith
Very fast; implemented by rasterops. Can be done in place.
Rows of pixels are moved a distance proportional to their vertical distance from the center of rotation. A sequence of three alternating H,V,H shears perform the rotation. For small angles (< 3 degrees), it can be done with two shears
Distortions can accumulate; if you were to rotate an image using 180 rotations of 12 degrees each, the result would be effectively encrypted. However, "unwinding" the image would restore it.
R = [ cos(a) sin(a) ]
[ -sin(a) cos(a) ]
= [ 1 tan(a/2) ] [ 1 0 ] [ 1 tan(a/2) ]
[ 0 1 ] [ -sin(a) 1 ] [ 0 1 ]
Note: first rotate in steps of 90° so that a <= 45
I have C code for a similar algorithm that does arbitrary rotation, scaling, and translation of an image, all with integer math, but it's big and hairy and probably not appropriate for your problem.
Frankly though, I'd just look around and see if there's already an implementation in Java; look for 2-d graphics libraries.
Upvotes: 2