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 Prag Prog topics Top

jeffmcompsci
Title: Design and Build Great Web APIs - typo “https://company-atk.herokuapp.com/2258ie4t68jv” (page 19, third bullet in URL list) Typo:...
New
jesse050717
Title: Web Development with Clojure, Third Edition, pg 116 Hi - I just started chapter 5 and I am stuck on page 116 while trying to star...
New
lirux
Hi Jamis, I think there’s an issue with a test on chapter 6. I own the ebook, version P1.0 Feb. 2019. This test doesn’t pass for me: ...
New
raul
Page 28: It implements io.ReaderAt on the store type. Sorry if it’s a dumb question but was the io.ReaderAt supposed to be io.ReadAt? ...
New
conradwt
First, the code resources: Page 237: rumbl_umbrella/apps/rumbl/mix.exs Note: That this file is missing. Page 238: rumbl_umbrella/app...
New
New
curtosis
Running mix deps.get in the sensor_hub directory fails with the following error: ** (Mix) No SSH public keys found in ~/.ssh. An ssh aut...
New
nicoatridge
Hi, I have just acquired Michael Fazio’s “Kotlin and Android Development” to learn about game programming for Android. I have a game in p...
New
brunogirin
When trying to run tox in parallel as explained on page 151, I got the following error: tox: error: argument -p/–parallel: expected one...
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

Other popular topics Top

AstonJ
What chair do you have while working… and why? Is there a ‘best’ type of chair or working position for developers?
New
wolf4earth
@AstonJ prompted me to open this topic after I mentioned in the lockdown thread how I started to do a lot more for my fitness. https://f...
New
Rainer
My first contact with Erlang was about 2 years ago when I used RabbitMQ, which is written in Erlang, for my job. This made me curious and...
New
dimitarvp
Small essay with thoughts on macOS vs. Linux: I know @Exadra37 is just waiting around the corner to scream at me “I TOLD YOU SO!!!” but I...
New
DevotionGeo
The V Programming Language Simple language for building maintainable programs V is already mentioned couple of times in the forum, but I...
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
AstonJ
We’ve talked about his book briefly here but it is quickly becoming obsolete - so he’s decided to create a series of 7 podcasts, the firs...
New
PragmaticBookshelf
Author Spotlight James Stanier @jstanier James Stanier, author of Effective Remote Work , discusses how to rethink the office as we e...
New
husaindevelop
Inside our android webview app, we are trying to paste the copied content from another app eg (notes) using navigator.clipboard.readtext ...
New
PragmaticBookshelf
Author Spotlight: Bruce Tate @redrapids Programming languages always emerge out of need, and if that’s not always true, they’re defin...
New

Latest in PragProg

View all threads ❯