blob: 93b17d6b4e6ffd476fc8edc92432a3fce6e843cd (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System;
internal class Sphere : SceneObject
{
public Vector Center;
public float Radius;
public Sphere(Vector center, double radius, Surface surface) : base(surface) { Center = center; Radius = (float)radius; }
public override ISect Intersect(Ray ray)
{
Vector eo = Vector.Minus(Center, ray.Start);
float v = Vector.Dot(eo, ray.Dir);
float dist;
if (v < 0)
{
dist = 0;
}
else
{
double disc = Math.Pow(Radius, 2) - (Vector.Dot(eo, eo) - Math.Pow(v, 2));
dist = disc < 0 ? 0 : v - (float)Math.Sqrt(disc);
}
if (dist == 0) return ISect.Null;
return new ISect(this, ray, dist);
}
public override Vector Normal(Vector pos)
{
return Vector.Norm(Vector.Minus(pos, Center));
}
}
|