-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoint.java
More file actions
223 lines (187 loc) · 5.24 KB
/
Copy pathPoint.java
File metadata and controls
223 lines (187 loc) · 5.24 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
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
public class Point{
private double x,y;
private String myString;
private String myColor;
//default constructor, bland
public Point(){
this(0, 0);
myString = " ";
}
//overloaded constructor, takes coordinate values
public Point (double xIn, double yIn){
x = xIn;
y = yIn;
checkAxis();
}
//toString prints myString with whatever color the point is.
public String toString() {
return myColor + myString + ANSI.RESET;
}
//determines if a point is on x or y axis and sets myString accordingly
public void checkAxis() {
if(x==0 && y==0)
myString="+";
else if(x==0)
myString="|";
else if(y==0)
myString="-";
myColor = ANSI.WHITE;
}
//makes myString blank
public void reset() {
myString = " ";
myColor = ANSI.WHITE;
}
//this version of checkAxis does a "close enough" to axis sorta thing
public void checkAxis(double halfInc) {
boolean nearX = x < halfInc && x > -1*halfInc;
boolean nearY = y < halfInc && y > -1*halfInc;
if (nearX && nearY)
myString = "+";
else if (nearX)
myString = "|";
else if (nearY)
myString = "-";
// else
// myString = " ";
}
//substitues its coordinates into a given equation and checks for equality
public boolean subEq(String eq){
eq = MathString.sub(eq,"x",x);
eq = MathString.sub(eq,"y",y);
return (MathString.isEqual(eq));
}
//sets myColor variable using ANSI codes
public void setColor(int graphNum) {
graphNum = graphNum % 7;
switch (graphNum) {
case 0:
myColor = ANSI.WHITE;
break;
case 1:
myColor = ANSI.RED;
break;
case 2:
myColor = ANSI.GREEN;
break;
case 3:
myColor = ANSI.YELLOW;
break;
case 4:
myColor = ANSI.BLUE;
break;
case 5:
myColor = ANSI.PURPLE;
break;
case 6:
myColor = ANSI.CYAN;
break;
}
}
//closeEnough but with colors
public void closeEnough_Color(String eq, double halfInc, int graphNum) {
boolean graphed = myString.equals("*");
myString = " ";
closeEnough(eq, halfInc);
if (myString.equals("*"))
setColor(graphNum);
else if (graphed)
myString = "*";
}
//long and complicated algorithm that determines whether or not a point is close enough to the curve of a graph
public void closeEnough(String eq, double halfInc) {
String center = MathString.sub(eq, "x", x);
center = MathString.sub(center, "y", y);
boolean divZero = MathString.divZero(center);
center = MathString.evaluateParens(center);
int divZeroIndex = center.indexOf("/0.0");
boolean numNext;
if (center.length() > divZeroIndex + 4) {
String nextChar = center.substring(divZeroIndex+4, divZeroIndex+5);
numNext = MathString.getNumbers().indexOf(nextChar) != -1;
} else
numNext = false;
if (divZero || divZeroIndex != -1 && halfInc > 0.001 && !numNext) {
//handle asymptotes
checkAxis(halfInc);
double origX = x;
double origY = y;
setCor(origX + halfInc/2, origY + halfInc/2);
closeEnough(eq, halfInc/2 - 0.01);
if (!myString.equals("*")) {
setCor(origX + halfInc/2, origY - halfInc/2);
closeEnough(eq, halfInc/2 - 0.01);
}
if (!myString.equals("*")) {
setCor(origX - halfInc/2, origY - halfInc/2);
closeEnough(eq, halfInc/2 - 0.01);
}
if (!myString.equals("*")) {
setCor(origX - halfInc/2, origY + halfInc/2);
closeEnough(eq, halfInc/2 - 0.01);
}
setCor(origX, origY);
if (!myString.equals("*"))
checkAxis(halfInc);
return;
}
boolean positives = false;
boolean negatives = false;
try {
if (MathString.isEqual(center)) {
myString = "*";
return;
}
String eq1 = MathString.sub(eq,"x",x + halfInc);
//System.out.println(eq1);
eq1= MathString.sub(eq1,"y",y + halfInc);
//System.out.print(MathString.subSides(eq1));
if(MathString.subSides(eq1) == -1)
negatives = true;
else if(MathString.subSides(eq1) == 1)
positives = true;
String eq2 = MathString.sub(eq,"x",x - halfInc);
eq2 = MathString.sub(eq2,"y",y - halfInc);
//System.out.print(MathString.subSides(eq2));
if(MathString.subSides(eq2) == -1)
negatives = true;
else if(MathString.subSides(eq2) == 1)
positives = true;
String eq3 = MathString.sub(eq,"x",x + halfInc);
eq3 = MathString.sub(eq3,"y",y - halfInc);
//System.out.print(MathString.subSides(eq3));
if(MathString.subSides(eq3) == -1)
negatives = true;
else if(MathString.subSides(eq3) == 1)
positives = true;
String eq4 = MathString.sub(eq,"x",x - halfInc);
eq4 = MathString.sub(eq4,"y",y + halfInc);
//System.out.print(MathString.subSides(eq4) + "\n");
if(MathString.subSides(eq4) == -1)
negatives = true;
else if(MathString.subSides(eq4) == 1)
positives = true;
} catch(Exception e) {}
if (positives && negatives) {
myString = "*";
} else {
checkAxis(halfInc);
}
}
//mutator method for x and y coordinates
public void setCor(double X, double Y){
x = X;
y = Y;
}
//accessor method for x and y coordinates
public double[] getCor() {
double[] coords = {x, y};
return coords;
}
//moves a point a certain distance
public void translate(double dx, double dy) {
x += dx;
y += dy;
checkAxis();
}
}