
import java.awt.*;
import java.applet.Applet;
import java.util.*;

public class FieldLines extends Plotter {

 int Nlines = 16;
 double smallcircle = 0.3;
 double tstep = 0.1;

    //  location of charge
 double x1 = 0.0;  double y1 = 0.0;
	
 public void setup(){
   setPlotSize(400,400);
   setLimits(-10.0, 10.0, -10.0, 10.0);
   setTicks(1.0, 1.0);
 }
	
 public void plot(Graphics g){
   g.setColor(Color.red);
   plotCharge(g,x1,y1);
   g.setColor(Color.black);
   plotFieldLines(g,x1,y1);
 }

 public void plotCharge(Graphics g, double x0,double y0){
    plotStream P = new plotStream();
    P.add(x0,y0);
    plotPoints(g,P);
 }

 void plotFieldLines(Graphics g, double x0, double y0){
    int i;
    for (i = 0; i < Nlines; i++){
       plotOneLine(g, x0,y0, (2.0 * Math.PI * i)/Nlines);
    }
 }

 double[] Efield(double x, double y){
    double [] efield = new double[3];
    double r1 = Math.sqrt((x-x1)*(x-x1) + (y-y1)*(y-y1));
    efield[1] = (x-x1)/(r1*r1*r1);
    efield[2] = (y-y1)/(r1*r1*r1);
    return efield;
 }

 void plotOneLine(Graphics g, double x0, double y0, double theta){
    double [] E = new double[3];
    plotStream P = new plotStream();
    double x = x0 + smallcircle * Math.cos(theta);
    double y = y0 + smallcircle * Math.sin(theta);
    double t;
    for (t = 0.0; t <= 30.0; t+= tstep){
      P.add(x,y);
      E = Efield(x,y);
      double Eval = Math.sqrt(E[1]*E[1] + E[2]*E[2]);
      if (Eval > 50.0) break;
      x += tstep*E[1]/Eval;
      y += tstep*E[2]/Eval;
    }
    plotCurve(g,P);
  }

}	
