Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.util.*;
- import java.io.*;
- /**
- * Write a description of class VectorField here.
- *
- * @author (your name)
- * @version (a version number or a date)
- */
- public class VectorField
- {
- private ArrayList<Polygon> polys = new ArrayList<Polygon>();
- private static final int[] AX_COLOUR = new int[]{127,127,127};
- private int[] arrowColour;
- /**
- * construct0r
- */
- public VectorField(double[][][] coeffs, int[] colour)
- {
- int x,y,i,j;
- double[] current = new double[2];
- arrowColour = colour; //for reuse later or something idk
- //spawn a shitload of arrows
- for(x=-10; x<=10; x+=2)
- for(y=-10; y<=10; y+=2)
- {
- current[0]=0;current[1]=0;
- for(i=0; i<coeffs.length; i++)
- for(j=0; j<coeffs[i].length; j++)
- {
- current[0]+=coeffs[i][j][0]*Math.pow((double)x,i)*Math.pow((double)y,j);
- current[1]-=coeffs[i][j][1]*Math.pow((double)x,i)*Math.pow((double)y,j);
- }
- polys.add(makeArrow(new int[]{320+25*x-(int)(0.5*(current[0]+1)),320-25*y-(int)(0.5*(current[1]+1))},current, colour));
- }
- //draw axes
- ArrayList<Point> yAxis = new ArrayList<Point>(2);
- yAxis.add(new Point(new int[]{320,30}));
- yAxis.add(new Point(new int[]{320,610}));
- polys.add(new Polygon(yAxis, AX_COLOUR, true));
- ArrayList<Point> xAxis = new ArrayList<Point>(2);
- xAxis.add(new Point(new int[]{30,320}));
- xAxis.add(new Point(new int[]{610,320}));
- polys.add(new Polygon(xAxis, AX_COLOUR, true));
- }
- /**
- * default colour constructor
- */
- public VectorField(double[][][] coeffs)
- {
- this(coeffs, new int[]{0,0,0});
- }
- /**
- * Makes an arrow
- */
- public static Polygon makeArrow(int[] offset, double[] length, int[] colour)
- {
- ArrayList<Point> arrow = new ArrayList<Point>(5);
- int[] p0 = new int[]{offset[0],offset[1]};
- int[] p1 = new int[]{offset[0]+(int)(length[0]+0.5),offset[1]+(int)(length[1]+0.5)};
- int[] p2 = new int[]{offset[0]+(int)(0.75*length[0]+0.125*length[1]+0.5),offset[1]+(int)(0.75*length[1]-0.125*length[0]+0.5)};
- int[] p3 = p1;
- int[] p4 = new int[]{offset[0]+(int)(0.75*length[0]-0.125*length[1]+0.5),offset[1]+(int)(0.75*length[1]+0.125*length[0]+0.5)};
- arrow.add(new Point(p0));
- arrow.add(new Point(p1));
- arrow.add(new Point(p2));
- arrow.add(new Point(p3));
- arrow.add(new Point(p4));
- return new Polygon(arrow, colour, false);
- }
- /**
- * can't you even think of a colour
- */
- public static Polygon makeArrow(int[] offset, double[] length)
- {
- return makeArrow(offset, length, new int[]{0,0,0});
- }
- /**
- * Draws
- */
- public void draw(Canvas canvas)
- {
- for(int i=0;i<polys.size();i++)
- polys.get(i).draw(canvas);
- }
- /**
- * Returns a picture of this vector field.
- */
- public Picture toPicture()
- {
- return new Picture(polys);
- }
- /**
- * outputs dis shit to a file
- */
- public void write(String filespec) throws IOException
- {
- File f = new File(filespec);
- f.createNewFile();
- PrintWriter out = new PrintWriter(f);
- out.println("[ placeholder title ]");
- //axes hack
- out.println("c "+AX_COLOUR[0]+" "+AX_COLOUR[1]+" "+AX_COLOUR[2]+" "+
- polys.get(polys.size()-2).getPoints().get(0).getPosition()[0]+" "+
- polys.get(polys.size()-2).getPoints().get(0).getPosition()[1]+" "+
- polys.get(polys.size()-2).getPoints().get(1).getPosition()[0]+" "+
- polys.get(polys.size()-2).getPoints().get(1).getPosition()[1]);
- out.println("c "+AX_COLOUR[0]+" "+AX_COLOUR[1]+" "+AX_COLOUR[2]+" "+
- polys.get(polys.size()-1).getPoints().get(0).getPosition()[0]+" "+
- polys.get(polys.size()-1).getPoints().get(0).getPosition()[1]+" "+
- polys.get(polys.size()-1).getPoints().get(1).getPosition()[0]+" "+
- polys.get(polys.size()-1).getPoints().get(1).getPosition()[1]);
- //arrowz
- int s = polys.size()-2; //leave axes off
- int sp,i,j;
- for (i=0; i<s; i++)
- {
- sp = polys.get(i).getPoints().size();
- out.print("C "+arrowColour[0]+" "+arrowColour[1]+" "+arrowColour[2]+" ");
- for(j = 0;j<sp;j++)
- out.print(polys.get(i).getPoints().get(j).getPosition()[0]+" "+polys.get(i).getPoints().get(j).getPosition()[1]+" ");
- out.println(); //for readability
- }
- out.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement