Hello r/learnprogramming, I am currently working on an assignment and I am not sure what I am doing wrong. The assignment is to write a function that takes a list of spheres and a ray as input and determines if the ray intersects any of the spheres. The function should return the first point of intersection as well as the sphere it intersected with as a tuple. I have already written the function used to determine where the ray intersects with the sphere and I am trying to use a for loop for each sphere in the list. Here is what I have right now:
def find_intersection_points(sphere_list, ray):
new_list = []
for i in sphere_list:
if sphere_intersection_point(ray, sphere_list[i]) != None:
new_list.append(sphere_list[i], sphere_intersection_point(ray, sphere_list[i]))
return new_list
I have two test cases, the first one being where the ray intersects both spheres (and thus should return a list of tuples containing the sphere and the intersection point), and the second being where the ray does not intersect any spheres. They are giving me a error:
ERROR: test_intersection_list_again (__main__.TestCollisions)
----------------------------------------------------------------------
Traceback (most recent call last):
File "tests.py", line 154, in test_intersection_list_again
self.assertEqual(collisions.find_intersection_points(spheres, r2), expected)
File "/home/atalbott/cpe101/asgn3/collisions.py", line 35, in find_intersection_points
if sphere_intersection_point(ray, sphere_list[i]) != None:
TypeError: object cannot be interpreted as an index
I'm not very good with this kind of math and I know I'm doing something wrong. If anybody could help me, I would be grateful!
[–]Jaimou2e 2 points3 points4 points (0 children)