2 posts tagged “maths”
I need to calculate the probability that the pips on x D6 will add up to y. Anyone got any ideas?
Currently I am writing a spinner control for a program I am writing, much like a fruit machine. The simplest way to achieve this effect is to have the graphics scroll past, perhaps with a bit of shading to create the illusion of 3D. However, I thought it would be interesting to try and take it one step further and actually warp the image so that you get the foreshortening effect that you see when looking at a cylinder face on.
Notice how, despite being equal distances apart on the surface of the cylinder, the dotted lines appear closer together towards the edge.
My first attempt at creating this effect was to calculate what row of pixels of the source image would be visible at a given position on the target image.
The second row of pixels in the target image corresponds to the Cth pixel of the source image when wrapped around the theoretical cylinder.
To calculate the Cth pixel we must employ some simple triggernomics
Assuming
the texture begins at the north side of the cylinder, and given x
as the row of the target image and r as the radius of
the cylinder, in order to calculate C
we must first know θ.
By
bisecting the triangle at the sample line we can create two right
angle triangles, greatly simplifying the triggernomics needed.
We
can then calculate θ.
Now
we know θ
we can calculate the length of the arc C like so:
We know that r must be half the height of the target image, so we can replace r with h/2 where h is the height of the target image.
Of course this means that the source image must be larger than the target image. We can see this by finding the hth row. Let's assume h is 20.
The source image must be about 1.5 times taller than the target image. it is usually quite difficult to produce images that are correctly stretched taller, and it is just as easy to scale C to fit a source image the same size as the target image. To do this just multiply the resultant value by 2/π to give the general formula:
I knocked up a quick test to see how well it works, and the results are okay. The only trouble is, even when caching the row mappings (they will never change while the size is the same), it takes too long for a real time animation. I've got some other optimisations to try, which will hopefully make it acceptable.
Above is the results on a test image I am using.
On the left is the original image, a grid with a black-white linear gradient.
The middle has had the general algorithm posted here applied to it. As you can see, there is quite a bit of artifacting. This isn't so obvious on an actual image, but can be seen here clearly on this grid.
The far right employs some basic interpolating. If a C value falls between two rows it interpolates between the two rows the C value falls between. This produces a much more accurate image, but still with some artifacting. A more sophisticated interpolation algorithm could probably remove most of this.
