Guest User

GraphDrawer

a guest
Nov 21st, 2012
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.56 KB | None | 0 0
  1. package com.int
  2.  
  3. import net.astesana.javaluator.DoubleEvaluator;
  4. import android.content.Context;
  5. import android.graphics.Canvas;
  6. import android.graphics.Color;
  7. import android.graphics.Paint;
  8. import android.graphics.Path;
  9. import android.graphics.Point;
  10. import android.util.DisplayMetrics;
  11. import android.view.Display;
  12. import android.view.View;
  13. import android.view.WindowManager;
  14.  
  15. public class GraphDrawer extends View {
  16.  
  17.     // My paint brushes
  18.     Paint axis = new Paint();
  19.     Paint axisNumbers = new Paint();
  20.     Paint graph1 = new Paint();
  21.     Paint graph2 = new Paint();
  22.     Paint extremal = new Paint();
  23.  
  24.  
  25.     // Get screen size from users mobile phone/tablet
  26.     DisplayMetrics dm = new DisplayMetrics();
  27.     int width = getContext().getResources().getDisplayMetrics().widthPixels;
  28.     int height = getContext().getResources().getDisplayMetrics().heightPixels;
  29.    
  30.     // Get from user
  31.     double xMin = -10.0;
  32.     double xMax = 10.0;
  33.     double xScale = 1.0;
  34.     double yMin = -10.0;
  35.     double yMax = 10.0;
  36.     double yScale = 1.0;
  37.    
  38.     // Get equation from user
  39.     public String e1 = "(Some equation from Graph.java)";
  40.    
  41.    
  42.     public GraphDrawer(Context context) {
  43.         super(context);
  44.  
  45.         // Set axes colors
  46.         axis.setColor(Color.rgb(164, 164, 164)); // light grey
  47.         axisNumbers.setColor(Color.rgb(65, 65, 65)); // dark grey
  48.         axisNumbers.setTextSize(16);
  49.  
  50.         // Set graph colors
  51.         graph1.setColor(Color.rgb(82, 85, 164));
  52.         graph2.setColor(Color.rgb(82, 85, 164));
  53.        
  54.         // Set extremal colors
  55.         extremal.setColor(Color.rgb(0,0,0));
  56.         extremal.setTextSize(20);
  57.     }
  58.  
  59.     @Override
  60.     public void onDraw(Canvas canvas) {
  61.         /*- Calcualte what is largest, x or y? to find plot size ------------------------ */
  62.         int p; // plotSize
  63.         if(width < height){
  64.             p = width;
  65.         }
  66.         else{
  67.             p = height;
  68.         }
  69.  
  70.        
  71.  
  72.         /*- Draw y axis ------------------------------------------------------------------ */
  73.         // Draw y axes
  74.         canvas.drawLine(p/2, 0, p/2, p, axis); // startX, startY, stopX, stopY
  75.        
  76.         // Draw horizontal lines on y axis
  77.         double yPlacement = 0;
  78.         int yPlacementInt;
  79.        
  80.         double yMinAbs = Math.abs(yMin); // How many points are there between yMin and yMax?
  81.         double yMaxAbs = Math.abs(yMax);
  82.         double yQuantity = yMinAbs+yMaxAbs;
  83.         double heightDividedByNumberOfPixels = p/yQuantity;
  84.        
  85.         for(double i=yMin;i<yMax;i++){
  86.            
  87.             // Line
  88.             yPlacement = yPlacement+heightDividedByNumberOfPixels;
  89.             yPlacementInt = (int)yPlacement;
  90.             canvas.drawLine((width/2)-6, yPlacementInt, (width/2)+6, yPlacementInt, axis); // startX, startY, stopX, stopY
  91.  
  92.             // Scale
  93.             i = i-1+yScale;
  94.         } // for
  95.    
  96.        
  97.         /*- Draw x axis ------------------------------------------------------------------ */
  98.         canvas.drawLine(0, p/2, p, p/2, axis); // startX, startY, stopX, stopY
  99.    
  100.         // Draw vertical lines on x axis
  101.         double xPlacement = 0;
  102.         int xPlacementInt;
  103.        
  104.         double xMinAbs = Math.abs(xMin); // How many points are there between yMin and yMax?
  105.         double xMaxAbs = Math.abs(xMax);
  106.         double xQuantity = xMinAbs+xMaxAbs;
  107.         double xWidthDividedByNumberOfPixels = p/xQuantity;
  108.        
  109.         for(double i=xMin;i<xMax;i++){
  110.            
  111.             // Line
  112.             xPlacement = xPlacement+xWidthDividedByNumberOfPixels;
  113.             xPlacementInt = (int)xPlacement;
  114.             canvas.drawLine(xPlacementInt, (width/2)-6, xPlacementInt, (width/2)+6, axis); // startX, startY, stopX, stopY
  115.  
  116.             // Scale
  117.             i = i-1+xScale;
  118.         }
  119.        
  120.  
  121.        
  122.         /*- Draw Equation 1 ------------------------------------------------------------- */
  123.         double xValueD = xMin;
  124.         String xValueS;
  125.         int xValueI;
  126.        
  127.         double yValueD = 0;
  128.         String yValueS;
  129.         int yValueI;
  130.        
  131.         String expression;
  132.        
  133.         // What size is one square?
  134.         yMinAbs = Math.abs(yMin); // How many points are there between yMin and yMax?
  135.         yMaxAbs = Math.abs(yMax);
  136.         yQuantity = yMinAbs+yMaxAbs;
  137.         double route = p/yQuantity;
  138.        
  139.         // Temporary double values
  140.         double xCord = 0.0;
  141.         double yCord = 0.0;
  142.    
  143.         //
  144.         double a;
  145.        
  146.         for(double i=0;i<5000;i++){
  147.                
  148.                 // Find next x value
  149.                 xValueD = xValueD+0.001;
  150.                 xValueD = xValueD*1000;
  151.                 xValueD = Math.round(xValueD);
  152.                 xValueD = xValueD/1000;
  153.                 xValueS = Double.toString(xValueD);
  154.                 if(xValueD < 0.0){
  155.                     xValueS = "(" + xValueS + ")";
  156.                 }
  157.  
  158.                 expression = e1.replaceAll("x",xValueS);
  159.                
  160.                 try{
  161.                     yValueD = new DoubleEvaluator().evaluate(expression);
  162.                    
  163.                     // Calculate where the dot should be placed
  164.                    
  165.                    
  166.                     // x cord
  167.                     xCord = 240+xValueD*24;
  168.  
  169.                     // y cord
  170.                     yCord = 240+yValueD*-24;
  171.                    
  172.                     int startX = (int)(xCord);
  173.                     int startY = (int)(yCord);
  174.                     int stopX = (int)(xCord);
  175.                     int stopY = (int)(yCord);
  176.                    
  177.                     // Draw it
  178.                     if(startX < p && startY < p){
  179.                         canvas.drawLine(startX, startY, stopX+1, stopY, graph1); // startX, startY, stopX, stopY
  180.                     }
  181.                     else{
  182.                         // we are out of range, plus 1 extra to xvalue
  183.                         xValueD = xValueD+0.5;
  184.                     }
  185.                    
  186.                    
  187.                 }
  188.                 catch (Exception e) {
  189.                     String errorMsg = "Error: " + e;
  190.  
  191.                     // canvas.drawText(errorMsg, -150, 20, axisNumbers);
  192.                     // canvas.drawText(expression, 0, 40, axisNumbers);
  193.                 }
  194.         }
  195.        
  196.  
  197.     }
  198.  
  199.     public void putExtra(String tempDataEditTextEquation1) {
  200.         e1 = tempDataEditTextEquation;
  201.     }
  202.  
  203. }
Advertisement
Add Comment
Please, Sign In to add comment