
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

As per the title, thanks.
New

Following the steps described in Chapter 6 of the book, I’m stuck with running the migration as described on page 84:
bundle exec sequel...
New

Title: Intuitive Python: docker run… denied error (page 2)
Attempted to run the docker command in both CLI and Powershell
PS C:\Users\r...
New

In case this helps anyone, I’ve had issues setting up the rails source code. Here were the solutions:
In Gemfile, change
gem 'rails'
t...
New

The book has the same “Problem space/Solution space” diagram on page 18 as is on page 17. The correct Problem/Solution space diagrams ar...
New

Title: Build a Weather Station with Elixir and Nerves: Problem connecting to Postgres with Grafana on (page 64)
If you follow the defau...
New

It seems the second code snippet is missing the code to set the current_user:
current_user: Accounts.get_user_by_session_token(session["...
New

The allprojects block listed on page 245 produces the following error when syncing gradle:
“org.gradle.api.GradleScriptException: A prob...
New

Modern front-end development for Rails, second edition - Struggling to get the first chapter to work
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

I am using Android Studio Chipmunk | 2021.2.1 Patch 2
Build #AI-212.5712.43.2112.8815526, built on July 10, 2022
Runtime version: 11.0....
New
Other popular topics

Which, if any, games do you play? On what platform?
I just bought (and completed) Minecraft Dungeons for my Nintendo Switch. Other than ...
New

@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

Curious to know which languages and frameworks you’re all thinking about learning next :upside_down_face:
Perhaps if there’s enough peop...
New

I’ve been hearing quite a lot of comments relating to the sound of a keyboard, with one of the most desirable of these called ‘thock’, he...
New

Thanks to @foxtrottwist’s and @Tomas’s posts in this thread: Poll: Which code editor do you use? I bought Onivim! :nerd_face:
https://on...
New

Just done a fresh install of macOS Big Sur and on installing Erlang I am getting:
asdf install erlang 23.1.2
Configure failed.
checking ...
New

This is going to be a long an frequently posted thread.
While talking to a friend of mine who has taken data structure and algorithm cou...
New

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

I’m able to do the “artistic” part of game-development; character designing/modeling, music, environment modeling, etc.
However, I don’t...
New

Get the comprehensive, insider information you need for Rails 8 with the new edition of this award-winning classic.
Sam Ruby @rubys
...
New
Latest in The Ray Tracer Challenge
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /ruby
- /wasm
- /erlang
- /phoenix
- /keyboards
- /rails
- /python
- /js
- /security
- /go
- /swift
- /vim
- /clojure
- /emacs
- /haskell
- /java
- /onivim
- /typescript
- /svelte
- /kotlin
- /crystal
- /c-plus-plus
- /tailwind
- /react
- /gleam
- /ocaml
- /elm
- /flutter
- /vscode
- /ash
- /html
- /opensuse
- /centos
- /php
- /deepseek
- /zig
- /scala
- /sublime-text
- /lisp
- /textmate
- /nixos
- /debian
- /react-native
- /agda
- /kubuntu
- /arch-linux
- /django
- /ubuntu
- /revery
- /spring
- /manjaro
- /nodejs
- /diversity
- /lua
- /deno
- /julia
- /slackware
- /c