-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplexGraph.java
More file actions
361 lines (344 loc) · 12.5 KB
/
ComplexGraph.java
File metadata and controls
361 lines (344 loc) · 12.5 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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/**
* ComplexGraph.java
*
* Graphs two complex numbers, and shows the
* sum, or difference, or product, or quotient
* of these two numbers, as chosen by the user.
* The two complex numbers are draggable (can be
* selected by the mouse and dragged to a new
* location). Also, the values of the two points
* are printed to the GUI, along with the
* resulting sum, difference, product, or quotient,
* as chosen by the user.
*
* @author Scott DeRuiter
* @version 1.0
* @since 10/26/2022
*/
import java.awt.Color;
import java.awt.Font;
public class ComplexGraph
{
/** Complex values to be graphed and manipulated. */
private Complex redA, blueB;
/** Booleans to keep track of which point is being moved or changed. */
private boolean changeA, changeB;
/** Indicates if the mouse has been pressed on one of the points. */
private boolean pointHasFocus;
/**
* Creates a ComplexGraph object. A no-args constructor. The boolean
* fields are all set to false, and the complex points are set to random
* values.
*/
public ComplexGraph ( )
{
redA = new Complex(-4 + (int)(9 * Math.random()),-3 + (int)(9 * Math.random()));
blueB = new Complex(-4 + (int)(9 * Math.random()),-3 + (int)(9 * Math.random()));
changeA = changeB = pointHasFocus = false;
}
/**
* The main method, to run some of the operations and print them to the
* terminal window. Also, a canvas is set up, and the GUI is run.
*/
public static void main(String[] args)
{
ComplexGraph graph = new ComplexGraph();
graph.checkComplexOperations();
graph.setUpCanvas();
graph.runGUI();
}
/**
* Checks many of the constructors and methods of the Complex class,
* including the four constructors, addition, subtraction, multiplication,
* and division.
*/
public void checkComplexOperations ( )
{
System.out.println("\n\n\n");
Complex num1 = new Complex();
Complex num2 = new Complex(4);
Complex num3 = new Complex(3,4);
Complex num4 = new Complex(1,-2);
Complex num5 = new Complex(num3);
System.out.println("num1 = " + num1);
System.out.println("num2 = " + num2);
System.out.println("num3 = " + num3);
System.out.println("num4 = " + num4);
System.out.println("num5 = " + num5);
num5.set(2.4,3.5);
System.out.println("num5 = " + num5);
System.out.println("(" + num3 + ")" + " + " + "(" + num4 + ") = " + num3.add(num4));
System.out.println("(" + num3 + ")" + " - " + "(" + num4 + ") = " + num3.subtract(num4));
System.out.println("(" + num3 + ")" + " * " + "(" + num4 + ") = " + num3.multiply(num4));
System.out.println("(" + num3 + ")" + " / " + "(" + num4 + ") = " + num3.divide(num4));
System.out.println("\n\n\n");
}
/**
* Sets up the canvas, using methods from StdDraw. This includes
* setting up the canvas size, the horizontal scale (Xscale), and
* the vertical scale (Yscale).
*/
public void setUpCanvas ( )
{
final int WIDTH = 1240;
final int HEIGHT = 740;
StdDraw.setCanvasSize(WIDTH, HEIGHT);
StdDraw.setXscale(-9.4, 15.4);
StdDraw.setYscale(-7.4, 7.4);
StdDraw.enableDoubleBuffering();
}
/**
* Runs the Graphical User Interface.
*/
public void runGUI ( )
{
int buttonChoice = 4;
while(true)
{
StdDraw.clear(new Color(255,255,255));
drawAxesScaleAndBackground();
drawPointsAndVectorsAndInfo(buttonChoice);
buttonChoice = drawOperators(buttonChoice);
reactToMousePressAndDragOnPoint(redA);
reactToMousePressAndDragOnPoint(blueB);
StdDraw.show();
StdDraw.pause(40);
}
}
/**
* Draws the axes for the real and imaginary parts of the complex
* number. These axes are labeled, and grid lines are shown, to
* help with the identification of the point in the plane.
*/
public void drawAxesScaleAndBackground ( )
{
Font font = new Font("Arial", Font.PLAIN, 18);
StdDraw.setFont(font);
for(double integers = -9; integers <= 15; integers++)
{
StdDraw.setPenColor(new Color(220,220,220));
if(integers == 0)
{
StdDraw.setPenColor(new Color(0,0,0));
}
StdDraw.line(integers,-7.4,integers,7.4);
StdDraw.line(-9.4,integers,15.4,integers);
StdDraw.setPenColor(new Color(0,0,0));
if(integers != 0)
{
StdDraw.text(integers,-0.4,"" + (int)integers);
StdDraw.text(-0.3,integers-0.05,"" + (int)integers);
}
}
font = new Font("Arial", Font.BOLD, 18);
StdDraw.setFont(font);
StdDraw.text(14.5,0.11,"REAL");
StdDraw.text(0.24,-6,"IMAGINARY",90);
}
/**
* Draws the 3 points, A, B, and the operation on A and B (A+B or A-B or
* A*B or A/B), as chosen by the user. Vectors are also drawn to A and
* B. The values of A and B, along with the value of the operation on
* A and B, are printed in the lower right of the GUI.
*/
public void drawPointsAndVectorsAndInfo(int buttonChoice)
{
drawPoints(buttonChoice);
drawVectorsAnglesAndInfo(buttonChoice);
}
/**
* Draws the 3 points, A, B, and the operation on A and B (A+B or A-B or
* A*B or A/B), as chosen by the user.
*/
public void drawPoints(int buttonChoice)
{
Font font = new Font("Arial", Font.BOLD, 18);
StdDraw.setFont(font);
Complex sum = redA.add(blueB);
Complex diff = redA.subtract(blueB);
Complex product = redA.multiply(blueB);
Complex quotient = redA.divide(blueB);
StdDraw.setPenColor(new Color(255,0,0));
StdDraw.line(0,0,redA.getReal(),redA.getImaginary());
StdDraw.setPenColor(new Color(0,0,255));
StdDraw.line(0,0,blueB.getReal(),blueB.getImaginary());
for(int i = 0; i < 5; i++)
{
StdDraw.setPenColor(new Color(255 - i * 15,0,0));
StdDraw.filledCircle(redA.getReal(),redA.getImaginary(),0.2 - 0.04 * i);
StdDraw.setPenColor(new Color(0,0,255 - i * 15));
StdDraw.filledCircle(blueB.getReal(),blueB.getImaginary(),0.2 - 0.04 * i);
StdDraw.setPenColor(new Color(0 + 25 * i,0 + 25 * i,0 + 25 * i));
if(buttonChoice == 4)
{
StdDraw.filledCircle(sum.getReal(), sum.getImaginary(), 0.2 - 0.04 * i);
}
else if(buttonChoice == 3)
{
StdDraw.filledCircle(diff.getReal(), diff.getImaginary(), 0.2 - 0.04 * i);
}
else if(buttonChoice == 2)
{
StdDraw.filledCircle(product.getReal(), product.getImaginary(), 0.2 - 0.04 * i);
}
else if(buttonChoice == 1)
{
StdDraw.filledCircle(quotient.getReal(), quotient.getImaginary(), 0.2 - 0.04 * i);
}
}
}
/**
* Vectors are drawn to A and B with addition and subtraction. Angles for A and
* B are swept out for multiplication and division. The values of A and B, along
* with the value of the operation on A and B, are printed in the lower right of the GUI.
*/
public void drawVectorsAnglesAndInfo(int buttonChoice)
{
Font font = new Font("Arial", Font.BOLD, 18);
StdDraw.setFont(font);
Complex sum = redA.add(blueB);
Complex diff = redA.subtract(blueB);
Complex product = redA.multiply(blueB);
Complex quotient = redA.divide(blueB);
StdDraw.setPenColor(new Color(0,0,0));
StdDraw.text(redA.getReal() - 0.4,redA.getImaginary() - 0.05,"A");
StdDraw.text(blueB.getReal() - 0.4,blueB.getImaginary() - 0.05,"B");
StdDraw.textLeft(11,-4.5,"A = " + redA);
StdDraw.textLeft(11,-5.5,"B = " + blueB);
if(buttonChoice == 4)
{
StdDraw.text(sum.getReal() - 0.63, sum.getImaginary() - 0.05, "A+B");
StdDraw.textLeft(11,-6.5,"A+B = " + sum);
StdDraw.setPenColor(new Color(0,0,255));
StdDraw.line(redA.getReal(),redA.getImaginary(),redA.getReal()+blueB.getReal(),redA.getImaginary()+blueB.getImaginary());
}
else if(buttonChoice == 3)
{
StdDraw.text(diff.getReal() - 0.63, diff.getImaginary() - 0.05, "A-B");
StdDraw.textLeft(11,-6.5,"A-B = " + diff);
StdDraw.setPenColor(new Color(0,0,255));
StdDraw.line(redA.getReal(),redA.getImaginary(),redA.getReal()-blueB.getReal(),redA.getImaginary()-blueB.getImaginary());
}
else if(buttonChoice == 2)
{
StdDraw.text(product.getReal() - 0.63, product.getImaginary() - 0.05, "A*B");
StdDraw.textLeft(11,-6.5,"A*B = " + product);
StdDraw.setPenColor(new Color(255,0,0));
StdDraw.arc(0,0,Math.sqrt(Math.pow(redA.getReal(),2) + Math.pow(redA.getImaginary(),2)),0,180 / Math.PI * Math.atan2(redA.getImaginary(),redA.getReal()));
StdDraw.setPenColor(new Color(0,0,255));
StdDraw.arc(0,0,Math.sqrt(Math.pow(blueB.getReal(),2) + Math.pow(blueB.getImaginary(),2)),0,180 / Math.PI * Math.atan2(blueB.getImaginary(),blueB.getReal()));
StdDraw.setPenColor(new Color(255,0,0));
StdDraw.arc(0,0,Math.sqrt(Math.pow(product.getReal(),2) + Math.pow(product.getImaginary(),2)) + 0.1,0,180 / Math.PI * Math.atan2(redA.getImaginary(),redA.getReal()));
StdDraw.setPenColor(new Color(0,0,255));
StdDraw.arc(0,0,Math.sqrt(Math.pow(product.getReal(),2) + Math.pow(product.getImaginary(),2)),180 / Math.PI * Math.atan2(redA.getImaginary(),redA.getReal()),
180 / Math.PI * Math.atan2(redA.getImaginary(),redA.getReal()) + 180 / Math.PI * Math.atan2(blueB.getImaginary(),blueB.getReal()));
}
else if(buttonChoice == 1)
{
StdDraw.text(quotient.getReal() - 0.63, quotient.getImaginary() - 0.05, "A/B");
StdDraw.textLeft(11,-6.5,"A/B = " + quotient);
StdDraw.setPenColor(new Color(255,0,0));
StdDraw.arc(0,0,Math.sqrt(Math.pow(redA.getReal(),2) + Math.pow(redA.getImaginary(),2)),0,180 / Math.PI * Math.atan2(redA.getImaginary(),redA.getReal()));
StdDraw.setPenColor(new Color(0,0,255));
StdDraw.arc(0,0,Math.sqrt(Math.pow(blueB.getReal(),2) + Math.pow(blueB.getImaginary(),2)),0,180 / Math.PI * Math.atan2(blueB.getImaginary(),blueB.getReal()));
StdDraw.setPenColor(new Color(255,0,0));
StdDraw.arc(0,0,Math.sqrt(Math.pow(quotient.getReal(),2) + Math.pow(quotient.getImaginary(),2)) + 0.1,0,180 / Math.PI * Math.atan2(redA.getImaginary(),redA.getReal()));
StdDraw.setPenColor(new Color(0,0,255));
StdDraw.arc(0,0,Math.sqrt(Math.pow(quotient.getReal(),2) + Math.pow(quotient.getImaginary(),2)),
180 / Math.PI * Math.atan2(redA.getImaginary(),redA.getReal()) - 180 / Math.PI * Math.atan2(blueB.getImaginary(),blueB.getReal()),
180 / Math.PI * Math.atan2(redA.getImaginary(),redA.getReal()));
}
}
/**
* Draws the buttons representing the four operations, +, -, *, and /,
* in the upper right hand corner of the GUI. The user may click on
* a button to select a particular operation.
*/
public int drawOperators(int buttonChoice)
{
double x = StdDraw.mouseX();
double y = StdDraw.mouseY();
if(x >= 14.1 && x <= 14.9 && StdDraw.isMousePressed() && !pointHasFocus)
{
if(y >= 3.1 && y <= 3.9)
{
buttonChoice = 1;
}
else if(y >= 4.1 && y <= 4.9)
{
buttonChoice = 2;
}
else if(y >= 5.1 && y <= 5.9)
{
buttonChoice = 3;
}
else if(y >= 6.1 && y <= 6.9)
{
buttonChoice = 4;
}
}
for(int i = 1; i <= 4; i++)
{
if(i == buttonChoice)
{
StdDraw.setPenColor(new Color(255,0,0));
StdDraw.filledRectangle(14.5,2.5 + i,0.5,0.5);
}
StdDraw.setPenColor(new Color(0,0,0));
StdDraw.filledRectangle(14.5,2.5 + i,0.4,0.4);
StdDraw.setPenColor(new Color(200,200,200));
StdDraw.filledRectangle(14.5,2.5 + i,0.34,0.34);
}
Font font = new Font("Arial", Font.BOLD, 45);
StdDraw.setFont(font);
StdDraw.setPenColor(new Color(0,0,0));
StdDraw.text(14.5,6.35,"+");
StdDraw.text(14.5,5.44,"-");
StdDraw.text(14.5,4.2,"*");
StdDraw.text(14.5,3.36,"/");
return buttonChoice;
}
/**
* Reacts to the mouse being pressed and then dragged on the point A or
* the point B. Boolean values are set to indicate that a point has
* focus, and to indicate which point has focus.
*/
public void reactToMousePressAndDragOnPoint(Complex number)
{
double x = StdDraw.mouseX();
double y = StdDraw.mouseY();
if(Math.sqrt(Math.pow(x - number.getReal(),2) + Math.pow(y - number.getImaginary(),2)) < 0.22)
{
if(StdDraw.isMousePressed() & !pointHasFocus)
{
if(number == redA)
{
redA.set(x,y);
changeA = pointHasFocus = true;
changeB = false;
}
else if(number == blueB)
{
blueB.set(x,y);
changeB = pointHasFocus = true;
changeA = false;
}
}
}
if(StdDraw.isMousePressed() && pointHasFocus)
{
if(changeA)
{
redA.set(x,y);
}
else if(changeB)
{
blueB.set(x,y);
}
}
else
{
pointHasFocus = changeA = changeB = false;
}
}
}