-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAxisGraph.java
More file actions
141 lines (119 loc) · 3.71 KB
/
Copy pathAxisGraph.java
File metadata and controls
141 lines (119 loc) · 3.71 KB
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
import java.util.ArrayList;
public class AxisGraph {
private Point[][] plane;
private double highest;
private ArrayList<String> graphs = new ArrayList<String>();
private ArrayList<String> storage = new ArrayList<String>();
//constructor method, makes a 41 by 41 array of Points and autozooms to 10
public AxisGraph() {
plane = new Point[41][41];
for (int y = 0; y < plane.length; y++)
for (int x = 0; x < plane[0].length; x++)
plane[y][x] = new Point();
zoom(10);
}
//prints out the 2D array of points
public String toString() {
String retStr = "";
for (Point[] row : plane) {
for (Point p : row)
retStr += p;
retStr += "\n";
}
return retStr;
}
//zoom determines the spacing of each point given the highest values of both x and y
public void zoom(double highVal){
double increment = highVal / ((plane.length - 1) / 2.0);
for (int y = 0; y < plane.length; y++){
double yVal = highVal - (y*increment);
for(int x = 0; x < plane.length; x++){
double xVal = (-1 * highVal) + (x*increment);
plane[y][x].setCor(xVal,yVal);
// if (graphs.size() != 0)
// reGraphAll();
}
}
refresh();
highest = highVal;
}
//runs Point.translate(double,double) on all points, moving the entire graph.
public void translate(double dx, double dy) {
for (Point[] row : plane)
for (Point p : row)
p.translate(dx, dy);
refresh();
}
//runa closeEnough on all the Points, forming a graph
public void graph(String eq, int num){
double increment = highest / ((plane.length - 1) / 2.0);
for(Point[] row: plane)
for(Point p: row)
p.closeEnough_Color(eq, increment / 2.0, num);
}
//graphs new given equation as well as all of the previous ones for graph overlay.
public void graphAll(String eq){
graphs.add(eq);
graphAll();
}
public void graphAll() {
for(int x = 0; x < graphs.size();x++)
graph(graphs.get(x), x);
}
//runs reset() on all Points
public void refresh(){
for(Point[] row: plane){
for(Point p: row){
p.reset();
p.checkAxis();
}
}
}
//clear gets rid of everything, all saved points and everything
public void clear() {
graphs = new ArrayList<String>();
refresh();
}
//takes the input string, checks if it exist already in storage. If it does rewrite over that input, if not, add it to storage
public void store(String eq){
String var = eq.substring(eq.indexOf("[x]")-1,eq.indexOf("[x]"));
for(int i = 0; i < storage.size(); i++){
Boolean truth = var.equals((storage.get(i)).substring(eq.indexOf("[x]")-1,eq.indexOf("[x]")));
if (truth)
{
storage.remove(i);
}
}
storage.add(eq);
}
//takes the function name, matches it with its corresponding place in storage, and returns the expression that was stored.
public String function(String input){
input = input.replace(" ", "");
while(input.indexOf("[x]") != -1){
String var = findname(input);
String replaced = input.substring(input.indexOf("[x]")-1, input.indexOf("[x]")+3);
input = input.replace(replaced,findexp(var));
}
return input;
}
//finds the letter name of the function
public String findname(String input){
String fname = input.substring(input.indexOf("[x]")-1,input.indexOf("[x]"));
return fname;
}
//finds the expression of the function with a certain name
public String findexp(String name){
String exp = "";
for(int i = 0; i < storage.size(); i++){
if(name.equals(findname(storage.get(i)))){
String fexp = storage.get(i);
exp = fexp.substring(fexp.indexOf("=")+1);
exp = "(" + exp + ")";
}
}
return exp;
}
public ArrayList<String> getGraphs() {
return graphs;
}
}