-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraceIt.java
280 lines (236 loc) · 7.64 KB
/
traceIt.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
import exp4j.*;
import java.util.*;
import javafx.application.Application;
import javafx.collections.*;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.chart.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.input.*;
import javafx.scene.text.*;
import javafx.geometry.*;
import javafx.event.*;
public class Main extends Application {
// VARIABLES
double x_max = 10, x_min = -10, x_increment = 0.1; // change these to control points
int no_of_points = (int)((x_max - x_min)/x_increment + 1.0);// changes as per above params
int i,m,n; // loop counters
double x; // final x values to be plotted
double[] y = new double [no_of_points]; // final y values to be plotted
double[][] term = new double[100][no_of_points]; // supported for upto 100 terms
double power_of_x = 0,coefficient_of_x = 0,prevTerm = 0; // term control vars
int term_count = 0,raised = 0, sign = 1,decimal_point = 0,decimal_part = 0; // control triggers
int button_number,buttonX,buttonY, curveNo = 0; // button controls
String string_eqn = new String("y = ");
NumberAxis xAxis = new NumberAxis(x_min,x_max,1.0);
NumberAxis yAxis = new NumberAxis(x_min,x_max,1.0);
LineChart linechart = new LineChart(xAxis,yAxis);
List<XYChart.Series> series = new ArrayList<>();
// CALCULATE EACH TERM - GENERAL FORM - coeff * x ^ power
public double[] calc_term()
{
double termPlot[] = new double[no_of_points];
for(x = x_min, i=0; x<= x_max; x += x_increment,i++)
termPlot[i] = coefficient_of_x*java.lang.Math.pow(x,power_of_x);
return termPlot;
}
// RESET VARIABLES AND SET CONSTANTS WHEN TERM ENDS
public void termEnd_reset()
{
term_count+=1;
decimal_point = 0;
power_of_x = 0;
coefficient_of_x = 0;
prevTerm = 0;
raised = 0;
}
public void eqn_reset()
{
for(m=0,x=x_min ;m< no_of_points; m++,x+=x_increment)
{
y[m] = 0;
for(n=0;n<=term_count;n++)
term[n][m] = 0;
}
term_count = 0;
}
public void draw()
{
if(raised==1 && prevTerm==0)
return;
else if(raised==1)
power_of_x = sign * prevTerm;
else if(prevTerm!=0)
coefficient_of_x = sign * prevTerm;
term[term_count] = calc_term();
series.get(curveNo).getData().clear();
linechart.getData().removeAll(series.get(curveNo));
for(m=0,x=x_min ;m< no_of_points; m++,x+=x_increment)
{
y[m] = 0;
for(n=0;n<=term_count;n++)
y[m] += term[n][m];
series.get(curveNo).getData().add(new XYChart.Data(x,y[m]));
}
linechart.getData().add(series.get(curveNo));
linechart.setAnimated(false);
linechart.setCreateSymbols(false); //disables the points
}
public void start(Stage stage)
{
GridPane operatorGrid = new GridPane();
GridPane numberGrid = new GridPane();
Text variable_txt = new Text("Variable");
Text operator_txt = new Text("Operator");
Text number_txt = new Text("Number");
Text equation = new Text(string_eqn);
Button x_btn= new Button("x");
Button plus = new Button("+");
Button minus = new Button("-");
Button decimal= new Button(".");
Button power = new Button("^");
Button newCurve = new Button("add curve");
Button[] button = new Button[10]; // button array
Button reset = new Button("Reset");
// initialize each button in array
for(button_number = 0; button_number < button.length; button_number++)
button[button_number] = new Button(Integer.toString(button_number));
// position of operators
operatorGrid.add(plus,0,0);
operatorGrid.add(minus,1,0);
operatorGrid.add(decimal,2,0);
operatorGrid.add(power,3,0);
// position of number in mobile keypad form
for(buttonY=0,button_number=1;buttonY<3;buttonY++)
for(buttonX=0;buttonX<3;buttonX++,button_number++)
numberGrid.add(button[button_number],buttonX,buttonY);
numberGrid.add(button[0],1,3);
// graph set-up
xAxis.setLabel("x");
xAxis.setTickUnit(2);
xAxis.setAutoRanging(true);
yAxis.setLabel("y");
yAxis.setTickLabelRotation(0);
yAxis.setTickUnit(2);
linechart.setCreateSymbols(false);
series.add(new XYChart.Series());
// layout of all elements on page
VBox input = new VBox(5);
input.setPadding(new Insets(10, 10, 10, 10));
input.getChildren().addAll(variable_txt,x_btn,operator_txt,operatorGrid,number_txt,numberGrid,reset,newCurve);
VBox output = new VBox(5);
output.setPadding(new Insets(10, 10, 10, 10));
output.getChildren().addAll(equation,linechart);
HBox finalScreen = new HBox(5);
finalScreen.setPadding(new Insets(10, 10, 10, 10));
finalScreen.getChildren().addAll(input,output);
// BUTTON CLICK EVENTS
x_btn.setOnMouseClicked((new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
coefficient_of_x = sign * 1;
power_of_x = 1; //default power of x;
if(prevTerm!=0)
coefficient_of_x = sign * prevTerm;
draw();
prevTerm = 0;
decimal_point = 0;
sign = 1;
string_eqn+=" x";
series.get(curveNo).setName(string_eqn);
equation.setText(string_eqn);
}
}));
plus.setOnMouseClicked((new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
if(!(raised==1 && prevTerm==0))
termEnd_reset();
sign = 1;
string_eqn+=" +";
series.get(curveNo).setName(string_eqn);
equation.setText(string_eqn);
}
}));
minus.setOnMouseClicked((new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
if(!(raised==1 && prevTerm==0))
termEnd_reset();
sign = -1;
string_eqn+=" -";
series.get(curveNo).setName(string_eqn);
equation.setText(string_eqn);
}
}));
decimal.setOnMouseClicked((new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
decimal_point = 1;
decimal_part = 0;
string_eqn+=".";
series.get(curveNo).setName(string_eqn);
equation.setText(string_eqn);
}
}));
power.setOnMouseClicked((new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
raised = 1;
string_eqn+="^";
series.get(curveNo).setName(string_eqn);
equation.setText(string_eqn);
}
}));
for(button_number = 0;button_number<button.length;button_number++){
final Button buttonI = button[button_number];
buttonI.setOnMouseClicked((new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
if(decimal_point==1)
{
decimal_part-=1;
prevTerm+= Integer.parseInt(buttonI.getText())*java.lang.Math.pow(10,decimal_part);
}
else
prevTerm = prevTerm*10 + Integer.parseInt(buttonI.getText());
draw();
string_eqn+=buttonI.getText();
series.get(curveNo).setName(string_eqn);
equation.setText(string_eqn);
}
}));
}
reset.setOnMouseClicked((new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
termEnd_reset();
raised = 0;
sign = 1;
eqn_reset();
string_eqn = "y = ";
series.get(curveNo).setName(string_eqn);
equation.setText(string_eqn);
for(i=0;i<=curveNo;i++)
series.get(i).getData().clear();
linechart.getData().clear();
linechart.setAnimated(false);
}
}));
newCurve.setOnMouseClicked((new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
termEnd_reset();
raised = 0;
sign = 1;
eqn_reset();
series.add(new XYChart.Series());
curveNo++;
string_eqn = "y = ";
series.get(curveNo).setName(string_eqn);
equation.setText(string_eqn);
}
}));
// window set-up
Scene scene = new Scene(finalScreen,finalScreen.getPrefWidth(),finalScreen.getPrefHeight());
stage.setTitle("Trace_it_!!");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}