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

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
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
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
brian-m-ops
#book-python-testing-with-pytest-second-edition Hi. Thanks for writing the book. I am just learning so this might just of been an issue ...
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
fynn
This is as much a suggestion as a question, as a note for others. Locally the SGP30 wasn’t available, so I ordered a SGP40. On page 53, ...
New
hazardco
On page 78 the following code appears: &lt;%= link_to ‘Destroy’, product, class: ‘hover:underline’, method: :delete, data: { confirm...
New
Henrai
Hi, I’m working on the Chapter 8 of the book. After I add add the point_offset, I’m still able to see acne: In the image above, I re...
New
a.zampa
@mfazio23 I’m following the indications of the book and arriver ad chapter 10, but the app cannot be compiled due to an error in the Bas...
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
axelson
I’ve been really enjoying obsidian.md: It is very snappy (even though it is based on Electron). I love that it is all local by defaul...
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
In case anyone else is wondering why Ruby 3 doesn’t show when you do asdf list-all ruby :man_facepalming: do this first: asdf plugin-upd...
New
Exadra37
Oh just spent so much time on this to discover now that RancherOS is in end of life but Rancher is refusing to mark the Github repo as su...
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
PragmaticBookshelf
Author Spotlight Jamis Buck @jamis This month, we have the pleasure of spotlighting author Jamis Buck, who has written Mazes for Prog...
New
Help
I am trying to crate a game for the Nintendo switch, I wanted to use Java as I am comfortable with that programming language. Can you use...
New
PragmaticBookshelf
Author Spotlight: VM Brasseur @vmbrasseur We have a treat for you today! We turn the spotlight onto Open Source as we sit down with V...
New
New

Sub Categories: