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

brianokken
Many tasks_proj/tests directories exist in chapters 2, 3, 5 that have tests that use the custom markers smoke and get, which are not decl...
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
sdmoralesma
Title: Web Development with Clojure, Third Edition - migrations/create not working: p159 When I execute the command: user=&gt; (create-...
New
HarryDeveloper
Hi @venkats, It has been mentioned in the description of ‘Supervisory Job’ title that 2 things as mentioned below result in the same eff...
New
adamwoolhether
When trying to generate the protobuf .go file, I receive this error: Unknown flag: --go_opt libprotoc 3.12.3 MacOS 11.3.1 Googling ...
New
s2k
Hi all, currently I wonder how the Tailwind colours work (or don’t work). For example, in app/views/layouts/application.html.erb I have...
New
EdBorn
Title: Agile Web Development with Rails 7: (page 70) I am running windows 11 pro with rails 7.0.3 and ruby 3.1.2p20 (2022-04-12 revision...
New
bjnord
Hello @herbert ! Trying to get the very first “Hello, Bracket Terminal!" example to run (p. 53). I develop on an Amazon EC2 instance runn...
New
mcpierce
@mfazio23 I’ve applied the changes from Chapter 5 of the book and everything builds correctly and runs. But, when I try to start a game,...
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

Other popular topics Top

AstonJ
Or looking forward to? :nerd_face:
New
PragmaticBookshelf
Design and develop sophisticated 2D games that are as much fun to make as they are to play. From particle effects and pathfinding to soci...
New
DevotionGeo
I know that -t flag is used along with -i flag for getting an interactive shell. But I cannot digest what the man page for docker run com...
New
AstonJ
Curious to know which languages and frameworks you’re all thinking about learning next :upside_down_face: Perhaps if there’s enough peop...
New
AstonJ
There’s a whole world of custom keycaps out there that I didn’t know existed! Check out all of our Keycaps threads here: https://forum....
New
AstonJ
I have seen the keycaps I want - they are due for a group-buy this week but won’t be delivered until October next year!!! :rofl: The Ser...
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
PragmaticBookshelf
A Hero’s Journey with Chris Pine @chrispine Chris Pine, author of Learn to Program, Third Edition, discusses his journey to beco...
New
AstonJ
If you’re getting errors like this: psql: error: connection to server on socket “/tmp/.s.PGSQL.5432” failed: No such file or directory ...
New
AstonJ
This is cool! DEEPSEEK-V3 ON M4 MAC: BLAZING FAST INFERENCE ON APPLE SILICON We just witnessed something incredible: the largest open-s...
New

Latest in PragProg

View all threads ❯