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

Where Next?

Popular Pragmatic Bookshelf topics Top

jimschubert
In Chapter 3, the source for index introduces Config on page 31, followed by more code including tests; Config isn’t introduced until pag...
New
iPaul
page 37 ANTLRInputStream input = new ANTLRInputStream(is); as of ANTLR 4 .8 should be: CharStream stream = CharStreams.fromStream(i...
New
mikecargal
Title: Hands-On Rust (Chapter 11: prefab) Just played a couple of amulet-less games. With a bit of debugging, I believe that your can_p...
New
herminiotorres
Hi @Margaret , On page VII the book tells us the example and snippets will be all using Elixir version 1.11 But on page 3 almost the en...
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
jgchristopher
“The ProductLive.Index template calls a helper function, live_component/3, that in turn calls on the modal component. ” Excerpt From: Br...
New
davetron5000
Hello faithful readers! If you have tried to follow along in the book, you are asked to start up the dev environment via dx/build and ar...
New
dachristenson
I just bought this book to learn about Android development, and I’m already running into a major issue in Ch. 1, p. 20: “Update activity...
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

AstonJ
A thread that every forum needs! Simply post a link to a track on YouTube (or SoundCloud or Vimeo amongst others!) on a separate line an...
New
AstonJ
SpaceVim seems to be gaining in features and popularity and I just wondered how it compares with SpaceMacs in 2020 - anyone have any thou...
New
AstonJ
We have a thread about the keyboards we have, but what about nice keyboards we come across that we want? If you have seen any that look n...
New
PragmaticBookshelf
“Finding the Boundaries” Hero’s Journey with Noel Rappin @noelrappin Even when you’re ultimately right about what the future ho...
New
Margaret
Hello content creators! Happy new year. What tech topics do you think will be the focus of 2021? My vote for one topic is ethics in tech...
New
gagan7995
API 4 Path: /user/following/ Method: GET Description: Returns the list of all names of people whom the user follows Response [ { ...
New
PragmaticBookshelf
Author Spotlight Jamis Buck @jamis This month, we have the pleasure of spotlighting author Jamis Buck, who has written Mazes for Prog...
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
First poster: bot
Large Language Models like ChatGPT say The Darnedest Things. The Errors They MakeWhy We Need to Document Them, and What We Have Decided ...
New
AstonJ
Curious what kind of results others are getting, I think actually prefer the 7B model to the 32B model, not only is it faster but the qua...
New

Sub Categories: