def hit(self, r: Ray, t_min: float, t_max: float):
        oc = r.origin() - self.center
        a = np.dot(r.direction(), r.direction())
        b = np.dot(oc, r.direction())
        c = np.dot(oc, oc) - self.radius * self.radius
        discriminant = b * b - a * c

        if discriminant > 0:
            temp = (-b - sqrt(b * b - a * c)) / a
            if (temp < t_max) and (temp > t_min):
                t = temp
                p = r.point_at_parameter(t)
                normal = (p - self.center) / self.radius
                rec = hit_record(t, p, normal, self.material)
                return True, rec

            temp = (-b + sqrt(b * b - a * c)) / a
            if (temp < t_max) and (temp > t_min):
                t = temp
                p = r.point_at_parameter(t)
                normal = (p - self.center) / self.radius
                rec = hit_record(t, p, normal, self.material)
                return True, rec

        return False, None
Exemple #2
0
    def hit(self, r: Ray, t_min: float, t_max: float):
        t: float = (self.k - r.origin()[2]) / r.direction()[2]

        if (t < t_min) or (t > t_max):
            return False, None
        x: float = r.origin()[0] + t * r.direction()[0]
        y: float = r.origin()[1] + t * r.direction()[1]

        if (x < self.x0) or (x > self.x1) or (y < self.y0) or (y > self.y1):
            return False, None

        u = (x - self.x0) / (self.x1 - self.x0)
        v = (y - self.y0) / (self.y1 - self.y0)
        p = r.point_at_parameter(t)
        normal = np.array((0, 0, 1))

        return True, hit_record(t, p, normal, self.material, u, v)
    def hit(self, r: Ray, tmin: float, tmax: float):
        for a in range(3):
            invD = 1.0 / r.direction()[a]

            t0 = (self._min[a] - r.origin()[a]) * invD

            t1 = (self._max[a] - r.origin()[a]) * invD

            if invD < 0.0:
                aux = t1
                t1 = t0
                t0 = aux

            tmin = t0 if t0 > tmin else tmin
            tmax = t1 if t1 < tmax else tmax

            if tmax <= tmin:
                return False

        return True
Exemple #4
0
    def hit(self, ray: Ray, t_min: float, t_max: float, rec: hit_record):
        oc = ray.origin() - self.center
        a = Vector3.dot(ray.direction(), ray.direction())
        b = Vector3.dot(oc, ray.direction())
        c = Vector3.dot(oc, oc) - self.radius * self.radius
        discriminant = b * b - a * c

        if (discriminant > 0):
            temp = (-b - math.sqrt(discriminant)) / a
            if (temp < t_max and temp > t_min):
                rec.t = temp
                rec.p = ray.point_at_parameter(rec.t)
                rec.normal = (rec.p - self.center).scalar_div(self.radius)
                return True
            temp = (-b + math.sqrt(discriminant)) / a
            if (temp < t_max and temp > t_min):
                rec.t = temp
                rec.p = ray.point_at_parameter(rec.t)
                rec.normal = (rec.p - self.center).scalar_div(self.radius)
                return True

        return False