//AmpAppletLogic.java

import java.awt.*;

class AmpAppletLogic extends AppletLogic {
	//independent values.
	static double Rb = 10000.0;		//150000;	//150k
	static double Rc = 10000.0;	//5000;		
	static double Vcc = 5.0;
	static double belta = 60;	
	static double Vbe = 0.71;	//0.71v

	//dependent values.
	static double Ic_yAxis;
	static double Vce_xAxis;
	
	static int noWaves = 2;
	static IntRange[] intRg_time, intRg;
	static FloatRange[] floatRg_time, floatRg;
	
	static double period;
	static double halfPeriod;
	static double[] amp; 

	static double[] timeStep;
	static double[] yAmp;
	private static double[] cur_time;
	private static int count=0;
	
	//the scale factor between the moving waveforms for the circuit and the curves
	//the size of the waveform for the circuit is half of that of the curves
	//if it is 2.0 and so on
	static double scaleFactor = 2.0;
	
	static Point up_Left, down_Right;//the screen value without shift(translation)
	
	//how par the operating point away from Vcc/Rc point.
	//fractal = Oper.x/Vcc.x
	//default = 0.5; operating point is in the middle.
	static double fractal = 0.5;	
	static Point operPt;
	static double oper_X = Vcc*fractal, oper_Y=Vcc/Rc*fractal;
	static double originX;
	
	
	static double Vinpp, Voutpp;
//	static boolean moving = false;
	
	//the distort flags for the output waveforms.
//	static boolean leftDistort = false;
//	static boolean rightDistort = false;
	
	
	public static void init(){
		set_Vce_xAxis(Vcc);
		set_Ic_yAxis(Vcc, Rc);
	
		period = 85;//100.0;
		halfPeriod = period/2.0;
		intRg_time = new IntRange[noWaves];
		floatRg_time = new FloatRange[noWaves];
		for(int i=0; i<noWaves; i++){
			intRg_time[i] = new IntRange(0,(int)period);
			floatRg_time[i] = new FloatRange(0.0,(int)period);
		}
		
		timeStep = new double[noWaves];
		cur_time = new double[noWaves];
		for(int i=0; i<noWaves; i++)
			timeStep[i] = 2*period/period; 
			
		amp = new double[noWaves];
		amp[0] = Vcc/Rc/2;
		
		yAmp = new double[noWaves+2];
		
//		yAmp[1] = 3;
	}

	static void nextStep(){
		nextStep_0();
		nextStep_1();
		nextStep_1_forCircuit();
	}
	static void nextStep_0(){
		count++;
		cur_time[0] += timeStep[0]; 
	
//		if(!moving)
			yAmp[0] = amp[0]/2*Math.sin(cur_time[0]/period*2*Math.PI);	
			yAmp[2] = yAmp[0]/scaleFactor;
//		else yAmp[0] = -1E4;
	}
	static void nextStep_1(){
//		if(!moving)
			yAmp[1] = Ic_get_Vce(-yAmp[0]+ oper_Y)+originX;
//		else yAmp[1] = -1E4;
	}
	static void nextStep_1_forCircuit(){
			yAmp[3] = -(Ic_get_Vce(-yAmp[0]+ oper_Y)-oper_X)/scaleFactor;	
	}
	public static double Ic_get_Vce(double Ic){
		double fac = -Rc;//(Vcc-0)/(0-Vcc/Rc);
		double Vce = 0 + fac*(Ic-Vcc/Rc);
		
		return Vce;
	}
	
	public static void set_Vce_xAxis(double Vcc){
		algorithm.set_Vce_xAxis(Vcc);
	}
	public static double get_Vce_xAxis(){return Vce_xAxis;}
	public static void set_Ic_yAxis(double Vcc, double Rc){
		algorithm.set_Ic_yAxis(Vcc, Rc);
	}
	public static double get_Ic_yAxis(){return Ic_yAxis;}

	public static void setRb(double Rb_new){Rb = Rb_new;}
	public static double getRb(){return Rb;}

	public static void setRc(double Rc_new){Rc = Rc_new;}
	public static double getRc(){return Rc;}

	public static void setVcc(double Vcc_new){Vcc = Vcc_new;}
	public static double getVcc(){return Vcc;}

	public static void setbelta(double belta_new){belta = belta_new;}
	public static double getbelta(){return belta;}

	public static void setVbe(double Vbe_new){Vbe = Vbe_new;}
	public static double getVbe(){return Vbe;}

	public static void setOper_XY(){
		oper_X = Vcc*fractal;
		oper_Y = Vcc/Rc*(1-fractal);	
	}
	public static void setUp_Left(Point up_left){up_Left = up_left;}	public static void setDown_Right(Point down_right){down_Right = down_right;}

}