Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.int
- import net.astesana.javaluator.DoubleEvaluator;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Path;
- import android.graphics.Point;
- import android.util.DisplayMetrics;
- import android.view.Display;
- import android.view.View;
- import android.view.WindowManager;
- public class GraphDrawer extends View {
- // My paint brushes
- Paint axis = new Paint();
- Paint axisNumbers = new Paint();
- Paint graph1 = new Paint();
- Paint graph2 = new Paint();
- Paint extremal = new Paint();
- // Get screen size from users mobile phone/tablet
- DisplayMetrics dm = new DisplayMetrics();
- int width = getContext().getResources().getDisplayMetrics().widthPixels;
- int height = getContext().getResources().getDisplayMetrics().heightPixels;
- // Get from user
- double xMin = -10.0;
- double xMax = 10.0;
- double xScale = 1.0;
- double yMin = -10.0;
- double yMax = 10.0;
- double yScale = 1.0;
- // Get equation from user
- public String e1 = "(Some equation from Graph.java)";
- public GraphDrawer(Context context) {
- super(context);
- // Set axes colors
- axis.setColor(Color.rgb(164, 164, 164)); // light grey
- axisNumbers.setColor(Color.rgb(65, 65, 65)); // dark grey
- axisNumbers.setTextSize(16);
- // Set graph colors
- graph1.setColor(Color.rgb(82, 85, 164));
- graph2.setColor(Color.rgb(82, 85, 164));
- // Set extremal colors
- extremal.setColor(Color.rgb(0,0,0));
- extremal.setTextSize(20);
- }
- @Override
- public void onDraw(Canvas canvas) {
- /*- Calcualte what is largest, x or y? to find plot size ------------------------ */
- int p; // plotSize
- if(width < height){
- p = width;
- }
- else{
- p = height;
- }
- /*- Draw y axis ------------------------------------------------------------------ */
- // Draw y axes
- canvas.drawLine(p/2, 0, p/2, p, axis); // startX, startY, stopX, stopY
- // Draw horizontal lines on y axis
- double yPlacement = 0;
- int yPlacementInt;
- double yMinAbs = Math.abs(yMin); // How many points are there between yMin and yMax?
- double yMaxAbs = Math.abs(yMax);
- double yQuantity = yMinAbs+yMaxAbs;
- double heightDividedByNumberOfPixels = p/yQuantity;
- for(double i=yMin;i<yMax;i++){
- // Line
- yPlacement = yPlacement+heightDividedByNumberOfPixels;
- yPlacementInt = (int)yPlacement;
- canvas.drawLine((width/2)-6, yPlacementInt, (width/2)+6, yPlacementInt, axis); // startX, startY, stopX, stopY
- // Scale
- i = i-1+yScale;
- } // for
- /*- Draw x axis ------------------------------------------------------------------ */
- canvas.drawLine(0, p/2, p, p/2, axis); // startX, startY, stopX, stopY
- // Draw vertical lines on x axis
- double xPlacement = 0;
- int xPlacementInt;
- double xMinAbs = Math.abs(xMin); // How many points are there between yMin and yMax?
- double xMaxAbs = Math.abs(xMax);
- double xQuantity = xMinAbs+xMaxAbs;
- double xWidthDividedByNumberOfPixels = p/xQuantity;
- for(double i=xMin;i<xMax;i++){
- // Line
- xPlacement = xPlacement+xWidthDividedByNumberOfPixels;
- xPlacementInt = (int)xPlacement;
- canvas.drawLine(xPlacementInt, (width/2)-6, xPlacementInt, (width/2)+6, axis); // startX, startY, stopX, stopY
- // Scale
- i = i-1+xScale;
- }
- /*- Draw Equation 1 ------------------------------------------------------------- */
- double xValueD = xMin;
- String xValueS;
- int xValueI;
- double yValueD = 0;
- String yValueS;
- int yValueI;
- String expression;
- // What size is one square?
- yMinAbs = Math.abs(yMin); // How many points are there between yMin and yMax?
- yMaxAbs = Math.abs(yMax);
- yQuantity = yMinAbs+yMaxAbs;
- double route = p/yQuantity;
- // Temporary double values
- double xCord = 0.0;
- double yCord = 0.0;
- //
- double a;
- for(double i=0;i<5000;i++){
- // Find next x value
- xValueD = xValueD+0.001;
- xValueD = xValueD*1000;
- xValueD = Math.round(xValueD);
- xValueD = xValueD/1000;
- xValueS = Double.toString(xValueD);
- if(xValueD < 0.0){
- xValueS = "(" + xValueS + ")";
- }
- expression = e1.replaceAll("x",xValueS);
- try{
- yValueD = new DoubleEvaluator().evaluate(expression);
- // Calculate where the dot should be placed
- // x cord
- xCord = 240+xValueD*24;
- // y cord
- yCord = 240+yValueD*-24;
- int startX = (int)(xCord);
- int startY = (int)(yCord);
- int stopX = (int)(xCord);
- int stopY = (int)(yCord);
- // Draw it
- if(startX < p && startY < p){
- canvas.drawLine(startX, startY, stopX+1, stopY, graph1); // startX, startY, stopX, stopY
- }
- else{
- // we are out of range, plus 1 extra to xvalue
- xValueD = xValueD+0.5;
- }
- }
- catch (Exception e) {
- String errorMsg = "Error: " + e;
- // canvas.drawText(errorMsg, -150, 20, axisNumbers);
- // canvas.drawText(expression, 0, 40, axisNumbers);
- }
- }
- }
- public void putExtra(String tempDataEditTextEquation1) {
- e1 = tempDataEditTextEquation;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment