### Post by sbehnke on Jul 5, 2019 1:26:03 GMT

`Scenario: The refracted color with a refracted ray`

Given w ← default_world()

And A ← the first object in w

And A has:

| material.ambient | 1.0 |

| material.pattern | test_pattern() |

And B ← the second object in w

And B has:

| material.transparency | 1.0 |

| material.refractive_index | 1.5 |

And r ← ray(point(0, 0, 0.1), vector(0, 1, 0))

And xs ← intersections(-0.9899:A, -0.4899:B, 0.4899:B, 0.9899:A)

When comps ← prepare_computations(xs[2], r, xs)

And c ← refracted_color(w, comps, 5)

Then c = color(0, 0.99888, 0.04725)

I'm having trouble getting this test to pass.

` let w = World.defaultWorld()`

let A = w.objects.first!

A.material.ambient = 1.0

A.material.pattern = TestPattern()

let B = w.objects.last!

B.material.transparency = 1.0

B.material.refractiveIndex = 1.5

let r = Ray(origin: .Point(x: 0, y: 0, z: 0.1), direction: .Vector(x: 0, y: 1, z: 0))

let xs = [Intersection(t: -0.9899, object: A),

Intersection(t: -0.4899, object: B),

Intersection(t: 0.4899, object: B),

Intersection(t: 0.9899, object: A)]

let comps = xs[2].prepareCopmutation(ray: r, xs: xs)

let c = w.refractedColor(computation: comps, remaining: 5)

XCTAssertEqual(c, Color(r: 0, g: 0.99888, b: 0.04725))

My end result is:

`XCTAssertEqual failed: ("Color(backing: [0.5, 0.5, 0.5])") is not equal to ("Color(backing: [0.0, 0.99888, 0.04725])")`

That seems really odd. I'm stepping through the recursion but I'm really sure where it is going sideways.

` static func refractedColor(world: World, computation: Computation, remaining: Int = MaximumRecursionDepth) -> Color {`

// Maximum recursion met, return black

if remaining < 1 || computation.object!.material.transparency == 0 {

return Color.black

}

// Check for total internal reflection with snells' law, return black

let nRatio = Double(computation.n1 / computation.n2)

let cosI = Tuple.dot(lhs: computation.eyeVector, rhs: computation.normalVector)

let sin2T = (nRatio * nRatio) * (1 - (cosI * cosI))

if sin2T > 1 {

return Color.black

}

let cosT = sqrt(1 - sin2T)

let direction = computation.normalVector * (nRatio * cosI - cosT) - computation.eyeVector * nRatio

let refractedRay = Ray(origin: computation.underPoint, direction: direction)

let color = colorAt(world: world, ray: refractedRay, remaining: remaining - 1) * computation.object!.material.transparency

return color

}

Any help would be greatly appreciated! Loving the book!