Skip to content

Commit

Permalink
Fix test in sphere (hit)
Browse files Browse the repository at this point in the history
  • Loading branch information
Miou-zora committed Apr 19, 2024
1 parent 2aa6ef5 commit 9d4f4f6
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/Sphere.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ pub const Sphere = struct {
radius: f32,

pub fn hits(self: *const Self, ray: Ray) HitRecord {
const a: f32 =
std.math.pow(f32, ray.direction.x, 2) +
std.math.pow(f32, ray.direction.y, 2) +
std.math.pow(f32, ray.direction.z, 2);
const a: f32 = ray.direction.dot(ray.direction);
const b: f32 =
2 * (ray.direction.x * (ray.origin.x - self.center.x) +
ray.direction.y * (ray.origin.y - self.center.y) +
Expand Down Expand Up @@ -60,12 +57,18 @@ test "hit" {
.radius = 1,
};
const ray = Ray{
.origin = Pt3{ .x = 0, .y = 0, .z = 0 },
.direction = Pt3{ .x = 0, .y = 0, .z = 1 },
.origin = Pt3{ .x = 0, .y = 0, .z = 2 },
.direction = Pt3{ .x = 0, .y = 0, .z = -1 },
};

const hit = sphere.hits(ray);
try std.testing.expect(hit);
try std.testing.expect(hit.hit);
try std.testing.expect(hit.intersection_point.x == 0);
try std.testing.expect(hit.intersection_point.y == 0);
try std.testing.expect(hit.intersection_point.z == 1);
try std.testing.expect(hit.normal.x == 0);
try std.testing.expect(hit.normal.y == 0);
try std.testing.expect(hit.normal.z == 1);
}

test "dontHit" {
Expand All @@ -79,7 +82,7 @@ test "dontHit" {
};

const hit = sphere.hits(ray);
try std.testing.expect(!hit);
try std.testing.expect(!hit.hit);
}

test "limit" {
Expand All @@ -88,12 +91,18 @@ test "limit" {
.radius = 1,
};
const ray = Ray{
.origin = Pt3{ .x = 0, .y = -1, .z = 0 },
.origin = Pt3{ .x = 0, .y = -1, .z = -1 },
.direction = Pt3{ .x = 0, .y = 0, .z = 1 },
};

const hit = sphere.hits(ray);
try std.testing.expect(hit);
try std.testing.expect(hit.hit);
try std.testing.expect(hit.intersection_point.x == 0);
try std.testing.expect(hit.intersection_point.y == -1);
try std.testing.expect(hit.intersection_point.z == 0);
try std.testing.expect(hit.normal.x == 0);
try std.testing.expect(hit.normal.y == -1);
try std.testing.expect(hit.normal.z == 0);
}

test {
Expand Down

0 comments on commit 9d4f4f6

Please sign in to comment.