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
Title: Web Development with Clojure, Third Edition, vB17.0 (p9)
The create table guestbook syntax suggested doesn’t seem to be accepted ...
New
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
I think I might have found a problem involving SwitchCompat, thumbTint, and trackTint.
As entered, the SwitchCompat changes color to hol...
New
I’m a newbie to Rails 7 and have hit an issue with the bin/Dev script mentioned on pages 112-113.
Iteration A1 - Seeing the list of prod...
New
Hi,
I am getting an error I cannot figure out on my test.
I have what I think is the exact code from the book, other than I changed “us...
New
Book: Programming Phoenix LiveView, page 142 (157/378), file lib/pento_web/live/product_live/form_component.ex, in the function below:
d...
New
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
@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
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
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
Other popular topics
New
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
The V Programming Language
Simple language for building maintainable programs
V is already mentioned couple of times in the forum, but I...
New
Hello everyone! This thread is to tell you about what authors from The Pragmatic Bookshelf are writing on Medium.
New
Author Spotlight
Dmitry Zinoviev
@aqsaqal
Today we’re putting our spotlight on Dmitry Zinoviev, author of Data Science Essentials in ...
New
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
Jan | Rethink the Computer.
Jan turns your computer into an AI machine by running LLMs locally on your computer. It’s a privacy-focus, l...
New
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
Get the comprehensive, insider information you need for Rails 8 with the new edition of this award-winning classic.
Sam Ruby @rubys
...
New
This is cool!
DEEPSEEK-V3 ON M4 MAC: BLAZING FAST INFERENCE ON APPLE SILICON
We just witnessed something incredible: the largest open-s...
New
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /python
- /js
- /rails
- /security
- /go
- /swift
- /vim
- /clojure
- /java
- /emacs
- /haskell
- /svelte
- /onivim
- /typescript
- /kotlin
- /c-plus-plus
- /crystal
- /tailwind
- /react
- /gleam
- /ocaml
- /elm
- /flutter
- /vscode
- /ash
- /html
- /opensuse
- /zig
- /centos
- /deepseek
- /php
- /scala
- /react-native
- /lisp
- /sublime-text
- /textmate
- /nixos
- /debian
- /agda
- /django
- /kubuntu
- /deno
- /arch-linux
- /nodejs
- /ubuntu
- /revery
- /manjaro
- /spring
- /lua
- /diversity
- /julia
- /markdown
- /slackware








