GwieF.com : Project : Classes : Intersection
logo

Intersection.java


package com.gwief.jTrace;

/*
    Intersection.java by Damian Newport
    Holds a ray/shape intersection and implements nessessary behavior
*/

public class Intersection {

    static int count = 0;
    
    int type;       /* 1 = start edge, -1 = end edge */
    int stype;      /* hold the original type value */
    double value;   /* distance along ray */
    Shape3D hit;    /* stores shape hit */
        
    public Intersection() {
        type = 0;
        value = 0f;
    }

    public Intersection(int a ,double b,Shape3D shape){
        type = a;
        value = b;
        hit = shape;
        
        stype = a;  /* so we know wether the normal shoudl be reversed */
    }
    
    public static Intersection[] mergeIntersections(Intersection[] setA, Intersection[] setB){
        int counterA = 0;
        int counterB = 0;
        int lengthA = setA.length;
        int lengthB = setB.length;
        Intersection[] sorted = new Intersection[lengthA+lengthB];

        if((lengthA+lengthB) == 0){ return sorted; }
        if(lengthA==0){ return setB; }
        if(lengthB==0){ return setA; }

        for(int i = 0; ((counterA < lengthA) && (counterB < lengthB)) ;i++){
            
            if(setA[counterA].value <= setB[counterB].value)
                sorted[i] = setA[counterA++];
            else
                sorted[i] = setB[counterB++];
        }
        if(counterB < lengthB){
            while(counterB < lengthB){
                sorted[counterB +lengthA] = setB[counterB];
                counterB++;
            }
        }else{
            while(counterA < lengthA){
                sorted[counterA +lengthB] = setA[counterA];
                counterA++;
            }
        }
                
        return sorted;
    }
}