The Ray Tracer Challenge: Chapter 7 - Calculating pixel size

Hi. I’m working on this test:

Scenario ​: The pixel size for a horizontal canvas
Given ​ c ← camera(200, 125, π/2)
Then ​ c.pixel_size = 0.01

I’m doing this in Haskell, so I’m pulling the following from a Haskell REPL prompt that is doing the same calculations as my code:

> hs = 200
> vs = 125
> fov = (pi/2)
> 1.5707963267948966

> radians = (fov/2) * (pi/180)
> 1.3707783890401887e-2         ==> (0.013707783)
> ar = (fromIntegral hs) / (fromIntegral vs)
> 1.6
> hv = tan radians
> 1.3708642534394055e-2         ==> (0.01370864253)
> hw = calcHalfWidth ar hv
> 1.3708642534394055e-2         ==> (0.01370864253)
> (hw * 2) / (fromIntegral hs)  ==> (0.02741728506) / 200
1.3708642534394054e-4           ==> (0.00013708642)

I’m trying to track down where this is going wrong, and every time I double check my calculations, I convince myself that they are correct, even though I’m off by so much.

Does anybody see any obvious issues?

2 Likes

You are not off. 0.000137 is just another way to write 1.37e-4.

You have to read it as 1.37 * 10 ^ (-4).

It is called scientific notation.

2 Likes

Sorry, I wasn’t clear. That last calculation is the pixel size, which should equal 0.01. I was just showing the non-scientific notation values by adding the ‘==> (some number)’

2 Likes

I have confirmed the book uses only radians for angles, so after undoing the radian conversion, my answer was actually 9.999999999999998e-3. or 0.009999999. So, I had it all along, but just couldn’t see it.

That’s what a night of sleep and a helpful stranger gets you. :wink:

2 Likes