From 6af5239c6e6cf30cdea2a27797e940f790c03f37 Mon Sep 17 00:00:00 2001 From: Sergey Solovyev Date: Sat, 19 Jan 2013 01:35:27 +0400 Subject: [PATCH] new plotter --- .../plot/CalculatorGraph2dView.java | 134 ++++++++++-------- 1 file changed, 73 insertions(+), 61 deletions(-) diff --git a/android-app/src/main/java/org/solovyev/android/calculator/plot/CalculatorGraph2dView.java b/android-app/src/main/java/org/solovyev/android/calculator/plot/CalculatorGraph2dView.java index 16cee0f0..c39ce2b2 100644 --- a/android-app/src/main/java/org/solovyev/android/calculator/plot/CalculatorGraph2dView.java +++ b/android-app/src/main/java/org/solovyev/android/calculator/plot/CalculatorGraph2dView.java @@ -56,9 +56,6 @@ public class CalculatorGraph2dView extends View implements GraphView { @NotNull private final Paint textPaint = new Paint(); - @NotNull - private final Paint fillPaint = new Paint(); - @NotNull private GraphViewHelper graphViewHelper = GraphViewHelper.newDefaultInstance(); @NotNull @@ -239,6 +236,76 @@ public class CalculatorGraph2dView extends View implements GraphView { graphsData.checkBoundaries(graphHeight, yMin, yMax); + final int tickDigits = drawGridAndAxis(canvas); + + { + // TOUCH POSITION + + if (lastTouchXPxs != NO_TOUCH && lastTouchYPxs != NO_TOUCH) { + + paint.setColor(graphViewHelper.getPlotViewDef().getGridColor()); + paint.setAlpha(100); + + canvas.drawLine(lastTouchXPxs, 0, lastTouchXPxs, heightPxs, paint); + canvas.drawLine(0, lastTouchYPxs, widthPxs, lastTouchYPxs, paint); + + final Point2d lastTouch = dimensions.toGraphCoordinates(lastTouchXPxs, lastTouchYPxs); + final String touchLabel = "[" + formatTick(lastTouch.getX(), tickDigits + 1) + ", " + formatTick(lastTouch.getY(), tickDigits + 1) + "]"; + canvas.drawText(touchLabel, 0, touchLabel.length(), lastTouchXPxs - 40, lastTouchYPxs - 40, textPaint); + + // restore alpha + paint.setAlpha(255); + } + } + + final float ratio = dimensions.getGraphToViewRatio(); + + matrix.reset(); + matrix.preTranslate(-dimensions.getX0(), -dimensions.getY0()); + matrix.postScale(1/ratio, -1/ratio); + matrix.postTranslate(widthPxs / 2, heightPxs / 2); + + paint.setAntiAlias(false); + + { + //GRAPH + + final List plotFunctions = graphViewHelper.getPlotFunctions(); + + // create path once + final Path path = new Path(); + + for (int i = 0; i < plotFunctions.size(); i++) { + final PlotFunction plotFunction = plotFunctions.get(i); + final GraphData graph = graphsData.get(i); + + graphCalculator.computeGraph(plotFunction.getXyFunction(), xMin, xMax, graph, graphsData, dimensions); + + graphToPath(graph, path); + + path.transform(matrix); + + AbstractCalculatorPlotFragment.applyToPaint(plotFunction.getPlotLineDef(), paint); + + canvas.drawPath(path, paint); + } + } + + + graphsData.setLastXMin(xMin); + graphsData.setLastXMax(xMax); + } + + private int drawGridAndAxis(@NotNull Canvas canvas) { + final float graphHeight = dimensions.getGraphHeight(); + + final float xMin = dimensions.getXMin(); + + final float yMin = dimensions.getYMin(graphHeight); + final float yMax = dimensions.getYMax(graphHeight, yMin); + final float widthPxs = dimensions.getVWidthPxs(); + final float heightPxs = dimensions.getVHeightPxs(); + // set background canvas.drawColor(graphViewHelper.getPlotViewDef().getBackgroundColor()); @@ -264,8 +331,8 @@ public class CalculatorGraph2dView extends View implements GraphView { } - final float tickStep = getTickStep(dimensions.getGWidth()); - final int tickDigits = countTickDigits(tickStep); + final float tickStep = getTickStep(dimensions.getGWidth()); + final int tickDigits = countTickDigits(tickStep); { // GRID @@ -316,62 +383,7 @@ public class CalculatorGraph2dView extends View implements GraphView { canvas.drawLine(x0px, 0, x0px, heightPxs, paint); canvas.drawLine(0, y0px, widthPxs, y0px, paint); } - - { - // TOUCH POSITION - - if (lastTouchXPxs != NO_TOUCH && lastTouchYPxs != NO_TOUCH) { - - paint.setColor(graphViewHelper.getPlotViewDef().getGridColor()); - paint.setAlpha(100); - - canvas.drawLine(lastTouchXPxs, 0, lastTouchXPxs, heightPxs, paint); - canvas.drawLine(0, lastTouchYPxs, widthPxs, lastTouchYPxs, paint); - - final Point2d lastTouch = dimensions.toGraphCoordinates(lastTouchXPxs, lastTouchYPxs); - final String touchLabel = "[" + formatTick(lastTouch.getX(), tickDigits + 1) + ", " + formatTick(lastTouch.getY(), tickDigits + 1) + "]"; - canvas.drawText(touchLabel, 0, touchLabel.length(), lastTouchXPxs - 40, lastTouchYPxs - 40, textPaint); - - // restore alpha - paint.setAlpha(255); - } - } - - - matrix.reset(); - matrix.preTranslate(-dimensions.getX0(), -dimensions.getY0()); - matrix.postScale(1/ratio, -1/ratio); - matrix.postTranslate(widthPxs / 2, heightPxs / 2); - - paint.setAntiAlias(false); - - { - //GRAPH - - final List functionPlotDefs = graphViewHelper.getPlotFunctions(); - - // create path once - final Path path = new Path(); - - for (int i = 0; i < functionPlotDefs.size(); i++) { - final PlotFunction fpd = functionPlotDefs.get(i); - final GraphData graph = graphsData.get(i); - - graphCalculator.computeGraph(fpd.getXyFunction(), xMin, xMax, graph, graphsData, dimensions); - - graphToPath(graph, path); - - path.transform(matrix); - - AbstractCalculatorPlotFragment.applyToPaint(fpd.getPlotLineDef(), paint); - - canvas.drawPath(path, paint); - } - } - - - graphsData.setLastXMin(xMin); - graphsData.setLastXMax(xMax); + return tickDigits; } /*