
//Walker class:
// This is a moving point along the edge of a triangle. 
// It is initialized with a staring Vertex3D, and set moving towords another 
// Vertex3D. The step() command moves it one pixel in the y-axis,
// towords its endpoint.

// slightly modified from my original Walker in proj2
//  (takes V3D's (not 2Ds) and uses general Blenders


public class Walker {

    ///FracInt quantities:
    private int y; 
    private int x;
    private int end_y;
    private int step_x;
    ///
    
    //we need to hold the end-point in case we break to another edge.
    private Vertex3D end;
    
    public Blender Blend; 
    
    //create storage:
    public Walker() {
	Blend = new Blender();
    }
    
    //initialize for a given triangle edge.
    //return true if we reach the end (short segment!)
    public final boolean init(Vertex3D start,Vertex3D end) {
	this.end = end; 
	
	//temporary FracInts:
	int x1 = FracInt.from_float(start.x);
	int y1 = FracInt.from_float(start.y);
	int x2 = FracInt.from_float(end.x);
	//this one is special -- it is an instance var.
	end_y = FracInt.from_float(end.y);

	//init the Blender
	Blend.init(start, end,(end_y - y1));
	
	//step in x foreach step down in y
	step_x = FracInt.div((x2 - x1),(end_y - y1));
	
	//find the next pixel center  -- round to integer, and add half
	y = (y1 & FracInt.INTPART) + FracInt.ONE;
	
	int y_step = y - y1;
	/// Now find the intersection of the line
	/// with that y ordinate
	if (y > end_y ) { //have we overshot?
	    return true;
	}
	//set x, so that x,y falls on the line.
	x = x1 + FracInt.mult(y_step , step_x);
	Blend.step(y_step);
	return false;
    }
    
  public final boolean break_to(Vertex3D v) {
      return init(end,v);
  }
    
    //step the walker down one pixel row. 
    //if we overshoot, return true.
    public final boolean step() {   
	y += FracInt.ONE;
	if (y > end_y) {
	    return true;
	}
	x += step_x;
	Blend.step();
	return false;
    }
    
    public final int getX() {
	return x;
    }

    public final Blender getBlend() {
	return Blend;
    }
    public final int getY() {
	return y;
    }
}
		
