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
page 37
ANTLRInputStream input = new ANTLRInputStream(is);
as of ANTLR 4 .8 should be:
CharStream stream = CharStreams.fromStream(i...
New
Running the examples in chapter 5 c under pytest 5.4.1 causes an AttributeError: ‘module’ object has no attribute ‘config’.
In particula...
New
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
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
I can’t setup the Rails source code. This happens in a working directory containing multiple (postgres) Rails apps.
With:
ruby-3.0.0
s...
New
“The ProductLive.Index template calls a helper function, live_component/3, that in turn calls on the modal component. ”
Excerpt From: Br...
New
I’m under the impression that when the reader gets to page 136 (“View Data with the Database Inspector”), the code SHOULD be able to buil...
New
The markup used to display the uploaded image results in a Phoenix.LiveView.HTMLTokenizer.ParseError error.
lib/pento_web/live/product_l...
New
When running the program in chapter 8, “Implementing Combat”, the printout Health before attack was never printed so I assumed something ...
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
Other popular topics
If it’s a mechanical keyboard, which switches do you have?
Would you recommend it? Why?
What will your next keyboard be?
Pics always w...
New
Ruby, Io, Prolog, Scala, Erlang, Clojure, Haskell. With Seven Languages in Seven Weeks, by Bruce A. Tate, you’ll go beyond the syntax—and...
New
poll
poll
Be sure to check out @Dusty’s article posted here: An Introduction to Alternative Keyboard Layouts It’s one of the best write-...
New
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
Continuing the discussion from Thinking about learning Crystal, let’s discuss - I was wondering which languages don’t GC - maybe we can c...
New
If you get Can't find emacs in your PATH when trying to install Doom Emacs on your Mac you… just… need to install Emacs first! :lol:
bre...
New
Was just curious to see if any were around, found this one:
I got 51/100:
Not sure if it was meant to buy I am sure at times the b...
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
Inside our android webview app, we are trying to paste the copied content from another app eg (notes) using navigator.clipboard.readtext ...
New
Node.js v22.14.0 has been released.
Link: Release 2025-02-11, Version 22.14.0 'Jod' (LTS), @aduh95 · nodejs/node · GitHub
New
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /ruby
- /wasm
- /erlang
- /phoenix
- /keyboards
- /python
- /js
- /rails
- /security
- /go
- /swift
- /vim
- /clojure
- /java
- /emacs
- /haskell
- /svelte
- /onivim
- /typescript
- /kotlin
- /crystal
- /c-plus-plus
- /tailwind
- /react
- /gleam
- /ocaml
- /flutter
- /elm
- /vscode
- /ash
- /html
- /opensuse
- /zig
- /centos
- /deepseek
- /php
- /scala
- /react-native
- /textmate
- /lisp
- /sublime-text
- /nixos
- /debian
- /agda
- /django
- /kubuntu
- /arch-linux
- /deno
- /nodejs
- /revery
- /ubuntu
- /spring
- /manjaro
- /lua
- /diversity
- /markdown
- /julia
- /c








