GwieF.com : project.GrieF.com : Classes : Cylinder
logo

Cylinder.java


package com.gwief.jTrace;

/*
    Cylinder.java by Damian Newport
    uses Quadric.java to implement a cylinder centered around (0,0,0)
    with height 1, diameter 1
*/

public class Cylinder{

    public static final double[] intersect(Ray i){
        double[] box = Box.intersect(i);
        // if we miss the box
        if (box.length == 0){
            return new double[0];
        }

        /* if we got here we hit the box - lets test the cylinder */
                                          /* A, B, C, J */
        double[] cyl = Quadric.intersect(i, 4, 0, 4, -1);
        if (cyl.length == 0){ /* if we miss the cylinder */
            return new double[0];
        }

        /* if we get here we've scored */
        /* now just have to work out where */
        if (cyl[0] > box[1]){
            return new double[0];
        }
        if (box[0] > cyl[1]){
            return new double[0];
        }
        if (box[0] < cyl[0]){
            box[0] = cyl[0];
        }
        if (cyl[1] < box[1]){
            box[1] = cyl[1];
        }
        return box;
    }
    
    public static final Vector3D normal(Vector3D a){
        
        /* if it intersects with the box bit */
        
        if ((0.5-a.y) < rayTrace.TINY){
            return new Vector3D(0,1,0);
        }
        if ((0.5+a.y) < rayTrace.TINY){
            return new Vector3D(0,-1,0);
        }
        
        /* else it hits the cylinder */
                              /* A, B, C,  J */
        return Quadric.normal(a, 4, 0, 4, -1);
    }
}