package renderer.primitive;

import basics.math.algebra.Ray;
import basics.math.algebra.Vector;

public class Disk implements Primitive {

    private double offset;
    private double radius;
    private Vector normal;

    public Disk(Vector normal, double offset, double radius) {
        this.offset = offset;
        this.radius = radius;
        this.normal = normal;
    }

    @Override
    public double intersect(Ray ray) {
        double distance = -(ray.getOrigin().dot(normal) + offset) / ray.getDirection().dot(normal);
        Vector intersection = ray.travel(distance);

        if (intersection.dot(intersection) > this.radius * this.radius) {
            return -1.0;
        }

        return distance;
    }

    @Override
    public Vector normalAt(Vector surfacePoint, Vector incoming) {
        return normal.scale(-Math.signum(normal.dot(incoming)));
    }
}
