-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphWindowView.java
335 lines (240 loc) · 9.25 KB
/
GraphWindowView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package com.example.mathsolver.Views;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;
import java.util.ArrayList;
import java.util.List;
public class GraphWindowView extends View {
private static final float MIN_GRID_SCALE = 0.000001f;
private List<PointF> points = new ArrayList<>();
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private ScaleGestureDetector scaleGestureDetector;
private float initialCursorPropX, initialCursorPropY;
private float initialViewportX, initialViewportY;
private float initialSpan;
private float initialViewportWidth, initialViewportHeight;
private OnViewportBoundsChangedListener o;
private GraphFunction f;
private float viewPortX;
private float viewPortY;
private float viewPortWidth;
private float viewPortHeight;
private float unitsPerDivX = 1, unitsPerDivY = 1;
private int minDivisions = 8;
private int numFunctionPoints = 400;
private ZoomMode currentZoomMode = ZoomMode.XY;
public enum ZoomMode {
XY,
X,
Y
}
public GraphWindowView(Context context) {
super(context);
init();
}
public GraphWindowView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public GraphWindowView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public GraphWindowView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
private void init() {
recenter();
paint.setTextSize(50);
paint.setStrokeWidth(5);
scaleGestureDetector = new ScaleGestureDetector(getContext(), scaleGestureListener);
}
int gridLineColor = Color.rgb(240, 240, 240);
int gridLineDarkerColor = Color.rgb(210, 210, 210);
@Override
protected void onDraw(Canvas canvas) {
// background
//canvas.drawColor(Color.rgb(240, 240, 240));
// minor grid lines
paint.setColor(gridLineColor);
for (float x = getFirstLine(viewPortX, unitsPerDivX); x <= viewPortX + viewPortWidth; x += unitsPerDivX) {
canvas.drawLine(getScreenX(x), 0, getScreenX(x), getHeight(), paint);
}
for (float y = getFirstLine(viewPortY, unitsPerDivY); y <= viewPortY + viewPortHeight; y += unitsPerDivY) {
canvas.drawLine(0, getScreenY(y), getWidth(), getScreenY(y), paint);
}
// major grid lines
paint.setColor(gridLineDarkerColor);
for (float x = getFirstLine(viewPortX, unitsPerDivX * 10); x <= viewPortX + viewPortWidth; x += unitsPerDivX * 10) {
canvas.drawLine(getScreenX(x), 0, getScreenX(x), getHeight(), paint);
}
for (float y = getFirstLine(viewPortY, unitsPerDivY * 10); y <= viewPortY + viewPortHeight; y += unitsPerDivY * 10) {
canvas.drawLine(0, getScreenY(y), getWidth(), getScreenY(y), paint);
}
// origin
paint.setColor(Color.BLACK);
canvas.drawLine(getScreenX(0), 0, getScreenX(0), getHeight(), paint);
canvas.drawLine(0, getScreenY(0), getWidth(), getScreenY(0), paint);
// plot
paint.setColor(Color.RED);
for (int i = 0; i < points.size() - 1; i++) {
canvas.drawLine(getScreenX(points.get(i).x), getScreenY(points.get(i).y), getScreenX(points.get(i + 1).x), getScreenY(points.get(i + 1).y), paint);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return scaleGestureDetector.onTouchEvent(event) || super.onTouchEvent(event);
}
private final ScaleGestureDetector.OnScaleGestureListener scaleGestureListener
= new ScaleGestureDetector.SimpleOnScaleGestureListener() {
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
initialCursorPropX = detector.getFocusX() / getWidth();
initialCursorPropY = 1 - detector.getFocusY() / getHeight();
initialSpan = detector.getCurrentSpan();
initialViewportX = viewPortX;
initialViewportY = viewPortY;
initialViewportWidth = viewPortWidth;
initialViewportHeight = viewPortHeight;
return true;
}
@Override
public boolean onScale(ScaleGestureDetector detector) {
float currentSpan = detector.getCurrentSpan();
float scale = initialSpan / currentSpan;
float cursorPropX = detector.getFocusX() / getWidth();
float cursorPropY = 1 - detector.getFocusY() / getHeight();
//Log.i("VIEWPORT", String.format("screen prop: (%f, %f)", cursorPropX, cursorPropY));
float deltaPropX = cursorPropX - initialCursorPropX;
float deltaPropY = cursorPropY - initialCursorPropY;
//Log.i("VIEWPORT", String.format("delta screen prop: (%f, %f)", deltaPropX, deltaPropY));
// change scale x
if (currentZoomMode != ZoomMode.Y) {
viewPortWidth = scale * initialViewportWidth;
}
// pan x
viewPortX = initialViewportX - cursorPropX * (viewPortWidth - initialViewportWidth) - deltaPropX * initialViewportWidth;
// change scale y
if (currentZoomMode != ZoomMode.X) {
viewPortHeight = scale * initialViewportHeight;
}
// pan y
viewPortY = initialViewportY - cursorPropY * (viewPortHeight - initialViewportHeight) - deltaPropY * initialViewportHeight;
// update grid scale for x and y
float optimalScale = Math.max(
getOptimalGridScale(viewPortWidth, minDivisions),
getOptimalGridScale(viewPortHeight, minDivisions)
);
unitsPerDivX = unitsPerDivY = optimalScale;
//unitsPerDivX = getOptimalGridScale(viewPortWidth, minDivisions);
//unitsPerDivX = getOptimalGridScale(viewPortWidth, minDivisions);
// report bounds changed
reportBoundsChanged();
// compute function points in new window
if (f != null) {
computePoints();
}
// force redraw
invalidate();
return true;
}
};
public void setZoomMode(ZoomMode zoomMode) {
currentZoomMode = zoomMode;
}
public ZoomMode getZoomMode() {
return currentZoomMode;
}
private void computePoints() {
clearPoints();
for (int i = 0; i < numFunctionPoints; i++) {
float x = viewPortX + ((float) i / numFunctionPoints) * viewPortWidth;
addPoint(new PointF(x, f.value(x)));
}
}
public void redraw() {
computePoints();
invalidate();
}
public void recenter(){
setViewportBounds(-10, 10, -10, 10);
}
public void squareScale() {
viewPortHeight = viewPortWidth/getWidth()*getHeight();
reportBoundsChanged();
invalidate();
}
private static float nearestPower(float number, float powerOf) {
return (float) Math.pow(powerOf, Math.floor(Math.log(number) / Math.log(powerOf)));
}
private static float getOptimalGridScale(float viewPortDimension, int minGridLines) {
return Math.max(MIN_GRID_SCALE, nearestPower(viewPortDimension / minGridLines, 10));
}
private static float getFirstLine(float viewportMin, float divisionSize) {
float sum = viewportMin + divisionSize;
return sum - (sum % divisionSize) - divisionSize;
}
private PointF getScreenPoint(PointF actualPoint) {
return new PointF(getScreenX(actualPoint.x), getScreenY(actualPoint.y));
}
private float getScreenX(float actualX) {
return (actualX - viewPortX) / viewPortWidth * getWidth();
}
private float getScreenY(float actualY) {
return (1 - (actualY - viewPortY) / viewPortHeight) * getHeight();
}
public void setOnViewportBoundsChangedListener(OnViewportBoundsChangedListener o) {
this.o = o;
}
public void setViewportBounds(float minX, float maxX, float minY, float maxY) {
viewPortX = minX;
viewPortY = minY;
viewPortWidth = maxX - minX;
viewPortHeight = maxY - minY;
unitsPerDivX = getOptimalGridScale(viewPortWidth, minDivisions);
unitsPerDivY = getOptimalGridScale(viewPortHeight, minDivisions);
reportBoundsChanged();
invalidate();
}
public RectF getViewportBounds() {
return new RectF(viewPortX, viewPortY + viewPortHeight, viewPortX + viewPortWidth, viewPortY);
}
private void reportBoundsChanged() {
if(o != null) {
o.onBoundsChanged(getViewportBounds());
}
}
public float getGridScaleX() {
return unitsPerDivX;
}
public float getGridScaleY() {
return unitsPerDivY;
}
public interface OnViewportBoundsChangedListener {
void onBoundsChanged(RectF bounds);
}
public void addPoint(PointF point) {
points.add(point);
}
public void clearPoints() {
points.clear();
}
public void setFunction(GraphFunction f) {
this.f = f;
computePoints();
ViewCompat.postInvalidateOnAnimation(GraphWindowView.this);
}
public interface GraphFunction {
float value(float input);
}
}