# The Ray Tracer Challenge: Cylinder test #7 (p185) - failed corner cases

Hi,

I’m stuck on the test for #7 “Intersecting the caps of a closed cylinder”.
Tests 3 & 5 are failing as the intersections count is equal to one (one the cap intersection is detected).

After staring at the code for some time, any help is appreciated!

``````override internal func shapeSpecificIntersection(transformedRay:Ray<T>) -> Array<Intersection<T>> {
var result = Array<Intersection<T>>()

let a = transformedRay.direction.x * transformedRay.direction.x + transformedRay.direction.z * transformedRay.direction.z
let b = 2 * transformedRay.origin.x * transformedRay.direction.x +
2 * transformedRay.origin.z * transformedRay.direction.z
if (a.magnitude < Self.defaultEpsilon || b.magnitude < Self.defaultEpsilon) {
return intersectCaps(ray: transformedRay)
}

let c = transformedRay.origin.x * transformedRay.origin.x + transformedRay.origin.z * transformedRay.origin.z - 1

let disc = b * b - 4 * a * c

if (disc < 0) {
return intersectCaps(ray: transformedRay)
}

var t0 = (-b - sqrt(disc)) / (2 * a)
var t1 = (-b + sqrt(disc)) / (2 * a)

if t0 > t1 {
let tmp = t1
t1 = t0
t0 = tmp
}

let y0 = transformedRay.origin.y + t0 * transformedRay.direction.y
if minimum < y0 && y0 < maximum {
result.append(Intersection<T>(shape: self, t: t0))
}

let y1 = transformedRay.origin.y + t1 * transformedRay.direction.y
if minimum < y1 && y1 < maximum {
result.append(Intersection<T>(shape: self, t: t1))
}

result.append(contentsOf: intersectCaps(ray: transformedRay))

return result
}

func checkCap(ray: Ray<T>, t: T) -> Bool {
let x = ray.origin.x + t * ray.direction.x
let z = ray.origin.z + t * ray.direction.z

return (x * x) + (z * z) <= 1
}

func intersectCaps(ray: Ray<T>) -> Array<Intersection<T>> {
var result = Array<Intersection<T>>()

if !closed {
return result
}

var t = (minimum - ray.origin.y) / ray.direction.y
if (checkCap(ray: ray, t: t)) {
result.append(Intersection<T>(shape: self, t: t))
}

t = (maximum - ray.origin.y) / ray.direction.y
if (checkCap(ray:ray, t: t)) {
result.append(Intersection<T>(shape: self, t: t))
}

return result
}
``````

Best regards & happy coding! doja