plot changes

This commit is contained in:
Sergey Solovyev 2013-01-21 00:48:45 +04:00
parent 1a861b6403
commit 62eb80c89d
8 changed files with 92 additions and 51 deletions

View File

@ -169,4 +169,9 @@ public class AndroidCalculatorPlotter implements CalculatorPlotter, SharedPrefer
setPlotImag(CalculatorPreferences.Graph.plotImag.getPreference(preferences)); setPlotImag(CalculatorPreferences.Graph.plotImag.getPreference(preferences));
} }
} }
@Override
public void setPlotData(boolean plot3d, @NotNull PlotBoundaries plotBoundaries) {
plotter.setPlotData(plot3d, plotBoundaries);
}
} }

View File

@ -216,8 +216,14 @@ public abstract class AbstractCalculatorPlotFragment extends CalculatorFragment
@Override @Override
public void onClick(@NotNull MenuItem data, @NotNull Context context) { public void onClick(@NotNull MenuItem data, @NotNull Context context) {
final PlotBoundaries plotBoundaries = getPlotBoundaries();
if (plotBoundaries != null) {
Locator.getInstance().getPlotter().setPlotData(true, plotBoundaries);
} else {
Locator.getInstance().getPlotter().setPlot3d(true); Locator.getInstance().getPlotter().setPlot3d(true);
} }
}
}; };
menuItems.add(plot3dMenuItem); menuItems.add(plot3dMenuItem);
@ -231,8 +237,14 @@ public abstract class AbstractCalculatorPlotFragment extends CalculatorFragment
@Override @Override
public void onClick(@NotNull MenuItem data, @NotNull Context context) { public void onClick(@NotNull MenuItem data, @NotNull Context context) {
final PlotBoundaries plotBoundaries = getPlotBoundaries();
if (plotBoundaries != null) {
Locator.getInstance().getPlotter().setPlotData(false, plotBoundaries);
} else {
Locator.getInstance().getPlotter().setPlot3d(false); Locator.getInstance().getPlotter().setPlot3d(false);
} }
}
}; };
menuItems.add(plot2dMenuItem); menuItems.add(plot2dMenuItem);

View File

