Advertisement
Guest User

Untitled

a guest
Sep 15th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 5.01 KB | None | 0 0
  1. import java.awt.*;
  2. import java.util.*;
  3. import java.io.*;
  4.  
  5. /**
  6.  * Write a description of class VectorField here.
  7.  *
  8.  * @author (your name)
  9.  * @version (a version number or a date)
  10.  */
  11. public class VectorField
  12. {
  13.  
  14.     private ArrayList<Polygon> polys = new ArrayList<Polygon>();
  15.     private static final int[] AX_COLOUR = new int[]{127,127,127};
  16.     private int[] arrowColour;
  17.  
  18.     /**
  19.      * construct0r
  20.      */
  21.     public VectorField(double[][][] coeffs, int[] colour)
  22.     {
  23.         int x,y,i,j;
  24.         double[] current = new double[2];
  25.  
  26.         arrowColour = colour;   //for reuse later or something idk
  27.         //spawn a shitload of arrows        
  28.         for(x=-10; x<=10; x+=2)
  29.             for(y=-10; y<=10; y+=2)
  30.             {
  31.                 current[0]=0;current[1]=0;
  32.                 for(i=0; i<coeffs.length; i++)
  33.                     for(j=0; j<coeffs[i].length; j++)
  34.                     {
  35.                         current[0]+=coeffs[i][j][0]*Math.pow((double)x,i)*Math.pow((double)y,j);
  36.                         current[1]-=coeffs[i][j][1]*Math.pow((double)x,i)*Math.pow((double)y,j);
  37.                     }
  38.                 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));
  39.             }
  40.            
  41.         //draw axes
  42.         ArrayList<Point> yAxis = new ArrayList<Point>(2);
  43.         yAxis.add(new Point(new int[]{320,30}));
  44.         yAxis.add(new Point(new int[]{320,610}));
  45.         polys.add(new Polygon(yAxis, AX_COLOUR, true));
  46.  
  47.         ArrayList<Point> xAxis = new ArrayList<Point>(2);
  48.         xAxis.add(new Point(new int[]{30,320}));
  49.         xAxis.add(new Point(new int[]{610,320}));        
  50.         polys.add(new Polygon(xAxis, AX_COLOUR, true));
  51.        
  52.     }
  53.    
  54.     /**
  55.      * default colour constructor
  56.      */
  57.     public VectorField(double[][][] coeffs)
  58.     {
  59.         this(coeffs, new int[]{0,0,0});
  60.     }
  61.  
  62.     /**
  63.      * Makes an arrow
  64.      */
  65.     public static Polygon makeArrow(int[] offset, double[] length, int[] colour)
  66.     {
  67.         ArrayList<Point> arrow = new ArrayList<Point>(5);
  68.         int[] p0 = new int[]{offset[0],offset[1]};
  69.         int[] p1 = new int[]{offset[0]+(int)(length[0]+0.5),offset[1]+(int)(length[1]+0.5)};
  70.         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)};
  71.         int[] p3 = p1;
  72.         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)};
  73.  
  74.         arrow.add(new Point(p0));
  75.         arrow.add(new Point(p1));
  76.         arrow.add(new Point(p2));
  77.         arrow.add(new Point(p3));
  78.         arrow.add(new Point(p4));
  79.        
  80.         return new Polygon(arrow, colour, false);
  81.     }
  82.    
  83.     /**
  84.      * can't you even think of a colour
  85.      */
  86.     public static Polygon makeArrow(int[] offset, double[] length)
  87.     {
  88.         return makeArrow(offset, length, new int[]{0,0,0});
  89.     }
  90.    
  91.     /**
  92.      * Draws
  93.      */
  94.     public void draw(Canvas canvas)
  95.     {
  96.         for(int i=0;i<polys.size();i++)
  97.             polys.get(i).draw(canvas);
  98.     }
  99.    
  100.     /**
  101.      * Returns a picture of this vector field.
  102.      */
  103.     public Picture toPicture()
  104.     {
  105.         return new Picture(polys);
  106.     }
  107.    
  108.     /**
  109.      * outputs dis shit to a file
  110.      */
  111.     public void write(String filespec) throws IOException
  112.     {
  113.  
  114.        
  115.         File f = new File(filespec);
  116.         f.createNewFile();
  117.         PrintWriter out = new PrintWriter(f);
  118.        
  119.         out.println("[ placeholder title ]");
  120.        
  121.        
  122.         //axes hack
  123.        
  124.         out.println("c "+AX_COLOUR[0]+" "+AX_COLOUR[1]+" "+AX_COLOUR[2]+" "+
  125.                   polys.get(polys.size()-2).getPoints().get(0).getPosition()[0]+" "+
  126.                   polys.get(polys.size()-2).getPoints().get(0).getPosition()[1]+" "+
  127.                   polys.get(polys.size()-2).getPoints().get(1).getPosition()[0]+" "+
  128.                   polys.get(polys.size()-2).getPoints().get(1).getPosition()[1]);
  129.         out.println("c "+AX_COLOUR[0]+" "+AX_COLOUR[1]+" "+AX_COLOUR[2]+" "+
  130.                   polys.get(polys.size()-1).getPoints().get(0).getPosition()[0]+" "+
  131.                   polys.get(polys.size()-1).getPoints().get(0).getPosition()[1]+" "+
  132.                   polys.get(polys.size()-1).getPoints().get(1).getPosition()[0]+" "+
  133.                   polys.get(polys.size()-1).getPoints().get(1).getPosition()[1]);
  134.                  
  135.        
  136.         //arrowz
  137.         int s = polys.size()-2;   //leave axes off
  138.         int sp,i,j;
  139.         for (i=0; i<s; i++)
  140.         {
  141.             sp = polys.get(i).getPoints().size();
  142.             out.print("C "+arrowColour[0]+" "+arrowColour[1]+" "+arrowColour[2]+" ");
  143.             for(j = 0;j<sp;j++)
  144.                 out.print(polys.get(i).getPoints().get(j).getPosition()[0]+" "+polys.get(i).getPoints().get(j).getPosition()[1]+" ");
  145.             out.println(); //for readability
  146.         }
  147.        
  148.         out.close();
  149.     }    
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement