Skip to content

Commit

Permalink
bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Xlythe committed Apr 19, 2014
1 parent 88a24ad commit 54599da
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 194 deletions.
1 change: 0 additions & 1 deletion Calculator/Calculator-Calculator.iml
Expand Up @@ -72,7 +72,6 @@
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="slider" level="project" />
<orderEntry type="library" exported="" name="ejml-0.21" level="project" />
<orderEntry type="library" exported="" name="achartengine" level="project" />
<orderEntry type="library" exported="" name="gson-2.2.4" level="project" />
<orderEntry type="library" exported="" name="appcompat-v7-19.1.0" level="project" />
<orderEntry type="library" exported="" name="support-v4-19.1.0" level="project" />
Expand Down
25 changes: 10 additions & 15 deletions Calculator/src/main/java/com/android2/calculator3/Graph.java
Expand Up @@ -25,56 +25,51 @@
import java.util.List;

public class Graph {
public static final double MAX_HEIGHT_X = 10;
public static final double MAX_HEIGHT_Y = 10;
public static final double MIN_HEIGHT_X = -10;
public static final double MIN_HEIGHT_Y = -10;
private final Logic mLogic;
private GraphView mGraphView;
private LinkedList<GraphView.Point> mData;
private LinkedList<GraphView.Point> mData = new LinkedList<GraphView.Point>();

public Graph(Logic l) {
mLogic = l;
}

public GraphView getGraph(Context context) {
String title = "";
double[] xValues = new double[0];
double[] yValues = new double[0];

public GraphView createGraph(Context context) {
mLogic.setGraph(this);

mGraphView = new GraphView(context);
mGraphView.setPanListener(new GraphView.PanListener() {
@Override
public void panApplied() {
mLogic.getGraphModule().updateGraphCatchErrors(Graph.this);
mLogic.getGraphModule().updateGraph(Graph.this);
}
});
mGraphView.setZoomListener(new GraphView.ZoomListener() {
@Override
public void zoomApplied(int level) {
mLogic.getGraphModule().updateGraphCatchErrors(Graph.this);
mLogic.getGraphModule().updateGraph(Graph.this);
}
});

mLogic.getGraphModule().updateGraphCatchErrors(this);

mGraphView.setBackgroundColor(Theme.getColor(context, R.color.graph_background));
mGraphView.setTextColor(Theme.getColor(context, R.color.graph_labels_color));
mGraphView.setGridColor(Theme.getColor(context, R.color.graph_grid_color));
mGraphView.setGraphColor(Theme.getColor(context, R.color.graph_color));
return mGraphView;
}

private void addData(float[] xValues, float[] yValues) {
public void addData(float[] xValues, float[] yValues) {
int seriesLength = xValues.length;
for (int k = 0; k < seriesLength; k++) {
mData.add(new GraphView.Point(xValues[k], yValues[k]));
}
mGraphView.setData(mData);
}

public void setData(LinkedList<GraphView.Point> data) {
mData = data;
mGraphView.setData(mData);
}

public LinkedList<GraphView.Point> getData() {
return mData;
}
Expand Down
238 changes: 107 additions & 131 deletions Calculator/src/main/java/com/android2/calculator3/GraphModule.java
@@ -1,41 +1,31 @@
package com.android2.calculator3;

import android.os.AsyncTask;
import android.util.Log;

import com.android2.calculator3.view.GraphView;

import org.achartengine.GraphicalView;
import org.achartengine.model.XYSeries;
import org.achartengine.util.MathHelper;
import org.javia.arity.SyntaxException;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class GraphModule {
Logic mLogic;
GraphView mGraphView;

GraphModule(Logic logic) {
this.mLogic = logic;
}

private static void clearGraph(Graph graph) {
graph.getData().clear();
}

void updateGraph(final Graph g) {
final String eq = mLogic.getText();
void updateGraph(Graph graph) {
final String equation = mLogic.getText();

if (eq.isEmpty()) {
mGraphView.invalidate();
if ((equation.length() != 0 && Logic.isOperator(equation.charAt(equation.length() - 1))) || mLogic.displayContainsMatrices() || equation.endsWith("(")) {
return;
}

if (Logic.isOperator(eq.charAt(eq.length() - 1)) || mLogic.displayContainsMatrices() || eq.endsWith("("))
return;

new GraphTask(g, mLogic).execute(eq);
new GraphTask(graph, mLogic).execute(equation);
}

public static class GraphTask extends AsyncTask<String, String, GraphView> {
Expand All @@ -52,153 +42,139 @@ protected GraphView doInBackground(String... eq) {
final String[] equation = eq[0].split("=");

if (equation.length != 2) {
cancel(true);
return null;
mGraph.setData(new LinkedList<GraphView.Point>());
return mLogic.mGraphView;
}

// Translate into decimal
equation[0] = mLogic.convertToDecimal(mLogic.localize(equation[0]));
equation[1] = mLogic.convertToDecimal(mLogic.localize(equation[1]));
final double minY = mGraph.getRenderer().getYAxisMin();
final double maxY = mGraph.getRenderer().getYAxisMax();
final double minX = mGraph.getRenderer().getXAxisMin();
final double maxX = mGraph.getRenderer().getXAxisMax();

try {
final List<XYSeries> series = new ArrayList<XYSeries>();
series.add(new XYSeries(""));
double lastX = (maxX - minX) / 2 + minX;
double lastY = (maxY - minY) / 2 + minY;

if (equation[0].equals(mLogic.mY) && !equation[1].contains(mLogic.mY)) {
for (double x = minX; x <= maxX; x += (0.00125 * (maxX - minX))) {
if (graphChanged(mGraph, eq[0], minX, maxX, minY, maxY)) return null;

try {
mLogic.mSymbols.define(mLogic.mX, x);
double y = mLogic.mSymbols.eval(equation[1]);

if (pointIsNaN(lastY, y, maxY, minY)) {
series.get(0).add(x, MathHelper.NULL_VALUE);
} else {
series.get(0).add(x, y);
}
lastY = y;
} catch (SyntaxException e) {
e.printStackTrace();
final double minY = mLogic.mGraphView.getYAxisMin();
final double maxY = mLogic.mGraphView.getYAxisMax();
final double minX = mLogic.mGraphView.getXAxisMin();
final double maxX = mLogic.mGraphView.getXAxisMax();

final LinkedList<GraphView.Point> series = new LinkedList<GraphView.Point>();
double lastX = (maxX - minX) / 2 + minX;
double lastY = (maxY - minY) / 2 + minY;

if (equation[0].equals(mLogic.mY) && !equation[1].contains(mLogic.mY)) {
for (double x = minX; x <= maxX; x += (0.00125 * (maxX - minX))) {
if (graphChanged(mGraph, eq[0], minX, maxX, minY, maxY)) return null;

try {
mLogic.mSymbols.define(mLogic.mX, x);
double y = mLogic.mSymbols.eval(equation[1]);

if (pointIsNaN(lastY, y, maxY, minY)) {
series.add(new GraphView.Point(x, GraphView.NULL_VALUE));
} else {
series.add(new GraphView.Point(x, y));
}
lastY = y;
} catch (SyntaxException e) {
e.printStackTrace();
}
} else if (equation[0].equals(mLogic.mX) && !equation[1].contains(mLogic.mX)) {
for (double y = minY; y <= maxY; y += (0.00125 * (maxY - minY))) {
if (graphChanged(mGraph, eq[0], minX, maxX, minY, maxY)) return null;

try {
mLogic.mSymbols.define(mLogic.mY, y);
double x = mLogic.mSymbols.eval(equation[1]);

if (pointIsNaN(lastX, x, maxX, minX)) {
series.get(0).add(MathHelper.NULL_VALUE, y);
} else {
series.get(0).add(x, y);
}
lastX = x;
} catch (SyntaxException e) {
e.printStackTrace();
}
} else if (equation[0].equals(mLogic.mX) && !equation[1].contains(mLogic.mX)) {
for (double y = minY; y <= maxY; y += (0.00125 * (maxY - minY))) {
if (graphChanged(mGraph, eq[0], minX, maxX, minY, maxY)) return null;

try {
mLogic.mSymbols.define(mLogic.mY, y);
double x = mLogic.mSymbols.eval(equation[1]);

if (pointIsNaN(lastX, x, maxX, minX)) {
series.add(new GraphView.Point(GraphView.NULL_VALUE, y));
} else {
series.add(new GraphView.Point(x, y));
}
lastX = x;
} catch (SyntaxException e) {
e.printStackTrace();
}
} else if (equation[1].equals(mLogic.mY) && !equation[0].contains(mLogic.mY)) {
for (double x = minX; x <= maxX; x += (0.00125 * (maxX - minX))) {
if (graphChanged(mGraph, eq[0], minX, maxX, minY, maxY)) return null;

try {
mLogic.mSymbols.define(mLogic.mX, x);
double y = mLogic.mSymbols.eval(equation[0]);

if (pointIsNaN(lastY, y, maxY, minY)) {
series.get(0).add(x, MathHelper.NULL_VALUE);
} else {
series.get(0).add(x, y);
}
lastY = y;
} catch (SyntaxException e) {
e.printStackTrace();
}
} else if (equation[1].equals(mLogic.mY) && !equation[0].contains(mLogic.mY)) {
for (double x = minX; x <= maxX; x += (0.00125 * (maxX - minX))) {
if (graphChanged(mGraph, eq[0], minX, maxX, minY, maxY)) return null;

try {
mLogic.mSymbols.define(mLogic.mX, x);
double y = mLogic.mSymbols.eval(equation[0]);

if (pointIsNaN(lastY, y, maxY, minY)) {
series.add(new GraphView.Point(x, GraphView.NULL_VALUE));
} else {
series.add(new GraphView.Point(x, y));
}
lastY = y;
} catch (SyntaxException e) {
e.printStackTrace();
}
}
} else if (equation[1].equals(mLogic.mX) && !equation[0].contains(mLogic.mX)) {
for (double y = minY; y <= maxY; y += (0.00125 * (maxY - minY))) {
if (graphChanged(mGraph, eq[0], minX, maxX, minY, maxY)) return null;

try {
mLogic.mSymbols.define(mLogic.mY, y);
double x = mLogic.mSymbols.eval(equation[0]);

if (pointIsNaN(lastX, x, maxX, minX)) {
series.add(new GraphView.Point(GraphView.NULL_VALUE, y));
} else {
series.add(new GraphView.Point(x, y));
}
lastX = x;
} catch (SyntaxException e) {
e.printStackTrace();
}
} else if (equation[1].equals(mLogic.mX) && !equation[0].contains(mLogic.mX)) {
for (double y = minY; y <= maxY; y += (0.00125 * (maxY - minY))) {
}
} else {
for (double x = minX; x <= maxX; x += (0.005 * (maxX - minX))) {
List<Double> values = new ArrayList<Double>();
for (double y = maxY; y >= minY; y -= (0.005 * (maxY - minY))) {
if (graphChanged(mGraph, eq[0], minX, maxX, minY, maxY)) return null;

try {
mLogic.mSymbols.define(mLogic.mX, x);
mLogic.mSymbols.define(mLogic.mY, y);
double x = mLogic.mSymbols.eval(equation[0]);

if (pointIsNaN(lastX, x, maxX, minX)) {
series.get(0).add(MathHelper.NULL_VALUE, y);
Double leftSide = mLogic.mSymbols.eval(equation[0]);
Double rightSide = mLogic.mSymbols.eval(equation[1]);
// TODO increase scale of graph as zooming
// out
if (leftSide < 0 && rightSide < 0) {
if (leftSide * 0.99 >= rightSide && leftSide * 1.01 <= rightSide) {
values.add(y);
}
} else {
series.get(0).add(x, y);
if (leftSide * 0.99 <= rightSide && leftSide * 1.01 >= rightSide) {
values.add(y);
}
}
lastX = x;
} catch (SyntaxException e) {
e.printStackTrace();
}
}
} else {
for (double x = minX; x <= maxX; x += (0.01 * (maxX - minX))) {
List<Double> values = new ArrayList<Double>();
for (double y = maxY; y >= minY; y -= (0.01 * (maxY - minY))) {
if (graphChanged(mGraph, eq[0], minX, maxX, minY, maxY)) return null;

try {
mLogic.mSymbols.define(mLogic.mX, x);
mLogic.mSymbols.define(mLogic.mY, y);
Double leftSide = mLogic.mSymbols.eval(equation[0]);
Double rightSide = mLogic.mSymbols.eval(equation[1]);
// TODO increase scale of graph as zooming
// out
if (leftSide < 0 && rightSide < 0) {
if (leftSide * 0.97 >= rightSide && leftSide * 1.03 <= rightSide) {
values.add(y);
}
} else {
if (leftSide * 0.97 <= rightSide && leftSide * 1.03 >= rightSide) {
values.add(y);
}
}
} catch (SyntaxException e) {
e.printStackTrace();
}
}

int color = mGraph.getRenderer().getSeriesRendererAt(0).getColor();
while (values.size() > series.size()) {
series.add(new XYSeries(""));
Graph.addSeriesRenderer(color, mGraph.getRenderer());
}

for (int i = 0; i < values.size(); i++) {
// TODO find closest value to previous one
series.get(i).add(x, values.get(i));
}
for (int i = 0; i < values.size(); i++) {
series.add(new GraphView.Point(x, values.get(i)));
}
}

clearGraph(mGraph);
for (XYSeries s : series) {
mGraph.getDataset().addSeries(s);
}
} catch (Exception e) {
e.printStackTrace();
}
return mLogic.mGraphDisplay;

System.out.println("Setting data");
mGraph.setData(series);
return mLogic.mGraphView;
}

private double tolerance(double result, double truth) {
return (100.0 * Math.abs(truth - result) / Math.abs(truth));
}

boolean graphChanged(Graph graph, String equation, double minX, double maxX, double minY, double maxY) {
return !equation.equals(mLogic.getText()) || minY != graph.getRenderer().getYAxisMin() || maxY != graph.getRenderer().getYAxisMax()
|| minX != graph.getRenderer().getXAxisMin() || maxX != graph.getRenderer().getXAxisMax();
return !equation.equals(mLogic.getText()) || minY != mLogic.mGraphView.getYAxisMin() || maxY != mLogic.mGraphView.getYAxisMax()
|| minX != mLogic.mGraphView.getXAxisMin() || maxX != mLogic.mGraphView.getXAxisMax();
}

boolean pointIsNaN(double lastV, double v, double max, double min) {
Expand Down

0 comments on commit 54599da

Please sign in to comment.