@ -598,6 +598,8 @@ public class CalculatorGraph2dView extends View implements GraphView {
public void onTouchZoomDown(float x1, float y1, float x2, float y2) { public void onTouchZoomDown(float x1, float y1, float x2, float y2) {
zoomTracker.start(dimensions.getGWidth(), dimensions.getGHeight(), x1, y1, x2, y2); zoomTracker.start(dimensions.getGWidth(), dimensions.getGHeight(), x1, y1, x2, y2);
lastTouchXPxs = NO_TOUCH;
lastTouchYPxs = NO_TOUCH;
} }
public void onTouchZoomMove(float x1, float y1, float x2, float y2) { public void onTouchZoomMove(float x1, float y1, float x2, float y2) {
@ -612,16 +614,11 @@ public class CalculatorGraph2dView extends View implements GraphView {
} }
private void scroll(float deltaX, float deltaY) { private void scroll(float deltaX, float deltaY) {
final float scale = dimensions.getGWidth() / dimensions.getVWidthPxs(); final float xScale = dimensions.getXGraphToViewScale();
float dx = deltaX * scale; final float yScale = dimensions.getYGraphToViewScale();
float dy = deltaY * scale;
final float adx = Math.abs(dx); float dx = deltaX * xScale;
final float ady = Math.abs(dy); float dy = deltaY * yScale;
if (adx < ady / 3) {
dx = 0;
} else if (ady < adx / 3) {
dy = 0;
}
dimensions.increaseXY(dx, dy); dimensions.increaseXY(dx, dy);
} }

View File

@ -167,7 +167,7 @@ public class CalculatorGraph3dView extends GLView implements GraphView {
@Override @Override
public void setXRange(float xMin, float xMax) { public void setXRange(float xMin, float xMax) {
dimensions.setXRange(PlotBoundaries.DEFAULT_MIN_NUMBER, PlotBoundaries.DEFAULT_MAX_NUMBER); dimensions.setRanges(1, 1, 1, 1);
zoomLevel = 1; zoomLevel = 1;
dirty = true; dirty = true;

View File

@ -165,15 +165,30 @@ public class Graph2dDimensions {
*/ */
public void setXRange(float xMin, float xMax) { public void setXRange(float xMin, float xMax) {
this.gWidth = xMax - xMin; setXRange0(xMin, xMax);
this.x0 = xMin + gWidth / 2;
this.graphView.invalidateGraphs(); this.graphView.invalidateGraphs();
} }
private void setXRange0(float xMin, float xMax) {
this.gWidth = xMax - xMin;
this.x0 = xMin + gWidth / 2;
}
public void setYRange(float yMin, float yMax) { public void setYRange(float yMin, float yMax) {
setYRange0(yMin, yMax);
this.graphView.invalidateGraphs();
}
private void setYRange0(float yMin, float yMax) {
this.gHeight = yMax - yMin; this.gHeight = yMax - yMin;
this.y0 = yMin + gHeight / 2; this.y0 = yMin + gHeight / 2;
}
public void setRanges(float xMin, float xMax, float yMin, float yMax) {
setXRange0(xMin, xMax);
setYRange0(yMin, yMax);
this.graphView.invalidateGraphs(); this.graphView.invalidateGraphs();
} }

View File

@ -1,56 +1,58 @@
package org.solovyev.android.calculator.plot; package org.solovyev.android.calculator.plot;
class ZoomTracker { class ZoomTracker {
private float sx1, sy1, sx2, sy2;
private float initialDist; private static final float EPS = 1.5f;
private static final float MIN_DISTANCE = distance(0f, 50f);
public static final String TAG = "ZoomTracker";
private float initialXDistance;
private float initialYDistance;
private float initialXValue; private float initialXValue;
private float initialYValue; private float initialYValue;
float xValue; float xValue;
float yValue; float yValue;
float moveX, moveY;
void start(float xValue, float yValue, float x1, float y1, float x2, float y2) { void start(float xValue, float yValue,
sx1 = x1; float x1, float y1,
sy1 = y1; float x2, float y2) {
sx2 = x2;
sy2 = y2; initialXDistance = distance(x1, x2);
initialDist = distance(x1, y1, x2, y2); initialYDistance = distance(y1, y2);
initialXValue = xValue; initialXValue = xValue;
initialYValue = yValue; initialYValue = yValue;
this.xValue = xValue;
this.yValue = yValue;
} }
boolean update(float x1, float y1, float x2, float y2) { boolean update(float x1, float y1, float x2, float y2) {
final float LIMIT = 1.5f; boolean result = false;
if (Math.abs(x1 - sx1) < LIMIT && Math.abs(y1 - sy1) < LIMIT &&
Math.abs(x2 - sx2) < LIMIT && Math.abs(y2 - sy2) < LIMIT) { if (initialXDistance > MIN_DISTANCE) {
return false; final float xDistance = distance(x1, x2);
if (xDistance > EPS) {
xValue = initialXDistance / xDistance * initialXValue;
result = true;
} }
moveX = common(x1, sx1, x2, sx2);
moveY = common(y1, sy1, y2, sy2);
float dist = distance(x1, y1, x2, y2);
xValue = initialDist / dist * initialXValue;
yValue = initialDist / dist * initialYValue;
sx1 = x1;
sx2 = x2;
sy1 = y1;
sy2 = y2;
return true;
} }
private static float distance(float x1, float y1, float x2, float y2) { if (initialYDistance > MIN_DISTANCE) {
final float dx = x1-x2; final float yDistance = distance(y1, y2);
final float dy = y1-y2; if (yDistance > EPS) {
// return (float) Math.sqrt(dx*dx+dy*dy); yValue = initialYDistance / yDistance * initialYValue;
return Math.max(dx*dx, dy*dy); result = true;
}
} }
private float common(float x1, float sx1, float x2, float sx2) { return result;
float dx1 = x1 - sx1; }
float dx2 = x2 - sx2;
return (dx1 < 0 && dx2 < 0) ? Math.max(dx1, dx2) : private static float distance(float x1, float x2) {
(dx1 > 0 && dx2 > 0) ? Math.min(dx1, dx2): final float dx = x1 - x2;
0; return dx * dx;
} }
} }

View File

@ -58,6 +58,7 @@ public interface CalculatorPlotter {
boolean isPlotPossibleFor(@NotNull Generic expression); boolean isPlotPossibleFor(@NotNull Generic expression);
void setPlot3d(boolean plot3d); void setPlot3d(boolean plot3d);
void setPlotData(boolean plot3d, @NotNull PlotBoundaries plotBoundaries);
void removeAllUnpinned(); void removeAllUnpinned();

View File

@ -372,6 +372,15 @@ public class CalculatorPlotterImpl implements CalculatorPlotter {
} }
} }
@Override
public void setPlotData(boolean plot3d, @NotNull PlotBoundaries plotBoundaries) {
if (this.plot3d != plot3d || this.plotBoundaries.equals(plotBoundaries)) {
this.plot3d = plot3d;
this.plotBoundaries = plotBoundaries;
firePlotDataChangedEvent();
}
}
private void firePlotDataChangedEvent() { private void firePlotDataChangedEvent() {
plotData = new PlotData(getVisibleFunctions(), plot3d, plotBoundaries); plotData = new PlotData(getVisibleFunctions(), plot3d, plotBoundaries);
calculator.fireCalculatorEvent(CalculatorEventType.plot_data_changed, plotData); calculator.fireCalculatorEvent(CalculatorEventType.plot_data_changed, plotData);