Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.51 KB | None | 0 0
  1. package com.polsl.calculator2;
  2.  
  3. import android.content.Context;
  4. import android.graphics.Canvas;
  5. import android.graphics.Color;
  6. import android.graphics.Paint;
  7. import android.graphics.Path;
  8. import android.graphics.PointF;
  9. import android.util.Log;
  10. import android.view.View;
  11.  
  12. import java.util.ArrayList;
  13. import java.util.List;
  14.  
  15. public class CustomView extends View {
  16.  
  17.     private Paint paint;
  18.     private Context context;
  19.     private int numberOfCurves = 1;
  20.     private Path ptCurve;
  21.     private List<PointF> aPoints;
  22.  
  23.     public CustomView(Context context) {
  24.         super(context);
  25.  
  26.         //this.context = context;
  27.         paint = new Paint();
  28.         ptCurve = new Path();
  29.         aPoints = new ArrayList<PointF>();
  30.         // paint.setColor(Color.WHITE);
  31.  
  32.     }
  33.  
  34.     @Override
  35.     protected void onDraw(Canvas canvas) {
  36.         paint.setStyle(Paint.Style.STROKE);
  37.         paint.setStrokeWidth(2);
  38.         paint.setColor(Color.BLACK);
  39.  
  40.  
  41.         int screenHeight = getResources().getDisplayMetrics().heightPixels;
  42.         int screenWidth = getResources().getDisplayMetrics().widthPixels;
  43.  
  44.  
  45.         System.out.println("WYSOKOSC" + screenHeight + "SZEROKOSC" + screenWidth);
  46.  
  47.         //Draw arrow
  48.         Path path = new Path();
  49.         path.moveTo(0, -10);
  50.         path.lineTo(5, 0);
  51.         path.lineTo(-5, 0);
  52.         path.close();
  53.         path.offset(screenWidth / 2, 10);
  54.         canvas.drawPath(path, paint);
  55.  
  56.         //Draw coordinate plane
  57.         canvas.drawLine(screenWidth / 2, screenHeight, screenWidth / 2, 0, paint);
  58.         canvas.drawLine(0, screenHeight / 2, screenWidth, screenHeight / 2, paint);
  59.  
  60.         //Draw arrow
  61.         Path path2 = new Path();
  62.         path2.moveTo(5, 0);
  63.         path2.lineTo(0, -10);
  64.         path2.lineTo(0, 10);
  65.         path2.close();
  66.         path2.offset(screenWidth - 5, screenHeight / 2);
  67.         canvas.drawPath(path2, paint);
  68.  
  69.         scaleXAxis(screenWidth, screenHeight, canvas, paint);
  70.         scaleYAxis(screenWidth, screenHeight, canvas, paint);
  71. //
  72.         drawLinearFunction(screenWidth, screenHeight, 2, canvas);
  73.         // drawQuadrantFunction(canvas);
  74.  
  75.  
  76.     }
  77.  
  78.     public void drawQuadrantFunction(Canvas canvas) {
  79.         calculatePoints();
  80.  
  81.         Log.d("HELLO", "DRAWING");
  82.         PointF point = aPoints.get(1);
  83.         ptCurve.moveTo(point.x, point.y);
  84.         // for (int i = 0; i < aPoints.size() - 1; i++) {
  85.         point = aPoints.get(1);
  86.         PointF middle = aPoints.get(2);
  87.         PointF next = aPoints.get(3);
  88.         ptCurve.cubicTo(point.x, point.y, middle.x, middle.y, next.x, next.y);
  89.         // }
  90.  
  91.         canvas.drawPath(ptCurve, paint);
  92.  
  93.         canvas.drawLine(aPoints.get(0).x, aPoints.get(0).y, aPoints.get(1).x, aPoints.get(1).y, paint);
  94.         canvas.drawLine(aPoints.get(3).x, aPoints.get(3).y, aPoints.get(4).x, aPoints.get(4).y, paint);
  95.     }
  96.  
  97.     public void calculatePoints() {
  98.         int a = 1;
  99.         int b = 6;
  100.         int c = 5;
  101.         double y = 0;
  102.         int x = 0;
  103.         //  y = (a*x^2) + (b*x) + c;
  104.         double delta = (Math.pow(b, 2) - (4 * a * c));
  105.         double sqrDelta = 0;
  106.         int root1 = 0;
  107.         int root2 = 0;
  108.         int peekX = 0;
  109.         int peekY = 0;
  110.  
  111.         if (delta > 0) {
  112.             sqrDelta = Math.sqrt(delta);
  113.             root1 = (int) ((-b - sqrDelta) / (2 * a));
  114.             root2 = (int) (-b + sqrDelta) / (2 * a);
  115.             peekX = (-b) / (2 * a);
  116.             peekY = (int) delta / (4 * a);
  117.  
  118.             Log.d("WARTOSCI", "a=" + a);
  119.             Log.d("WARTOSCI", "b=" + b);
  120.             Log.d("WARTOSCI", "c=" + c);
  121.             Log.d("WARTOSCI", "y=" + y);
  122.             Log.d("WARTOSCI", "x=" + x);
  123.             Log.d("WARTOSCI", "Delta=" + delta);
  124.             Log.d("WARTOSCI", "sqrDelta=" + sqrDelta);
  125.             Log.d("WARTOSCI", "root1=" + root1);
  126.             Log.d("WARTOSCI", "root2=" + root2);
  127.             Log.d("WARTOSCI", "peekX=" + peekX);
  128.             Log.d("WARTOSCI", "peekY=" + peekY);
  129.  
  130.             int root1Xpx = findXinPx(root1);
  131.             int root2Xpx = findXinPx(root2);
  132.  
  133.             Log.d("WARTOSCI", "root1Xpx=" + root1Xpx);
  134.             Log.d("WARTOSCI", "root2Xpx=" + root2Xpx);
  135.  
  136.             int pointXBefRoot1 = root1 - 1;
  137.             int pointXAftRoot2 = root2 + 1;
  138.  
  139.             Log.d("WARTOSCI", "pointXBefRoot1=" + pointXBefRoot1);
  140.             Log.d("WARTOSCI", "pointXAftRoot2=" + pointXAftRoot2);
  141.  
  142.             int pointYBefRoot1 = (int) ((a * Math.pow(pointXBefRoot1, 2.0)) + (b * pointXBefRoot1) + c);
  143.             int pointYAftRoot2 = (int) ((a * Math.pow(pointXAftRoot2, 2.0)) + (b * pointXAftRoot2) + c);
  144.  
  145.             int pointXBefRoot1Px = findXinPx(pointXBefRoot1);
  146.             int pointXAftRoot2Px = findXinPx(pointXAftRoot2);
  147.  
  148.             int pointYBefRoot1Px = findYinPx(pointYBefRoot1);
  149.             int pointYAftRoot2Px = findYinPx(pointYAftRoot2);
  150.  
  151.             Log.d("WARTOSCI", "pointXBefRoot1Px=" + pointXBefRoot1Px);
  152.             Log.d("WARTOSCI", "pointXBefRoot1Px=" + pointXAftRoot2Px);
  153.             Log.d("WARTOSCI", "pointYBefRoot1=" + pointYBefRoot1);
  154.             Log.d("WARTOSCI", "pointYAftRoot2=" + pointYAftRoot2);
  155.             Log.d("WARTOSCI", "pointYBefRoot1Px=" + pointYBefRoot1Px);
  156.             Log.d("WARTOSCI", "pointYAftRoot2Px=" + pointYAftRoot2Px);
  157.  
  158.             int peekXPx = findXinPx(peekX);
  159.             int peekXPy = findYinPxPeek(peekY);
  160.  
  161.             Log.d("WARTOSCI", "peekXPx=" + peekXPx);
  162.             Log.d("WARTOSCI", "peekXPy=" + peekXPy);
  163.  
  164.             PointF p1 = new PointF(pointXBefRoot1Px, pointYBefRoot1Px); //1 before root
  165.             PointF firstRoot = new PointF(root1Xpx, 400);
  166.             PointF peekPoint = new PointF(peekXPx, peekXPy);
  167.             PointF secondRoot = new PointF(root2Xpx, 400);
  168.             PointF p2 = new PointF(pointXAftRoot2Px, pointYAftRoot2Px);
  169.             aPoints.add(p1);
  170.             aPoints.add(firstRoot);
  171.             aPoints.add(peekPoint);
  172.             aPoints.add(secondRoot);
  173.             aPoints.add(p2);
  174.  
  175.  
  176.         }
  177.  
  178.         float w = getResources().getDisplayMetrics().widthPixels; //Screen width
  179.         float h = getResources().getDisplayMetrics().heightPixels; //Screen height
  180.  
  181.         Log.d("HELLO1", "h:" + h + " - w:" + w);
  182.  
  183.     }
  184.  
  185.  
  186.     public void drawLinearFunction(int maxWidth, int maxHeight, int freePar, Canvas canvas) {
  187.         int prevPx = 0;
  188.         int prevPy = 0;
  189.         int px = 0;
  190.         int py = 0;
  191.         int y = 0;
  192.         int a = 2;
  193.         int b = 3;
  194.  
  195.         for (int x = -6; x < 6; x++) {
  196.             y = a * x + b;
  197.             py = 400 - (y * 40);
  198.             if (x == -6) {
  199.                 canvas.drawLine(px, py, px, py, paint);
  200.                 prevPx = px;
  201.                 prevPy = py;
  202.             } else {
  203.                 canvas.drawLine(prevPx, prevPy, px, py, paint);
  204.                 prevPx = px;
  205.                 prevPy = py;
  206.             }
  207.             px += 40;
  208.             System.out.println("WARTOSC X:" + x + "Wartosc Y" + y);
  209.  
  210.         }
  211.     }
  212.  
  213.     //Note: przez 12
  214.     public void scaleXAxis(int x, int y, Canvas canvas, Paint paint) {
  215.         int itr = -6;
  216.         for (x = 0; x < 480; x += 40) {
  217.             canvas.drawLine(x, (y / 2) - 5, x, (y / 2) + 5, paint);
  218.             canvas.drawText(String.valueOf(itr), x - 5, (y / 2) + 20, paint);
  219.             itr++;
  220.         }
  221.     }
  222.  
  223.     //Note: przez 20
  224.     public void scaleYAxis(int x, int y, Canvas canvas, Paint paint) {
  225.         int itr = 10;
  226.         for (y = 0; y < 800; y += 40) {
  227.             canvas.drawLine((x / 2) - 5, y, (x / 2) + 5, y, paint);
  228.             if (itr != 0) {
  229.                 canvas.drawText(String.valueOf(itr), (x / 2) + 13, y + 3, paint);
  230.             }
  231.  
  232.             itr--;
  233.         }
  234.  
  235.     }
  236.  
  237.     public int findXinPx(int x) {
  238.         int tempX = -6 - x;
  239.         int xPx = 0;
  240.  
  241.         if (x < 0) {
  242.             xPx = Math.abs(tempX) * 40;
  243.         } else if (x > 0) {
  244.             xPx = (6 + x) * 40;
  245.         } else {
  246.             xPx = 240;
  247.         }
  248.         return xPx;
  249.     }
  250.  
  251.     public int findYinPxPeek(int y) {
  252.         int yPx = 0;
  253.         if (y > 0) {
  254.             yPx = 400 + (y * 80);
  255.         } else if (y < 0) {
  256.             yPx = 400 - (y * 80);
  257.         } else {
  258.             yPx = 0;
  259.         }
  260.  
  261.         return yPx;
  262.     }
  263.  
  264.     public int findYinPx(int y) {
  265.         int yPx = 0;
  266.         if (y > 0) {
  267.             yPx = 400 - (y * 40);
  268.         } else if (y < 0) {
  269.             yPx = 400 + (y * 40);
  270.         } else {
  271.             yPx = 0;
  272.         }
  273.  
  274.         return yPx;
  275.     }
  276.  
  277.  
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement