cohost! – “Rotation with three shears”

There is a graphics trick that was broadly identified that has now most likely nearly vanished from the graphics consciousness – you are able to do rotations by making use of three shears in a row. This could shock you! It shocked me. And it re-surprises me each time I keep in mind it.
Shears are quite simple graphics operations to do – you simply render the sprite, however shifting the strains or columns a bit every time. However rotations are gnarly issues that contain a lot of maths and interpolation and so forth. So how might you probably assemble one from the opposite?
The derivation is comparatively easy, however I will not do it right here as a result of there is a completely good clarification over right here:
https://www.ocf.berkeley.edu/~fricke/projects/israel/paeth/rotation_by_shearing.html
However the TL;DR is you do three shears:
- shear in X by -tan(angle/2)
- shear in Y by sin(angle)
- shear in X by -tan(angle/2)
(that is not a typo – the third shear is precisely the identical as the primary!)
This works for all angles -90 levels to +90 levels, which is implausible! Past that you simply simply want to use an X and Y flip first (i.e. a rotation by 180), which may normally be completed as a part of the primary shear.
Here is a GIF of the R-Sort fighter being easily rotated. Every picture is a sheared model of the one to its left, with the ultimate rotated picture on the proper. Sorry I did not get the looping excellent.
However why go to all this hassle although? Why not get your GPU to do it? Nicely, what if you do not have one? Again within the days of 16-bit and 32-bit machines, we did not have fancy GPUs that might do arbitrary rotations. However we did have “blitters” that might copy rectangles of pixels from one place to a different. And if you happen to had been intelligent you possibly can persuade them to do a shear on the similar time, as a result of it is simply offsetting the rows or columns as you go. So utilizing this trick you possibly can get arbitrary rotations completed. Now, you might be doing three of them for a single rotated sprite, so it is not precisely free, however the truth that you possibly can do them in any respect was fairly magical.
Doing rotations this fashion additionally has some very fascinating traits that doing them with a extra basic GPU operation doesn’t:
-
There is no such thing as a “maths” wanted on the pixel knowledge. We’re simply copying bits – there isn’t any interpretation of what the bits truly imply. This implies you are able to do this with any pixel format – it may be bitplaned 4-bit-per-pixel, 8-bit palettised, 565 format, or true-colour – the algorithm would not know or care.
-
Each pixel within the supply picture is there precisely as soon as within the last rotated picture. Shears are precise – they copy every pixel precisely as soon as. So subsequently the rotated model has each pixel within the supply precisely as soon as. There isn’t any duplicates, and none are eliminated.
-
Subsequently the world of the ultimate picture is completely similar. It needs to be – similar variety of pixels!
-
Subsequently there are not any issues with “aliasing” from over-sampling or under-sampling knowledge. It is a drawback with most picture manipulation – when you have a single very brilliant pixel, as you manipulate the picture, generally you miss it completely, generally you duplicate it a number of instances, and if this occurs in a different way every body, you get annoying glints. Would not occur right here – every supply pixel is all the time current precisely as soon as. (in fact you get spatial aliasing due to “the jaggies”)
-
It is completely reversible. I am truly undecided how useful that is in observe, nevertheless it’s a cool truth!
I discover it very odd watching the ultimate outcome, particularly because it rotates veeeeery slowly. Because it rotates, each pixel is all the time current – they do not seem and vanish, they simply migrate. I discover it very tough to wrap my mind round how they transfer across the display screen in circles, with out inflicting any gaps, and with out overwiritng one another. Mesmerising.
Anyway, this information might be of restricted use lately – I simply thought it was neat, and as I occurred to be engaged on a mission that has a blitter however no GPU, I remembered this and determined to implement it – that is the place the GIF above comes from.