Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.polsl.calculator2;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Path;
- import android.graphics.PointF;
- import android.util.Log;
- import android.view.View;
- import java.util.ArrayList;
- import java.util.List;
- public class CustomView extends View {
- private Paint paint;
- private Context context;
- private int numberOfCurves = 1;
- private Path ptCurve;
- private List<PointF> aPoints;
- public CustomView(Context context) {
- super(context);
- //this.context = context;
- paint = new Paint();
- ptCurve = new Path();
- aPoints = new ArrayList<PointF>();
- // paint.setColor(Color.WHITE);
- }
- @Override
- protected void onDraw(Canvas canvas) {
- paint.setStyle(Paint.Style.STROKE);
- paint.setStrokeWidth(2);
- paint.setColor(Color.BLACK);
- int screenHeight = getResources().getDisplayMetrics().heightPixels;
- int screenWidth = getResources().getDisplayMetrics().widthPixels;
- System.out.println("WYSOKOSC" + screenHeight + "SZEROKOSC" + screenWidth);
- //Draw arrow
- Path path = new Path();
- path.moveTo(0, -10);
- path.lineTo(5, 0);
- path.lineTo(-5, 0);
- path.close();
- path.offset(screenWidth / 2, 10);
- canvas.drawPath(path, paint);
- //Draw coordinate plane
- canvas.drawLine(screenWidth / 2, screenHeight, screenWidth / 2, 0, paint);
- canvas.drawLine(0, screenHeight / 2, screenWidth, screenHeight / 2, paint);
- //Draw arrow
- Path path2 = new Path();
- path2.moveTo(5, 0);
- path2.lineTo(0, -10);
- path2.lineTo(0, 10);
- path2.close();
- path2.offset(screenWidth - 5, screenHeight / 2);
- canvas.drawPath(path2, paint);
- scaleXAxis(screenWidth, screenHeight, canvas, paint);
- scaleYAxis(screenWidth, screenHeight, canvas, paint);
- //
- drawLinearFunction(screenWidth, screenHeight, 2, canvas);
- // drawQuadrantFunction(canvas);
- }
- public void drawQuadrantFunction(Canvas canvas) {
- calculatePoints();
- Log.d("HELLO", "DRAWING");
- PointF point = aPoints.get(1);
- ptCurve.moveTo(point.x, point.y);
- // for (int i = 0; i < aPoints.size() - 1; i++) {
- point = aPoints.get(1);
- PointF middle = aPoints.get(2);
- PointF next = aPoints.get(3);
- ptCurve.cubicTo(point.x, point.y, middle.x, middle.y, next.x, next.y);
- // }
- canvas.drawPath(ptCurve, paint);
- canvas.drawLine(aPoints.get(0).x, aPoints.get(0).y, aPoints.get(1).x, aPoints.get(1).y, paint);
- canvas.drawLine(aPoints.get(3).x, aPoints.get(3).y, aPoints.get(4).x, aPoints.get(4).y, paint);
- }
- public void calculatePoints() {
- int a = 1;
- int b = 6;
- int c = 5;
- double y = 0;
- int x = 0;
- // y = (a*x^2) + (b*x) + c;
- double delta = (Math.pow(b, 2) - (4 * a * c));
- double sqrDelta = 0;
- int root1 = 0;
- int root2 = 0;
- int peekX = 0;
- int peekY = 0;
- if (delta > 0) {
- sqrDelta = Math.sqrt(delta);
- root1 = (int) ((-b - sqrDelta) / (2 * a));
- root2 = (int) (-b + sqrDelta) / (2 * a);
- peekX = (-b) / (2 * a);
- peekY = (int) delta / (4 * a);
- Log.d("WARTOSCI", "a=" + a);
- Log.d("WARTOSCI", "b=" + b);
- Log.d("WARTOSCI", "c=" + c);
- Log.d("WARTOSCI", "y=" + y);
- Log.d("WARTOSCI", "x=" + x);
- Log.d("WARTOSCI", "Delta=" + delta);
- Log.d("WARTOSCI", "sqrDelta=" + sqrDelta);
- Log.d("WARTOSCI", "root1=" + root1);
- Log.d("WARTOSCI", "root2=" + root2);
- Log.d("WARTOSCI", "peekX=" + peekX);
- Log.d("WARTOSCI", "peekY=" + peekY);
- int root1Xpx = findXinPx(root1);
- int root2Xpx = findXinPx(root2);
- Log.d("WARTOSCI", "root1Xpx=" + root1Xpx);
- Log.d("WARTOSCI", "root2Xpx=" + root2Xpx);
- int pointXBefRoot1 = root1 - 1;
- int pointXAftRoot2 = root2 + 1;
- Log.d("WARTOSCI", "pointXBefRoot1=" + pointXBefRoot1);
- Log.d("WARTOSCI", "pointXAftRoot2=" + pointXAftRoot2);
- int pointYBefRoot1 = (int) ((a * Math.pow(pointXBefRoot1, 2.0)) + (b * pointXBefRoot1) + c);
- int pointYAftRoot2 = (int) ((a * Math.pow(pointXAftRoot2, 2.0)) + (b * pointXAftRoot2) + c);
- int pointXBefRoot1Px = findXinPx(pointXBefRoot1);
- int pointXAftRoot2Px = findXinPx(pointXAftRoot2);
- int pointYBefRoot1Px = findYinPx(pointYBefRoot1);
- int pointYAftRoot2Px = findYinPx(pointYAftRoot2);
- Log.d("WARTOSCI", "pointXBefRoot1Px=" + pointXBefRoot1Px);
- Log.d("WARTOSCI", "pointXBefRoot1Px=" + pointXAftRoot2Px);
- Log.d("WARTOSCI", "pointYBefRoot1=" + pointYBefRoot1);
- Log.d("WARTOSCI", "pointYAftRoot2=" + pointYAftRoot2);
- Log.d("WARTOSCI", "pointYBefRoot1Px=" + pointYBefRoot1Px);
- Log.d("WARTOSCI", "pointYAftRoot2Px=" + pointYAftRoot2Px);
- int peekXPx = findXinPx(peekX);
- int peekXPy = findYinPxPeek(peekY);
- Log.d("WARTOSCI", "peekXPx=" + peekXPx);
- Log.d("WARTOSCI", "peekXPy=" + peekXPy);
- PointF p1 = new PointF(pointXBefRoot1Px, pointYBefRoot1Px); //1 before root
- PointF firstRoot = new PointF(root1Xpx, 400);
- PointF peekPoint = new PointF(peekXPx, peekXPy);
- PointF secondRoot = new PointF(root2Xpx, 400);
- PointF p2 = new PointF(pointXAftRoot2Px, pointYAftRoot2Px);
- aPoints.add(p1);
- aPoints.add(firstRoot);
- aPoints.add(peekPoint);
- aPoints.add(secondRoot);
- aPoints.add(p2);
- }
- float w = getResources().getDisplayMetrics().widthPixels; //Screen width
- float h = getResources().getDisplayMetrics().heightPixels; //Screen height
- Log.d("HELLO1", "h:" + h + " - w:" + w);
- }
- public void drawLinearFunction(int maxWidth, int maxHeight, int freePar, Canvas canvas) {
- int prevPx = 0;
- int prevPy = 0;
- int px = 0;
- int py = 0;
- int y = 0;
- int a = 2;
- int b = 3;
- for (int x = -6; x < 6; x++) {
- y = a * x + b;
- py = 400 - (y * 40);
- if (x == -6) {
- canvas.drawLine(px, py, px, py, paint);
- prevPx = px;
- prevPy = py;
- } else {
- canvas.drawLine(prevPx, prevPy, px, py, paint);
- prevPx = px;
- prevPy = py;
- }
- px += 40;
- System.out.println("WARTOSC X:" + x + "Wartosc Y" + y);
- }
- }
- //Note: przez 12
- public void scaleXAxis(int x, int y, Canvas canvas, Paint paint) {
- int itr = -6;
- for (x = 0; x < 480; x += 40) {
- canvas.drawLine(x, (y / 2) - 5, x, (y / 2) + 5, paint);
- canvas.drawText(String.valueOf(itr), x - 5, (y / 2) + 20, paint);
- itr++;
- }
- }
- //Note: przez 20
- public void scaleYAxis(int x, int y, Canvas canvas, Paint paint) {
- int itr = 10;
- for (y = 0; y < 800; y += 40) {
- canvas.drawLine((x / 2) - 5, y, (x / 2) + 5, y, paint);
- if (itr != 0) {
- canvas.drawText(String.valueOf(itr), (x / 2) + 13, y + 3, paint);
- }
- itr--;
- }
- }
- public int findXinPx(int x) {
- int tempX = -6 - x;
- int xPx = 0;
- if (x < 0) {
- xPx = Math.abs(tempX) * 40;
- } else if (x > 0) {
- xPx = (6 + x) * 40;
- } else {
- xPx = 240;
- }
- return xPx;
- }
- public int findYinPxPeek(int y) {
- int yPx = 0;
- if (y > 0) {
- yPx = 400 + (y * 80);
- } else if (y < 0) {
- yPx = 400 - (y * 80);
- } else {
- yPx = 0;
- }
- return yPx;
- }
- public int findYinPx(int y) {
- int yPx = 0;
- if (y > 0) {
- yPx = 400 - (y * 40);
- } else if (y < 0) {
- yPx = 400 + (y * 40);
- } else {
- yPx = 0;
- }
- return yPx;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement