public void Intersects(ref Ray ray, out Scalar result) { if (Contains(ray.Origin)== ContainmentType.Contains) { result = 0; return; } Scalar distance; Scalar intersectValue; result = -1; if (ray.Origin.X < Min.X && ray.Direction.X > 0) { distance = (Min.X - ray.Origin.X) / ray.Direction.X; if (distance > 0) { intersectValue = ray.Origin.Y + ray.Direction.Y * distance; if (intersectValue >= Min.Y && intersectValue <= Max.Y && (result == -1 || distance < result)) { result = distance; } } } if (ray.Origin.X > Max.X && ray.Direction.X < 0) { distance = (Max.X - ray.Origin.X) / ray.Direction.X; if (distance > 0) { intersectValue = ray.Origin.Y + ray.Direction.Y * distance; if (intersectValue >= Min.Y && intersectValue <= Max.Y && (result == -1 || distance < result)) { result = distance; } } } if (ray.Origin.Y < Min.Y && ray.Direction.Y > 0) { distance = (Min.Y - ray.Origin.Y) / ray.Direction.Y; if (distance > 0) { intersectValue = ray.Origin.X + ray.Direction.X * distance; if (intersectValue >= Min.X && intersectValue <= Max.X && (result == -1 || distance < result)) { result = distance; } } } if (ray.Origin.Y > Max.Y && ray.Direction.Y < 0) { distance = (Max.Y - ray.Origin.Y) / ray.Direction.Y; if (distance > 0) { intersectValue = ray.Origin.X + ray.Direction.X * distance; if (intersectValue >= Min.X && intersectValue <= Max.X && (result == -1 || distance < result)) { result = distance; } } } }
public Scalar Intersects(Ray ray) { Scalar result; Intersects(ref ray, out result); return result; }