-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDraw_shape.java
More file actions
895 lines (654 loc) · 26.4 KB
/
Copy pathDraw_shape.java
File metadata and controls
895 lines (654 loc) · 26.4 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
import java.awt.image.BufferedImage;
import java.io.PrintWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.ArrayList;
import swiftbot.*;
public class Draw_shape {
public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_GREEN = "\u001B[32m";
public static final String ANSI_RED = "\u001B[31m";// these are the colour codes
static SwiftBotAPI swiftBot;
private static Boolean QR_valid = false; // this is a global variable so that it can work inside the button
public static void main(String[] args) throws InterruptedException {
swiftBot = new SwiftBotAPI(); //these are predefined variables
String decodedQR = "";
String[] shape_list = {};
String[] current_shape = {};
int side1 = 0;
int side2 = 0;
int side3 = 0;
double angle_list[] = {};
ArrayList<ArrayList<String>> ent_list = new ArrayList<>(); // multidimensional array list that stores all my shapes that need to be put in the save file
int count = 0;
start_screen(); // calls the start screen function
while(QR_valid == false)
{
System.out.println(ANSI_GREEN +"Please Scan a QR code" + ANSI_RESET);
shape_list = QR_code();
if(shape_list == null) //if there is something wrong with the shape list in the function and it returns null then the loop will repeat
{
continue;
}
if(shape_list.length > 5) // this makes sure that the QR code can only have up to 5 shapes
{
System.out.println(ANSI_RED +"Invalid QR code");
System.out.println("You can only input up to 5 shapes" + ANSI_RESET);
continue;
}
count = shape_list.length;
for(int i = 0; i < shape_list.length;i++) //loops through the shape list
{
current_shape = shape_check(shape_list[i]);
if(current_shape != null) // this checks is the shape is in the right format
{
if(current_shape.length == 2) //checks if there is only one side
{
side1 = Integer.parseInt(current_shape[1]);
if( ! length_check(side1)) // this checks if the side length is valid
{
System.out.println(ANSI_RED +"Invalid side length");
System.out.println("The side length must be between 15cm and 85cm" + ANSI_RESET);
continue;
}
}
else
{
side1 = Integer.parseInt(current_shape[1]); // these convert the sides from strings to ints
side2 = Integer.parseInt(current_shape[2]);
side3 = Integer.parseInt(current_shape[3]);
if(! length_check(side1,side2,side3)) // this checks if the 3 side lengths are valid
{
System.out.println(ANSI_RED +"Invalid side lengths");
System.out.println("The side lengths must be between 15cm and 85cm" + ANSI_RESET);
continue;
}
}
}
else
{
System.out.println(ANSI_RED + "invalid" + ANSI_RESET); // if the shape list is null it displays invalid
break;
}
if(current_shape[0].charAt(0) == 'T') //this checks if the shape the user inputed is a triangle
{
if( ! triangle_check(side1, side2, side3)) //if the function returns false
{
System.out.println(ANSI_RED +"Invalid Triangle");
System.out.println("Your Triangle can't be drawn" + ANSI_RESET);
continue;
}
angle_list = angle_calculator(side1, side2, side3); // this calls the function that calculates the angles for the triangle
ent_list.add(draw_shape_T(side1, side2, side3, angle_list)); // this calls the function that draws the shape for the triangle
}
else
{
ent_list.add(draw_shape(current_shape[0], side1)); // this calls the function that draws the shape for the square or pentagon then adds it to the ent list
}
if(count > 1) // this checks if multiple shapes needs to be drawn
{
swiftBot.move(-40,-42,1200); // moves the swiftbot back 15cm to draw next shape
count -= 1;
}
}
save_file(ent_list); // this calls the save file function
System.out.println(ANSI_GREEN + "Press X on your swiftbot if you would like to terminate from here!");
long endtime = System.currentTimeMillis() + 5000;
swiftBot.enableButton(Button.X, () -> {
System.out.println("Button pressed" + ANSI_RESET);
QR_valid = true;
swiftBot.disableButton(Button.X);
}); // this is my button that when pressed it terminates the program
while(System.currentTimeMillis() < endtime) // this loop waits 5 seconds giving the user enough time to be able to press the button
{
}
swiftBot.disableButton(Button.X); // this disables the button so it can't be pressed again
}
end_screen(); // displays end screen
}
public static void start_screen() // this is the start screen function
{
System.out.println("=====================================");
System.out.println(ANSI_GREEN +"Draw shape"+ ANSI_RESET);
System.out.println("=====================================");
System.out.println(ANSI_GREEN +"How to play:"+ ANSI_RESET);
System.out.println(ANSI_GREEN +"Scan a QR code containing the shape you want to draw"+ ANSI_RESET);
System.out.println("-------------------------------------");
System.out.println(ANSI_GREEN +"Allowed Shapes:"+ ANSI_RESET);
System.out.println(ANSI_GREEN +"Square"+ ANSI_RESET);
System.out.println(ANSI_GREEN +"Triangle"+ ANSI_RESET);
System.out.println(ANSI_GREEN +"Pentagon"+ ANSI_RESET);
System.out.println("-------------------------------------");
System.out.println(ANSI_GREEN +"Creating the QR code:"+ ANSI_RESET);
System.out.println(ANSI_GREEN +"The QR code must contain the shape you want to draw."+ ANSI_RESET);
System.out.println(ANSI_GREEN +"The lengths of the sides of the shape"+ ANSI_RESET);
System.out.println(ANSI_GREEN +"Square = 'S'"+ ANSI_RESET);
System.out.println(ANSI_GREEN +"Triangle = 'T'"+ ANSI_RESET);
System.out.println(ANSI_GREEN +"Pentagon = 'P'"+ ANSI_RESET);
System.out.println(ANSI_GREEN +"The shape name and side lengths must be split up with a dash '-'"+ ANSI_RESET);
System.out.println("-------------------------------------");
System.out.println(ANSI_GREEN +"Examples:"+ ANSI_RESET);
System.out.println(ANSI_GREEN +"Square = 'S-16'"+ ANSI_RESET);
System.out.println(ANSI_GREEN +"Triange = 'T-16-30-24'"+ ANSI_RESET);
System.out.println(ANSI_GREEN +"Pentagon = 'P-18'"+ ANSI_RESET);
System.out.println("-------------------------------------");
System.out.println(ANSI_GREEN +"Multiple Shapes "+ ANSI_RESET);
System.out.println(ANSI_GREEN +"You can have up to 5 shapes on one QR code"+ ANSI_RESET);
System.out.println(ANSI_GREEN +"Each shape should be seperated by '&'"+ ANSI_RESET);
System.out.println(ANSI_GREEN +"Example: 'S-16&T-16-30-24'"+ ANSI_RESET);
System.out.println("-------------------------------------");
System.out.println(ANSI_GREEN +"Rules:");
System.out.println(ANSI_GREEN +"Side lengths must be between 15 and 85 cm");
System.out.println(ANSI_GREEN +"For a Triangle the side lengths must be able to make a valid shape"+ ANSI_RESET);
System.out.println("=====================================");
}
public static String[] QR_code() //this function scans and decodes the QR code
{
long startTime = System.currentTimeMillis();
long endTime = startTime + 10000; // 10 seconds in milliseconds
String decode = "";
String decode2 = "";
String[] shape_list = {};
try
{
while(System.currentTimeMillis() < endTime) // this loops for the 10 seconds
{
BufferedImage img = swiftBot.getQRImage(); //this gets the contents of the QR code
decode = swiftBot.decodeQRImage(img); //this decodes the contents of the QR code
if(decode.length() != 0)
{
decode2 = decode;
break;
}
}
if(decode2.isEmpty())
{
System.out.println("QR code is empty, try again"); //if there is nothing on the QR code
return null;
}
else
{
shape_list = shape_split(decode2); // this calls the function which splits the decoded QR code into a list of shapes
return shape_list;
}
}
catch(Exception e) //if something goes wrong when scanning the QR code
{
System.out.println("Unable to scan QR code");
return null;
}
}
public static String[] shape_split(String QR_code) // this function splits the shapes into a list
{
String[] shape_list = {};
shape_list = QR_code.split("&"); // splits the QR code string and adds it to the shape list
return shape_list;
}
public static String[] shape_check(String shape) // this splits the shape entry by "-" and adds it to a list then makes checks that it is in the right format
{
String[] check = {};
check = shape.split("-");
if(check.length == 2) //for square or pentagon
{
char ch = check[0].charAt(0); // checks if first character is a letter
if(Character.isAlphabetic(ch))
{
ch = check[1].charAt(0);
if(Character.isDigit(ch)) // checks if the second character is a digit
{
return check; // returns the list that contains the shape and the sides
}
else
{
return null;
}
}
else
{
return null;
}
}
else if(check.length == 4) // for triangle
{
char ch = check[0].charAt(0);
if(Character.isAlphabetic(ch)) //checks if the first character is a letter
{
ch = check[1].charAt(0);
if(Character.isDigit(ch)) // checks if the second character is a digit
{
ch = check[2].charAt(0);
if(Character.isDigit(ch)) // checks if the third character is a digit
{
ch = check[3].charAt(0);
if(Character.isDigit(ch)) // checks if the fourth character is a digit
{
return check;
}
else
{
return null;
}
}
else
{
return null;
}
}
else
{
return null;
}
}
else
{
return null;
}
}
else // if the length of the shape isn't 2 or 4 then the shape is invalid
{
return null;
}
}
public static boolean length_check(int side1) // this is the length checker for 1 parameter
{
if((side1 >= 15) && (side1 <= 85)) // this checks if the the side length is bigger than 15cm and less than 85cm
{
return true;
}
else
{
return false;
}
}
public static boolean length_check(int side1, int side2, int side3) // this is the length checker for 3 parameters
{
if((side1 >= 15) && (side1 <= 85)) // this checks if the first side is between 15 and 85cm
{
if((side2 >= 15) && (side2 <= 85)) // this does the same check for side2
{
if((side3 >=15) && (side3 <= 85)) // this does the same check for side3
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
else
{
return false;
}
}
public static boolean triangle_check(int side1, int side2, int side3) //this function checks for a valid triangle
{
int sum = 0;
int rem_side = 0;
if(side1 > side2) //this look for the 2 smallest sides
{
if(side1 > side3)
{
sum = side2 + side3; // this adds the 2 smallest sides to sum
rem_side = side1; // this adds the largest side to rem_side
}
else
{
sum = side1 + side2;
rem_side = side3;
}
}
else
{
if(side2 > side3)
{
sum = side1 + side3;
rem_side = side2;
}
else
{
sum = side1 + side2;
rem_side = side3;
}
}
if(sum > rem_side) // this checks if the sum of the 2 smallest sides are bigger than the largest side
{
return true;
}
return false;
}
public static double[] angle_calculator(int side1, int side2,int side3) //this function calculates the angles for the triangle
{
double top1 = Math.pow(side1,2) + Math.pow(side2, 2) - Math.pow(side3, 2); // this calculates the top half of the fraction for angle1
double bottom1 = 2 * side1 * side2; // this calculates the bottom half of the fraction for angle1
double top2 = Math.pow(side2, 2) + Math.pow(side3, 2) - Math.pow(side1, 2); // this calculates the top half of the fraction for angle2
double bottom2 = 2 * side2 * side3; // this calculates the bottom half of the fraction for angle2
double top3 = Math.pow(side1, 2) + Math.pow(side3, 2) - Math.pow(side2, 2); // this calculates the top half of the fraction for angle3
double bottom3 = 2 * side1 * side3; // this calculates the bottom half of the fraction for angle3
double frac1 = top1/bottom1; // this calculates the total fraction for angle1
double frac2 = top2/bottom2; // this calculates the total fraction for angle2
double frac3 = top3/bottom3; // this calculates the total fraction for angle3
double angle1 = Math.acos(frac1); // these arccos the values to get the angles
double angle2 = Math.acos(frac2);
double angle3 = Math.acos(frac3);
angle1 = Math.toDegrees(angle1); // these turn the angles from radians to degrees
angle2 = Math.toDegrees(angle2);
angle3 = Math.toDegrees(angle3);
double angles[] = {angle1, angle2, angle3}; // this adds the angles all to a list
return angles;
}
public static ArrayList<String> draw_shape_T(int side1,int side2,int side3, double[] angle_list) throws InterruptedException // this function calculates the time for the drawing the shape for the triangle
{
ArrayList<String> update = new ArrayList<>(); // creates an array list for save values
int[] side_list = {side1, side2, side3}; // this creates a list with the sides
double[] sidelist_straight = new double[3]; // these create an empty list with an index value of 3
double[] anglelist_turn = new double[3];
double total_time = 0;
System.out.println(ANSI_GREEN +"Drawing a Triangle" + ANSI_RESET);
update.add("Triangle");
update.add(String.valueOf(side1));
update.add(String.valueOf(side2));
update.add(String.valueOf(side3));
for(int i = 0; i<3; i++)
{
update.add(String.valueOf(angle_list[i]));
double move_time = speed_time(side_list[i]); //this grabs the forward time for one side
double turn_time = turn_time_T(180 - angle_list[i]); //this grabs the turn time for one angle
sidelist_straight[i] = move_time; //this adds the forward time to a list
anglelist_turn[i] = turn_time; //this adds the turn time to a list
}
total_time = move_triangle(sidelist_straight, anglelist_turn); // this calls the move_triangle function
update.add(String.valueOf(total_time / 1000));
return update;
}
public static ArrayList<String> draw_shape(String shape,int side1) throws InterruptedException // this function calculates the time for drawing the shape for the square and pentagon
{
ArrayList<String> update = new ArrayList<>();
double total_time = 0;
if(shape.charAt(0) == 'S') // this checks if the shape is a square
{
update.add("Square");
System.out.println(ANSI_GREEN + "Drawing a Square" + ANSI_RESET);
double move_time = speed_time(side1);
total_time = move_square(move_time);
}
else if(shape.charAt(0) == 'P') // this checks if the shape is a pentagon
{
update.add("Pentagon");
System.out.println(ANSI_GREEN + "Drawing a Pentagon" + ANSI_RESET);
double move_time = speed_time(side1);
total_time = move_pentagon(move_time);
}
else // this is if the shape isn't a square or a pentagon
{
System.out.println(ANSI_RED +"Shape is Invalid" + ANSI_RESET);
return null;
}
update.add(String.valueOf(side1));
update.add(String.valueOf(total_time / 1000));
return update;
}
public static double speed_time(int side) // this calculates the time for the swiftbot to move forward
{
double speed = 0.13333333333333; // this is the calibrated speed for the swiftbot when moving forward
double distance = (double) side / 100; // this turns the cm into m
double time = distance / speed;
return time;
}
public static double turn_time_T(double angle) // this function calculates the time the swiftbot needs to turn when drawing a triangle
{
double speed = 7; // this is the calibrated speed for the swiftbot when turning
double time = angle * speed;
return time;
}
public static double move_triangle(double[] sidelist, double[] anglelist) throws InterruptedException // this function calculates and moves the swiftbot when drawing the triangle
{
double sum = 0;
int[] green = {0,255,0}; // this is the colour green
for(int i = 0; i < 3; i++)
{
double move_forward = sidelist[i] * 1000; // this converts the seconds into milliseconds
double move_forward2 = Math.round(move_forward); // this rounds the milliseconds to a whole number
double move_turn = Math.round(anglelist[i]); // this rounds the time for the angles to a whole number
int forward = (int) move_forward2; // these converts from double to int
int turn = (int) move_turn;
swiftBot.move(40,40,forward); // this moves the swiftbot in a straight line at 40% power
swiftBot.move(40,-40,turn); // this turns the swiftbot at 40% power
sum += forward; // these calculate the total sum of the swiftbot movement times
sum += turn;
}
try {
swiftBot.fillUnderlights(green); // this flashes the underlights green
Thread.sleep(600); // time delay to keep the lights on
swiftBot.disableUnderlights();
Thread.sleep(600);
swiftBot.fillUnderlights(green);
Thread.sleep(600);
swiftBot.disableUnderlights();
}
catch (Exception e){
e.printStackTrace();
System.out.println("ERROR: UnderLights have failed");
System.exit(5);
}
return sum;
}
public static double move_square(double side_time) throws InterruptedException // this function calculates and moves the swiftbot when drawing the square
{
// angle = 90
int turn = 660; // set turning time
double sum = 0;
int[] green = {0,255,0};
double move_forward = side_time * 1000; // converts the seconds to milliseconds
move_forward = Math.round(move_forward);
int forward = (int) move_forward;
for(int i = 0; i < 4; i++) // this is a loop that counts to 4 to make sure it draws 4 of the same side and angle
{
swiftBot.move(40,43,forward);
swiftBot.move(40,-40, turn);
sum += forward;
sum += turn;
}
try {
swiftBot.fillUnderlights(green); // this flashes the underlights green
Thread.sleep(600); // time delay to keep the lights on
swiftBot.disableUnderlights();
Thread.sleep(600);
swiftBot.fillUnderlights(green);
Thread.sleep(600);
swiftBot.disableUnderlights();
}
catch (Exception e){
e.printStackTrace();
System.out.println("ERROR: failed to set all under lights");
System.exit(5);
}
return sum;
}
public static double move_pentagon(double side_time) throws InterruptedException // this function calculates and moves the swiftbot when drawing the pentagon
{
// angle = 108
int turn = 540; // set turning time
double sum = 0;
int[] green = {0,255,0};
double move_forward = side_time * 1000;
move_forward = Math.round(move_forward);
int forward = (int) move_forward;
for(int i = 0; i < 5; i++) // this is a loop that counts to 5 to make sure it draws 5 of the same side and angle
{
swiftBot.move(40,43,forward);
swiftBot.move(40,-40, turn);
sum += forward;
sum += turn;
}
try {
swiftBot.fillUnderlights(green); // this flashes the underlights green
Thread.sleep(600); // time delay to keep the lights on
swiftBot.disableUnderlights();
Thread.sleep(600);
swiftBot.fillUnderlights(green);
Thread.sleep(600);
swiftBot.disableUnderlights();
}
catch (Exception e){
e.printStackTrace();
System.out.println("ERROR: failed to set all under lights");
System.exit(5);
}
return sum;
}
public static void save_file(ArrayList<ArrayList<String>> ent_list) // this function creates and appends to a text file and saves the shapes values to it
{
int frequent = 0;
String shape_frequent = "";
String lrg_shape = "";
double area = 0;
double total_time = 0;
int time_count = 0; // this variable is to count how many shapes there are
double avg_time = 0;
int t_count = 0;
int s_count = 0;
int p_count = 0;
try(PrintWriter writer = new PrintWriter(new FileWriter("save.txt",true))) // this creates a text file if it hasn't already been created and allows the program to append to it
{
for(int i = 0; i< ent_list.size(); i++) // this loops through my ent list
{
if(ent_list.get(i) == null) // checks if a value in the ent list is null
{
continue;
}
else
{
ArrayList<String> check = ent_list.get(i); // stores the shape list into a variable called check
if(check.get(0) != null && check.get(0) == "Triangle" ) // checks if the shape in the shape list is a triangle
{
total_time += Double.parseDouble(check.get(7));
time_count += 1;
double b = Double.parseDouble(check.get(2));
double c = Double.parseDouble(check.get(3));
double angle = Double.parseDouble(check.get(5));
double calc = (double) 0.5 * b * c * Math.sin(Math.toRadians(angle)); // calculates the area of the triangle
String name = "Triangle:" + check.get(1) +"," + check.get(2) + "," + check.get(3); // this creates the format of how the large shape name will be displayed
t_count += 1; // adds to the triangle count
if(calc > area) // checks if this triangle has the biggest area
{
area = calc;
lrg_shape = name;
}
writer.print(name);
writer.println(" Angle: " + check.get(4) + ","+check.get(5)+","+check.get(6)+";"+" Time: "+check.get(7)+" Seconds)"); // this writes the shape that was drawn to the text file
}
else if(check.get(0) != null && check.get(0) == "Square") // checks if the shape in the shape list is a square
{
total_time += Double.parseDouble(check.get(2));
time_count += 1;
s_count += 1; // adds to the square count
String name = "Square:" + check.get(1);
double calc = Math.pow(Double.parseDouble(check.get(1)), 2); // calculates the area of the square
if(calc > area) // checks if this square has the biggest area
{
area = calc;
lrg_shape = name;
}
writer.print(name);
writer.println(" (Time: " + check.get(2)+ " Seconds)"); // writes the shape that was drawn to the text file
}
else if(check.get(0) != null && check.get(0) == "Pentagon") // checks if the shape in the shape file is a pentagon
{
total_time += Double.parseDouble(check.get(2));
time_count += 1;
p_count += 1; // adds to the pentagon count
String name = "Pentagon:" + check.get(1);
double squ = Math.pow(Double.parseDouble(check.get(1)), 2);
double brac = 2 * Math.sqrt(5);
brac += 5;
brac *= 5;
double calc = (double) 0.25 * Math.sqrt(brac) * squ; // calculates the area of the pentagon
if(calc > area) // checks if this pentagon has the biggest area
{
area = calc;
lrg_shape = name;
}
writer.print(name);
writer.println(" (Time: "+ check.get(2)+ " Seconds)"); // writes the shape that was drawn to the text file
}
// all of this is to find out which shape is the most frequent
if(t_count > s_count)
{
if(t_count > p_count)
{
frequent = t_count;
shape_frequent = "Triangle";
}
else if(p_count > t_count)
{
frequent = p_count;
shape_frequent = "Pentagon";
}
else
{
frequent = t_count;
shape_frequent = "Triangle and Pentagon";
}
}
else if(s_count > t_count)
{
if(s_count > p_count)
{
frequent = s_count;
shape_frequent = "Square";
}
else if(p_count > s_count)
{
frequent = p_count;
shape_frequent = "Pentagon";
}
else
{
frequent = s_count;
shape_frequent = "Square and Pentagon";
}
}
else
{
if(s_count >p_count)
{
frequent = s_count;
shape_frequent = "Triangle and Square";
}
else if(p_count > s_count)
{
frequent = p_count;
shape_frequent = "Pentagon";
}
else
{
frequent = s_count;
shape_frequent = "Triangle and Square and Pentagon";
}
}
}
}
avg_time = total_time / time_count; // calculates the average time the swiftbot took to draw all the shapes
writer.println("Largest Shape: " + lrg_shape+ "by "+ area+"cm" ); // this writes the largest shape to the text file
writer.println("Most Frequent Shape "+ shape_frequent+ " Drawn " +frequent+" times"); // this writes the most frequent shape to the text file
writer.println("Average drawing time: " + avg_time); // this writes the average time to the text file
writer.println();
}
catch(IOException e)
{
e.printStackTrace();
}
}
public static void end_screen() // this function displays the end screen when the program is terminated
{
System.out.println(ANSI_GREEN +"Program Terminated");
System.out.println("Drawing Results:");
System.out.println("/data/home/pi/save.txt-pi@192.168.58.171"+ANSI_RESET);
}
}