doja

doja

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

Popular Pragmatic Bookshelf topics Top

kuroneko
Whilst the author has been careful to provide exact results for the tests elsewhere in the book (such as surds with the transformation te...
New
johnp
Running the examples in chapter 5 c under pytest 5.4.1 causes an AttributeError: ‘module’ object has no attribute ‘config’. In particula...
New
jeffmcompsci
Title: Design and Build Great Web APIs - typo “https://company-atk.herokuapp.com/2258ie4t68jv” (page 19, third bullet in URL list) Typo:...
New
edruder
I thought that there might be interest in using the book with Rails 6.1 and Ruby 2.7.2. I’ll note what I needed to do differently here. ...
New
jeremyhuiskamp
Title: Web Development with Clojure, Third Edition, vB17.0 (p9) The create table guestbook syntax suggested doesn’t seem to be accepted ...
New
AndyDavis3416
@noelrappin Running the webpack dev server, I receive the following warning: ERROR in tsconfig.json TS18003: No inputs were found in c...
New
brunogirin
When running tox for the first time, I got the following error: ERROR: InterpreterNotFound: python3.10 I realised that I was running ...
New
akraut
The markup used to display the uploaded image results in a Phoenix.LiveView.HTMLTokenizer.ParseError error. lib/pento_web/live/product_l...
New
andreheijstek
After running /bin/setup, the first error was: The foreman' command exists in these Ruby versions: That was easy to fix: gem install fore...
New
dachristenson
I’ve got to the end of Ch. 11, and the app runs, with all tabs displaying what they should – at first. After switching around between St...
New

Other popular topics Top

PragmaticBookshelf
Rust is an exciting new programming language combining the power of C with memory safety, fearless concurrency, and productivity boosters...
New
AstonJ
Just done a fresh install of macOS Big Sur and on installing Erlang I am getting: asdf install erlang 23.1.2 Configure failed. checking ...
New
AstonJ
I ended up cancelling my Moonlander order as I think it’s just going to be a bit too bulky for me. I think the Planck and the Preonic (o...
New
AstonJ
Do the test and post your score :nerd_face: :keyboard: If possible, please add info such as the keyboard you’re using, the layout (Qw...
New
Rainer
Not sure if following fits exactly this thread, or if we should have a hobby thread… For many years I’m designing and building model air...
New
AstonJ
Seems like a lot of people caught it - just wondered whether any of you did? As far as I know I didn’t, but it wouldn’t surprise me if I...
New
rustkas
Intensively researching Erlang books and additional resources on it, I have found that the topic of using Regular Expressions is either c...
New
Maartz
Hi folks, I don’t know if I saw this here but, here’s a new programming language, called Roc Reminds me a bit of Elm and thus Haskell. ...
New
First poster: bot
The overengineered Solution to my Pigeon Problem. TL;DR: I built a wifi-equipped water gun to shoot the pigeons on my balcony, controlle...
New
PragmaticBookshelf
A Ruby-Centric Chat with Noel Rappin @noelrappin Once you start noodling around with Ruby you quickly figure out, as Noel Rappi...
New