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
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
|
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dialog.ModalityType;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.ListCellRenderer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.BevelBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
/**
* Main class for Bezier graphing.
*
* @author acm
*
*/
public class CulkinAsssignmentNine {
/**
* The current bezier curve.
*/
public final Holder<Bezier> currentCurve;
/**
* The directory of all bezier curves.
*/
public final Map<String, Bezier> curveDirectory;
/**
* Create a new main class.
*/
public CulkinAsssignmentNine() {
curveDirectory = new HashMap<>();
/*
* Set current curve.
*/
currentCurve = new Holder<>();
currentCurve.setVal(new Bezier());
/*
* Install current curve into directory.
*/
curveDirectory.put("Default", currentCurve.getVal());
}
public static void main(String[] args) {
CulkinAsssignmentNine assign = new CulkinAsssignmentNine();
/*
* Use the native look and feel.
*/
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
| UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
JFrame fram = assign.setupFrame();
/*
* Display the GUI.
*/
fram.setSize(640, 480);
fram.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
fram.setVisible(true);
}
/*
* Setup the main JFrame
*/
private JFrame setupFrame() {
JFrame fram = new JFrame("Bezier Grapher");
fram.setLayout(new BorderLayout());
JPanel canvasPanel = new JPanel();
canvasPanel.setLayout(new GridLayout(1, 1));
BezierPanel canvas = new BezierPanel(curveDirectory.values());
canvasPanel.add(canvas);
JPanel points = new JPanel();
points.setLayout(new BorderLayout());
JPanel listPanel = new JPanel();
listPanel.setLayout(new GridLayout(1, 1));
DefaultListModel<TDPoint> pointModel = new DefaultListModel<>();
/*
* Repaint the canvas whenever the list changes.
*/
pointModel.addListDataListener(new CanvasRepainter(canvas));
/*
* Update the list whenever the curve changes.
*/
currentCurve.addHolderListener((val) -> {
pointModel.clear();
for (TDPoint punkt : val.controls) {
pointModel.addElement(punkt);
}
});
JList<TDPoint> pointList = new JList<>(pointModel);
/*
* Use special point renderer.
*/
pointList.setCellRenderer(new TDPointRenderer());
JScrollPane listScroller = new JScrollPane(pointList);
listPanel.add(listScroller);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1, 2));
/*
* Setup action buttons.
*/
JButton addPoint = new JButton("Add Control Points...");
addPoint.addActionListener(new PointAdder(pointModel, currentCurve, fram));
JButton remPoint = new JButton("Remove Control Point");
remPoint.addActionListener(new PointRemover(fram, pointList, pointModel, currentCurve));
buttonPanel.add(addPoint);
buttonPanel.add(remPoint);
points.add(listPanel, BorderLayout.CENTER);
points.add(buttonPanel, BorderLayout.PAGE_END);
JSplitPane main = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, points, canvasPanel);
JPanel menuPanel = new JPanel();
menuPanel.setLayout(new FlowLayout(FlowLayout.LEADING, 5, 1));
JPanel editingPanel = new JPanel();
editingPanel.setLayout(new FlowLayout(FlowLayout.LEADING, 5, 1));
editingPanel.setBorder(new BevelBorder(BevelBorder.RAISED));
JButton editCurveProperties = new JButton("Edit Curve Properties");
editCurveProperties.addActionListener(new CurveEditor(fram, currentCurve, canvas));
editingPanel.add(editCurveProperties);
JPanel destructivePanel = new JPanel();
destructivePanel.setLayout(new FlowLayout(FlowLayout.LEADING, 5, 1));
destructivePanel.setBorder(new BevelBorder(BevelBorder.RAISED));
/*
* Clear all curve points.
*/
JButton clearPoints = new JButton("Clear Points");
clearPoints.addActionListener((ev) -> {
int confirm = JOptionPane.showConfirmDialog(fram, "Are you sure you want to clear the points?",
"Clear Points", JOptionPane.YES_NO_OPTION);
if (confirm == JOptionPane.YES_OPTION) {
currentCurve.getVal().controls.clear();
pointModel.clear();
}
});
/*
* Reset to use new curve.
*/
JButton resetCurve = new JButton("Reset Curve");
resetCurve.addActionListener((ev) -> {
int confirm = JOptionPane.showConfirmDialog(fram, "Are you sure you want to reset the curve?",
"Clear Points", JOptionPane.YES_NO_OPTION);
if (confirm == JOptionPane.YES_OPTION) {
currentCurve.getVal().controls.clear();
currentCurve.setVal(new Bezier());
curveDirectory.put("Default", currentCurve.getVal());
canvas.repaint();
}
});
destructivePanel.add(clearPoints);
destructivePanel.add(resetCurve);
/*
* This isn't implemented yet.
*/
JPanel curvesPanel = new JPanel();
curvesPanel.setLayout(new FlowLayout(FlowLayout.LEADING, 5, 1));
curvesPanel.setBorder(new BevelBorder(BevelBorder.RAISED));
menuPanel.add(editingPanel);
menuPanel.add(destructivePanel);
// menuPanel.add(curvesPanel);
fram.add(main, BorderLayout.CENTER);
fram.add(menuPanel, BorderLayout.PAGE_START);
return fram;
}
}
/*
* Do editing of curve properties.
*/
class CurveEditor implements ActionListener {
private Bezier curve;
private JFrame fram;
private BezierPanel canvas;
public CurveEditor(JFrame fram, Holder<Bezier> currentCurve, BezierPanel canvas) {
this.fram = fram;
this.canvas = canvas;
curve = currentCurve.getVal();
/*
* Set the curve to the right one.
*/
currentCurve.addHolderListener((val) -> {
curve = val;
});
}
@Override
public void actionPerformed(ActionEvent ev) {
JDialog dia = new JDialog(fram, "Curve Editor", ModalityType.MODELESS);
dia.setLayout(new BorderLayout());
JPanel fields = new JPanel();
fields.setLayout(new BorderLayout());
LabeledInputPanel partsPanel = new LabeledInputPanel("# of Points to Graph", curve.data.parts);
JTabbedPane colorPanel = new JTabbedPane();
colorPanel.setBorder(new TitledBorder(new BevelBorder(BevelBorder.RAISED), "Colors"));
ColorInputPanel curveColor = new ColorInputPanel(curve.data.curveColor, "Curve Color");
ColorInputPanel pointColor = new ColorInputPanel(curve.data.pointColor, "Point Color");
ColorInputPanel boxColor = new ColorInputPanel(curve.data.boxColor, "Bounding Box Color");
colorPanel.addTab("Curve Color", curveColor);
colorPanel.addTab("Point Color", pointColor);
colorPanel.addTab("Bounding Box Color", boxColor);
LabeledInputPanel scalePanel = new LabeledInputPanel("Curve Scaling Multiplier", curve.data.scale);
fields.add(partsPanel, BorderLayout.PAGE_START);
fields.add(colorPanel, BorderLayout.CENTER);
fields.add(scalePanel, BorderLayout.PAGE_END);
JPanel buttons = new JPanel();
buttons.setLayout(new GridLayout(1, 3));
/*
* Persist changes to curve.
*/
JButton saveButton = new JButton("Save Changes");
saveButton.addActionListener((aev) -> {
curve.data.parts = (Integer) partsPanel.field.getValue();
curve.data.scale = (Double) scalePanel.field.getValue();
curve.data.curveColor = curveColor.picker.getColor();
curve.data.pointColor = pointColor.picker.getColor();
curve.data.boxColor = boxColor.picker.getColor();
canvas.repaint();
});
/*
* Reset fields to match curve.
*/
JButton resetButton = new JButton("Reset Changes");
resetButton.addActionListener((aev) -> {
partsPanel.field.setValue(curve.data.parts);
scalePanel.field.setValue(curve.data.scale);
curveColor.picker.setColor(curve.data.curveColor);
pointColor.picker.setColor(curve.data.pointColor);
boxColor.picker.setColor(curve.data.boxColor);
});
JButton cancelButton = new JButton("Cancel Changes");
cancelButton.addActionListener((aev) -> {
dia.dispose();
});
buttons.add(saveButton);
buttons.add(resetButton);
buttons.add(cancelButton);
dia.add(fields, BorderLayout.CENTER);
dia.add(buttons, BorderLayout.PAGE_END);
dia.pack();
dia.setVisible(true);
}
}
/*
* Panel for inputting colors.
*/
class ColorInputPanel extends JPanel {
private static final long serialVersionUID = 5201595672794938745L;
public final JColorChooser picker;
public ColorInputPanel(String label) {
this(Color.WHITE, label);
}
public ColorInputPanel(Color init, String label) {
picker = new JColorChooser(init);
setLayout(new BorderLayout());
JLabel lab = new JLabel(label);
add(lab, BorderLayout.LINE_START);
add(picker, BorderLayout.CENTER);
}
}
/*
* Panel that graphs a set of bezier curves.
*/
class BezierPanel extends JPanel {
public final Collection<Bezier> curves;
private static final long serialVersionUID = 8748298173487657108L;
public BezierPanel() {
super();
curves = new LinkedList<>();
setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
}
public BezierPanel(Collection<Bezier> curvs) {
super();
curves = curvs;
setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
int ourWidth = getWidth();
int ourHeight = getHeight();
int halfWidth = ourWidth / 2;
int halfHeight = ourHeight / 2;
g.setColor(Color.WHITE);
/*
* Draw background
*/
g.fillRect(0, 0, ourWidth, ourHeight);
g.setColor(Color.BLUE);
/*
* Draw coordinate grid.
*/
g.drawLine(halfWidth, 0, halfWidth, ourHeight);
g.drawLine(0, halfHeight, ourWidth, halfHeight);
/*
* Transform to place points at center of grid w/ right orientation.
*/
TDHTransform translate = new TDHCombination(new TDHXAxisReflection(), new TDHTranslate(halfWidth, halfHeight));
for (Bezier curve : curves) {
/*
* Skip curves with no points.
*/
if (curve.controls.isEmpty()) {
continue;
}
{
/*
* Draw curve bounding box.
*/
g.setColor(curve.data.boxColor);
TDPoint[] ex = curve.extrema(translate);
g.drawLine((int) ex[0].x, (int) ex[0].y, (int) ex[1].x, (int) ex[1].y);
g.drawLine((int) ex[1].x, (int) ex[1].y, (int) ex[2].x, (int) ex[2].y);
g.drawLine((int) ex[2].x, (int) ex[2].y, (int) ex[3].x, (int) ex[3].y);
g.drawLine((int) ex[3].x, (int) ex[3].y, (int) ex[0].x, (int) ex[0].y);
}
g.setColor(curve.data.pointColor);
for (TDPoint control : curve.controls) {
/*
* Draw curve points.
*/
TDHPoint translatedPoint = translate.transform(control.multiply(curve.data.scale).toTDHPoint());
control = translatedPoint.toTDPoint();
drawCircle(g, control.x, control.y, 6);
}
g.setColor(curve.data.curveColor);
for (int i = 0; i < curve.data.parts; i++) {
/*
* Draw curve itself.
*/
double dT = 1.0 / curve.data.parts;
TDPoint firt = curve.scaleEval(dT * i);
TDPoint secod = curve.scaleEval(dT * (i + 1));
TDPoint first = translate.transform(firt.toTDHPoint()).toTDPoint();
TDPoint second = translate.transform(secod.toTDHPoint()).toTDPoint();
g.drawLine((int) first.x, (int) first.y, (int) second.x, (int) second.y);
}
}
}
/*
* Draw a circle of a given diameter at a point.
*/
private static void drawCircle(Graphics g, double x, double y, int diameter) {
g.fillOval((int) x - diameter / 2, (int) y - diameter / 2, diameter, diameter);
}
}
/**
* Represents a bezier curve.
*/
class Bezier {
/**
* The control points of the curve.
*/
public final List<TDPoint> controls;
/**
* The display properties for the curve.
*/
public BezierProperties data;
/**
* Create a new bezier curve from a set of control points.
*
* @param points
* The control points to use.
*/
public Bezier(TDPoint... points) {
data = new BezierProperties();
controls = new ArrayList<>();
for (TDPoint punkt : points) {
controls.add(punkt);
}
}
/**
* Do a scaled evaluation of the curve.
*
* @param t
* The value to evaluate at.
* @return The point on the curve, scaled by the curves scaling factor.
*/
public TDPoint scaleEval(double t) {
return eval(t).multiply(data.scale);
}
/**
* Do a scaled evaluation of the curve.
*
* @param t
* The value to evaluate at.
*/
public TDPoint eval(double t) {
if (controls.isEmpty())
return new TDPoint(0, 0);
TDPoint punkt = evalIntern(t, controls.size() - 1, 0);
return punkt;
}
/*
* Recursive evaluation of bezier curve.
*/
private TDPoint evalIntern(double t, int j, int k) {
/*
* Base case.
*/
if (j <= 0) {
return controls.get(k);
}
/*
* Get the two component points.
*/
TDPoint l = evalIntern(t, j - 1, k);
TDPoint r = evalIntern(t, j - 1, k + 1);
/*
* Adjust them by parameter value
*/
l = l.multiply(1 - t);
r = r.multiply(t);
/*
* Give back their sum
*/
return TDPoint.add(l, r);
}
/**
* Split a curve into two curves at a specific point.
*
* @param t
* The point to split the curve at.
* @return The two curves, split at that point.
*/
public Bezier[] decompose(double t) {
Bezier[] curves = new Bezier[2];
{
/*
* Get the first curve.
*/
TDPoint[] points = new TDPoint[controls.size()];
for (int i = 0; i < points.length; i++) {
points[i] = evalIntern(t, i, 0);
}
curves[0] = new Bezier(points);
}
{
/*
* Get the second curve.
*/
TDPoint[] points = new TDPoint[controls.size()];
for (int i = 0; i < points.length; i++) {
points[i] = evalIntern(t, (points.length - 1) - i, i);
}
curves[1] = new Bezier(points);
}
return curves;
}
/**
* Get the bounding box for this curves control points.
*
* @return The bounding box for the control points.
*/
public TDPoint[] extrema() {
return extrema(new TDHIdentity());
}
/**
* Get the bounding box for a transformed version of this curves control points.
*
* @param transform
* The transform to apply to the control points.
* @return The bounding box for the transformed control points.
*/
public TDPoint[] extrema(TDHTransform transform) {
TDPoint[] box = new TDPoint[4];
double xMin = Double.MAX_VALUE, xMax = Double.MIN_VALUE;
double yMin = Double.MAX_VALUE, yMax = Double.MIN_VALUE;
for (TDPoint punkt1 : controls) {
/*
* Get transformed point
*/
TDPoint punkt = punkt1.multiply(data.scale);
punkt = transform.transform(punkt.toTDHPoint()).toTDPoint();
/*
* Set min/max x vals
*/
if (punkt.x < xMin) {
xMin = punkt.x;
}
if (punkt.x > xMax) {
xMax = punkt.x;
}
/*
* Set min/max y vals
*/
if (punkt.y < yMin) {
yMin = punkt.y;
}
if (punkt.y > yMax) {
yMax = punkt.y;
}
}
/*
* Create returned points.
*/
box[0] = new TDPoint(xMin, yMin);
box[1] = new TDPoint(xMax, yMin);
box[2] = new TDPoint(xMax, yMax);
box[3] = new TDPoint(xMin, yMax);
return box;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((controls == null) ? 0 : controls.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Bezier other = (Bezier) obj;
if (controls == null) {
if (other.controls != null)
return false;
} else if (!controls.equals(other.controls))
return false;
return true;
}
@Override
public String toString() {
return String.format("Bezier [controls=%s]", controls);
}
}
/**
* Represents a two-dimensional point.
*
* @author bjculkin
*
*/
class TDPoint {
/**
* The x coordinate.
*/
public final double x;
/**
* The y coordinate.
*/
public final double y;
/**
* Create a new two-dimensional point.
*
* @param x
* The x coordinate.
* @param y
* The y coordinate.
*/
public TDPoint(double x, double y) {
this.x = x;
this.y = y;
}
/**
* Create a new two-dimensional point.
*
* @param x
* The x coordinate.
* @param y
* The y coordinate.
* @return A point with those coordinates.
*/
public static TDPoint p2(double x, double y) {
return new TDPoint(x, y);
}
/**
* Return a new scaled point.
*
* @param s
* The amount to scale by.
* @return A point scaled by the specified amount.
*/
public TDPoint multiply(double s) {
return p2(x * s, y * s);
}
/**
* Add two points together.
*
* @param p1
* The first point to add.
* @param p2
* The second point to add.
* @return The sum of the points.
*/
public static TDPoint add(TDPoint p1, TDPoint p2) {
return p2(p1.x + p2.x, p1.y + p2.y);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(x);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(y);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
TDPoint other = (TDPoint) obj;
if (Double.doubleToLongBits(x) != Double.doubleToLongBits(other.x))
return false;
if (Double.doubleToLongBits(y) != Double.doubleToLongBits(other.y))
return false;
return true;
}
@Override
public String toString() {
return "TDPoint [x=" + x + ", y=" + y + "]";
}
/**
* Convert this point to a two-dimensional homogeneous point.
*
* @return A homogeneous version of this point.
*/
public TDHPoint toTDHPoint() {
return new TDHPoint(x, y);
}
}
/**
* A two-dimensional homogeneous point.
*
* @author bjculkin
*
*/
class TDHPoint extends TDPoint {
/**
* The homogeneous coordinate for the point.
*/
public final double z;
/**
* Create a new two-dimensional homogeneous point.
*
* @param x
* The x coordinate.
* @param y
* The y coordinate.
* @param z
* The homogeneous coordinate.
*/
public TDHPoint(double x, double y, double z) {
super(x, y);
this.z = z;
}
/**
* Create a new two-dimensional homogeneous point.
*
* The homogeneous coordinate is set to 1.
*
* @param x
* The x coordinate.
* @param y
* The y coordinate.
*/
public TDHPoint(double x, double y) {
this(x, y, 1);
}
/**
* Convert this point to a plain two-dimensional point.
*
* @return A two-dimensional version of this point.
*/
public TDPoint toTDPoint() {
/*
* Convert back down by dividing each coordinate by the homogeneous value.
*/
return new TDPoint(x / z, y / z);
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
long temp;
temp = Double.doubleToLongBits(z);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
TDHPoint other = (TDHPoint) obj;
if (Double.doubleToLongBits(z) != Double.doubleToLongBits(other.z))
return false;
return true;
}
@Override
public String toString() {
return "TDHPoint [z=" + z + ", x=" + x + ", y=" + y + "]";
}
}
/**
* Renderer for TDPoints in JLists.
*
* @author bjculkin
*
*/
final class TDPointRenderer extends JLabel implements ListCellRenderer<TDPoint> {
private static final long serialVersionUID = 629873168260730449L;
/**
* Create a new TDPoint renderer.
*/
public TDPointRenderer() {
setOpaque(true);
setHorizontalAlignment(CENTER);
setVerticalAlignment(CENTER);
}
@Override
public Component getListCellRendererComponent(JList<? extends TDPoint> list, TDPoint value, int index,
boolean isSelected, boolean cellHasFocus) {
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
setText(String.format("(%.2f, %.2f)", value.x, value.y));
return this;
}
}
/**
* Listener to remove points from a bezier curve.
*
* @author bjculkin
*
*/
class PointRemover implements ActionListener {
private final JFrame fram;
private final JList<TDPoint> pointList;
private final DefaultListModel<TDPoint> pointModel;
private Bezier curve;
/**
* Create a new listener to remove points from a bezier curve.
*
* @param fram
* The frame to use.
* @param pointList
* The list the points are stored in.
* @param pointModel
* The data backing the list.
* @param curveHolder
* The current curve.
*/
public PointRemover(JFrame fram, JList<TDPoint> pointList, DefaultListModel<TDPoint> pointModel,
Holder<Bezier> curveHolder) {
this.fram = fram;
this.pointList = pointList;
this.pointModel = pointModel;
/*
* Change our curve if the current one changes.
*/
curve = curveHolder.getVal();
curveHolder.addHolderListener((val) -> {
curve = val;
});
}
@Override
public void actionPerformed(ActionEvent ev) {
int selectedIndex = pointList.getSelectedIndex();
/*
* Nothing selected.
*/
if (selectedIndex == -1)
return;
TDPoint punkt = pointModel.get(selectedIndex);
String msg = String.format("Do you want to remove the control point (%.2f, %.2f)?", punkt.x, punkt.y);
int confirmed = JOptionPane.showConfirmDialog(fram, msg, "Remove Control Point?", JOptionPane.YES_NO_OPTION);
if (confirmed == JOptionPane.YES_OPTION) {
pointModel.remove(selectedIndex);
curve.controls.remove(punkt);
}
}
}
/**
* Listener for adding points to a bezier curve.
*
* @author bjculkin
*
*/
class PointAdder implements ActionListener {
private final DefaultListModel<TDPoint> pointModel;
private final JFrame fram;
private Bezier curve;
/**
* Create a listener that adds points to a bezier curve.
*
* @param pointModel
* The place to store points.
* @param curveHolder
* The curve to add to.
* @param fram
* The frame to use.
*/
public PointAdder(DefaultListModel<TDPoint> pointModel, Holder<Bezier> curveHolder, JFrame fram) {
this.pointModel = pointModel;
this.curve = curveHolder.getVal();
this.fram = fram;
/*
* Change our curve if the current one changes.
*/
curveHolder.addHolderListener((curv) -> {
curve = curv;
});
}
@Override
public void actionPerformed(ActionEvent ev) {
JDialog dia = new JDialog(fram);
dia.setTitle("Add Control Point");
dia.setModalityType(ModalityType.MODELESS);
dia.setLayout(new BorderLayout());
JPanel fields = new JPanel();
fields.setLayout(new GridLayout(2, 1));
LabeledInputPanel xPanel = new LabeledInputPanel("X Coordinate: ", 0.0);
LabeledInputPanel yPanel = new LabeledInputPanel("Y Coordinate: ", 0.0);
fields.add(xPanel);
fields.add(yPanel);
JPanel buttons = new JPanel();
buttons.setLayout(new GridLayout(1, 2));
JButton add = new JButton("Add Control Point");
AddListener addListener = new AddListener(xPanel, yPanel);
add.addActionListener(addListener);
JButton cancel = new JButton("Cancel");
cancel.addActionListener((aev) -> {
dia.dispose();
});
buttons.add(add);
buttons.add(cancel);
/*
* Change focus to each field on action
*/
xPanel.field.addActionListener((aev) -> {
yPanel.field.requestFocusInWindow();
});
yPanel.field.addActionListener((aev) -> {
addListener.actionPerformed(null);
});
dia.add(fields, BorderLayout.CENTER);
dia.add(buttons, BorderLayout.PAGE_END);
dia.pack();
dia.setVisible(true);
}
/**
* Listener for adding points to a curve.
*
* @author bjculkin
*
*/
class AddListener implements ActionListener {
private final LabeledInputPanel xPanel;
private final LabeledInputPanel yPanel;
public AddListener(LabeledInputPanel xPanel, LabeledInputPanel yPanel) {
this.xPanel = xPanel;
this.yPanel = yPanel;
}
@Override
public void actionPerformed(ActionEvent aev) {
/*
* Add point to curve.
*/
double xVal = (Double) xPanel.field.getValue();
double yVal = (Double) yPanel.field.getValue();
TDPoint punkt = new TDPoint(xVal, yVal);
pointModel.addElement(punkt);
curve.controls.add(punkt);
/*
* Reset field values.
*/
xPanel.field.setValue(0.0);
yPanel.field.setValue(0.0);
xPanel.field.requestFocusInWindow();
}
}
}
/**
* Listener to repaint a component whenever a list changes.
*
* @author bjculkin
*
*/
class CanvasRepainter implements ListDataListener {
private final JComponent comp;
/**
* Create a new repaint listener.
*
* @param canvas
* The component to repaint.
*/
public CanvasRepainter(JComponent canvas) {
this.comp = canvas;
}
@Override
public void intervalRemoved(ListDataEvent e) {
comp.repaint();
}
@Override
public void intervalAdded(ListDataEvent e) {
comp.repaint();
}
@Override
public void contentsChanged(ListDataEvent e) {
comp.repaint();
}
}
/**
* A component for getting formatted input with a label.
*
* @author bjculkin
*
*/
class LabeledInputPanel extends JPanel {
private static final long serialVersionUID = 1031310890698539040L;
/**
* The field input is stored in.
*/
public final JFormattedTextField field;
/**
* Create a new labeled input component.
*
* @param label
* The label for the component.
* @param val
* The initial value for the field.
*/
public LabeledInputPanel(String label, Object val) {
super();
setLayout(new BorderLayout());
JLabel xLabel = new JLabel(label);
field = new JFormattedTextField(val);
add(xLabel, BorderLayout.LINE_START);
add(field, BorderLayout.CENTER);
}
}
/**
* Dummy class for holding a value, and being notified when it changes.
*
* Essentially a pointer with change notifications.
*
* @author bjculkin
*
* @param <E>
* The type of held value.
*/
class Holder<E> {
/*
* The held value.
*/
private E val;
/*
* The listeners to notify.
*/
private final List<Consumer<E>> listeners;
/**
* Create a new holder holding nothing.
*/
public Holder() {
listeners = new LinkedList<>();
}
/**
* Create a new holder holding a specific value.
*
* @param val
* The value being held.
*/
public Holder(E val) {
this();
this.val = val;
}
/**
* Get the contained value.
*
* @return The contained value.
*/
public E getVal() {
return val;
}
/**
* Set the contained value, and notify listeners.
*
* @param val
* The new value.
*/
public void setVal(E val) {
this.val = val;
/*
* Notify listeners.
*/
for (Consumer<E> listen : listeners) {
listen.accept(val);
}
}
/**
* Add a listener.
*
* @param listen
* The listener to add.
*/
public void addHolderListener(Consumer<E> listen) {
listeners.add(listen);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((val == null) ? 0 : val.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Holder<?> other = (Holder<?>) obj;
if (val == null) {
if (other.val != null)
return false;
} else if (!val.equals(other.val))
return false;
return true;
}
@Override
public String toString() {
return "Holder [val=" + val + "]";
}
}
/**
* Types of transform to apply to TDHPoints.
*
* @author bjculkin
*
*/
enum TDHTransformType {
/**
* Coordinate translation.
*/
TRANSLATE,
/**
* Do nothing transform.
*/
IDENTITY,
/**
* Coordinate scaling.
*/
SCALE,
/**
* Coordinate rotation.
*/
ROTATION,
/**
* Coordinate reflection.
*/
REFLECTION,
/**
* Coordinate shearing.
*/
SHEAR,
/**
* Multiple transformations.
*/
COMBINATION,
/**
* Arbitrary matrix transformation.
*/
MATRIX
}
/**
* Transformation applicable to TDHPoints.
*
* @author bjculkin
*
*/
@FunctionalInterface
interface TDHTransform {
/**
* Get the type of this transform.
*
* Unknown transformations are assumed to be identity transforms.
*
* @return The type of this transform.
*/
default TDHTransformType type() {
return TDHTransformType.IDENTITY;
}
/**
* Get the matrix representation of the transform.
*
* Unknown transformations are assumed to be identity transforms.
*
* @return The matrix representation of the transform.
*/
default double[][] matrix() {
return new double[][] { new double[] { 1, 0, 0 }, new double[] { 0, 1, 0 }, new double[] { 0, 0, 1 } };
}
/**
* Get the inverse of the transform.
*
* Unknown transformations are assumed to be identity transforms.
*
* @return The inverse the transform.
*/
default TDHTransform invert() {
return new TDHIdentity();
}
/**
* Apply the transform to a point.
*
* @param punkt
* The point to transform.
* @return A transformed version of the point.
*/
TDHPoint transform(TDHPoint punkt);
}
/**
* A transform that does nothing.
*
* @author bjculkin
*
*/
class TDHIdentity implements TDHTransform {
@Override
public TDHPoint transform(TDHPoint punkt) {
return punkt;
}
@Override
public String toString() {
return "TDHIdentity []";
}
}
/**
* A transform that does coordinate translation.
*
* @author bjculkin
*
*/
class TDHTranslate implements TDHTransform {
/**
* The amount to translate the x-coordinate by.
*/
public final double h;
/**
* The amount to translate the y-coordinate by.
*/
public final double k;
/**
* Create a new translation transform.
*
* @param h
* The amount to translate x by.
* @param k
* The amount to translate y by.
*/
public TDHTranslate(double h, double k) {
this.h = h;
this.k = k;
}
@Override
public TDHTransformType type() {
return TDHTransformType.TRANSLATE;
}
@Override
public TDHPoint transform(TDHPoint punkt) {
double x = punkt.x + (punkt.z * h);
double y = punkt.y + (punkt.z * k);
return new TDHPoint(x, y, punkt.z);
}
@Override
public double[][] matrix() {
return new double[][] { new double[] { 1, 0, 0 }, new double[] { 0, 1, 0 }, new double[] { h, k, 1 } };
}
@Override
public TDHTransform invert() {
return new TDHTranslate(-h, -k);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(h);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(k);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
TDHTranslate other = (TDHTranslate) obj;
if (Double.doubleToLongBits(h) != Double.doubleToLongBits(other.h))
return false;
if (Double.doubleToLongBits(k) != Double.doubleToLongBits(other.k))
return false;
return true;
}
@Override
public String toString() {
return "TDHTranslate [h=" + h + ", k=" + k + "]";
}
}
/**
* Transform that does coordinate scaling.
*
* @author bjculkin
*
*/
class TDHScale implements TDHTransform {
/**
* Amount to scale x by.
*/
public final double sx;
/**
* Amount to scale y by.
*/
public final double sy;
/**
* Amount to scale the homogeneous coordinate by.
*/
public final double sz;
/**
* Create a new scaling that scales each coordinate by an equal amount
*
* @param factor
* The amount to scale the coordinates by.
*/
public TDHScale(double factor) {
this(factor, factor);
}
/**
* Create a new scaling that scales each coordinate separately.
*
* @param sx
* The amount to scale x by.
* @param sy
* The amount to scale y by.
*/
public TDHScale(double sx, double sy) {
this(sx, sy, 1);
}
/**
* Create a new scaling that scales each coordinate separately.
*
* Includes the homogeneous coordinate.
*
* @param sx
* The amount to scale x by.
* @param sy
* The amount to scale y by.
* @param sz
* The amount to scale the homogeneous coordinate by.
*/
public TDHScale(double sx, double sy, double sz) {
this.sx = sx;
this.sy = sy;
this.sz = sz;
}
@Override
public TDHTransformType type() {
return TDHTransformType.SCALE;
}
@Override
public TDHPoint transform(TDHPoint punkt) {
double x = punkt.x * sx;
double y = punkt.y * sy;
double z = punkt.z * sz;
return new TDHPoint(x, y, z);
}
@Override
public double[][] matrix() {
return new double[][] { new double[] { sx, 0, 0 }, new double[] { 0, sy, 0 }, new double[] { 0, 0, sz } };
}
public TDHTransform invert() {
return new TDHScale(1 / sx, 1 / sy, 1 / sy);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(sx);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(sy);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(sz);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
TDHScale other = (TDHScale) obj;
if (Double.doubleToLongBits(sx) != Double.doubleToLongBits(other.sx))
return false;
if (Double.doubleToLongBits(sy) != Double.doubleToLongBits(other.sy))
return false;
if (Double.doubleToLongBits(sz) != Double.doubleToLongBits(other.sz))
return false;
return true;
}
@Override
public String toString() {
return "TDHScale [sx=" + sx + ", sy=" + sy + ", sz=" + sz + "]";
}
}
class TDHRotation implements TDHTransform {
public final double theta;
public TDHRotation(double theta) {
this.theta = theta;
}
@Override
public TDHPoint transform(TDHPoint punkt) {
double x = (punkt.x * Math.cos(theta)) - (punkt.y * Math.sin(theta));
double y = (punkt.x * Math.sin(theta)) - (punkt.y * Math.cos(theta));
return new TDHPoint(x, y, punkt.z);
}
public double[][] matrix() {
return new double[][] { new double[] { Math.cos(theta), Math.sin(theta), 0 },
new double[] { -Math.sin(theta), Math.cos(theta), 0 }, new double[] { 0, 0, 1 } };
}
public TDHTransform invert() {
return new TDHRotation(-theta);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(theta);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
TDHRotation other = (TDHRotation) obj;
if (Double.doubleToLongBits(theta) != Double.doubleToLongBits(other.theta))
return false;
return true;
}
@Override
public String toString() {
return "TDHRotation [theta=" + theta + "]";
}
}
class TDHXAxisReflection implements TDHTransform {
@Override
public TDHTransformType type() {
return TDHTransformType.REFLECTION;
}
@Override
public TDHPoint transform(TDHPoint punkt) {
return new TDHPoint(punkt.x, -punkt.y, punkt.z);
}
@Override
public String toString() {
return "TDHXAxisReflection []";
}
}
class TDHYAxisReflection implements TDHTransform {
@Override
public TDHTransformType type() {
return TDHTransformType.REFLECTION;
}
@Override
public TDHPoint transform(TDHPoint punkt) {
return new TDHPoint(-punkt.x, punkt.y, punkt.z);
}
@Override
public String toString() {
return "TDHYAxisReflection []";
}
}
class TDHPointRotation extends TDHRotation {
public final double x0;
public final double y0;
public TDHPointRotation(double theta, double x0, double y0) {
super(theta);
this.x0 = x0;
this.y0 = y0;
}
@Override
public TDHTransformType type() {
return TDHTransformType.ROTATION;
}
@Override
public TDHPoint transform(TDHPoint punkt) {
double x1 = (punkt.x * Math.cos(theta)) - (punkt.y * Math.sin(theta));
double y1 = (punkt.x * Math.sin(theta)) - (punkt.y * Math.cos(theta));
double x2 = (-x0 * Math.cos(theta)) + (y0 * Math.sin(theta)) + x0;
double y2 = (-x0 * Math.sin(theta)) - (y0 * Math.cos(theta)) + y0;
double x = x1 + (punkt.z * x2);
double y = y1 + (punkt.z * y2);
return new TDHPoint(x, y, punkt.z);
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
long temp;
temp = Double.doubleToLongBits(x0);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(y0);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
TDHPointRotation other = (TDHPointRotation) obj;
if (Double.doubleToLongBits(x0) != Double.doubleToLongBits(other.x0))
return false;
if (Double.doubleToLongBits(y0) != Double.doubleToLongBits(other.y0))
return false;
return true;
}
@Override
public String toString() {
return "TDHPointRotation [x0=" + x0 + ", y0=" + y0 + "]";
}
}
class TDHLineReflection implements TDHTransform {
public final double a;
public final double b;
public final double c;
public TDHLineReflection(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
@Override
public TDHTransformType type() {
return TDHTransformType.REFLECTION;
}
@Override
public TDHPoint transform(TDHPoint punkt) {
double com = (a * a - b * b);
double x = (punkt.x * com) - (2 * a * b * punkt.y) - (2 * a * c * punkt.z);
double y = (-2 * a * b * punkt.x) + (punkt.y * com) - (2 * b * c * punkt.z);
double z = (punkt.z * com);
return new TDHPoint(x, y, z);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(a);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(b);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(c);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
TDHLineReflection other = (TDHLineReflection) obj;
if (Double.doubleToLongBits(a) != Double.doubleToLongBits(other.a))
return false;
if (Double.doubleToLongBits(b) != Double.doubleToLongBits(other.b))
return false;
if (Double.doubleToLongBits(c) != Double.doubleToLongBits(other.c))
return false;
return true;
}
@Override
public String toString() {
return "TDHLineReflection [a=" + a + ", b=" + b + ", c=" + c + "]";
}
}
class TDHShear implements TDHTransform {
public final double shx;
public final double shy;
@Override
public TDHTransformType type() {
return TDHTransformType.SHEAR;
}
public TDHShear(double shx, double shy) {
this.shx = shx;
this.shy = shy;
}
@Override
public TDHPoint transform(TDHPoint punkt) {
double x = punkt.x + (punkt.y * shx);
double y = punkt.y + (punkt.x * shy);
return new TDHPoint(x, y, punkt.z);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(shx);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(shy);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
TDHShear other = (TDHShear) obj;
if (Double.doubleToLongBits(shx) != Double.doubleToLongBits(other.shx))
return false;
if (Double.doubleToLongBits(shy) != Double.doubleToLongBits(other.shy))
return false;
return true;
}
@Override
public String toString() {
return "TDHShear [shx=" + shx + ", shy=" + shy + "]";
}
}
class TDHMatrix implements TDHTransform {
public final double[][] mat;
public TDHMatrix(double[][] mat) {
super();
this.mat = mat;
}
@Override
public TDHTransformType type() {
return TDHTransformType.MATRIX;
}
@Override
public TDHPoint transform(TDHPoint punkt) {
double x = (punkt.x * mat[0][0]) + (punkt.y * mat[1][0]) + (punkt.z * mat[2][0]);
double y = (punkt.x * mat[0][1]) + (punkt.y * mat[1][1]) + (punkt.z * mat[2][1]);
double z = (punkt.x * mat[0][2]) + (punkt.y * mat[1][2]) + (punkt.z * mat[2][2]);
return new TDHPoint(x, y, z);
}
public TDHTransform then(double[][] matr) {
double[][] ret = new double[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
ret[i][j] += mat[i][k] * matr[k][j];
}
}
}
return new TDHMatrix(ret);
}
}
class TDHCombination implements TDHTransform {
public final List<TDHTransform> forms;
@Override
public TDHTransformType type() {
return TDHTransformType.COMBINATION;
}
public TDHCombination(TDHTransform... forms) {
this.forms = new ArrayList<>(forms.length);
for (TDHTransform form : forms) {
this.forms.add(form);
}
}
@Override
public TDHPoint transform(TDHPoint punkt) {
TDHPoint ret = punkt;
for (TDHTransform form : forms) {
ret = form.transform(ret);
}
return ret;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((forms == null) ? 0 : forms.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
TDHCombination other = (TDHCombination) obj;
if (forms == null) {
if (other.forms != null)
return false;
} else if (!forms.equals(other.forms))
return false;
return true;
}
@Override
public String toString() {
return "TDHCombination [forms=" + forms + "]";
}
}
class BezierProperties {
/**
* The number of separate points to graph from the curve.
*/
public int parts = 100;
/**
* The multiplier to apply to coordinates.
*/
public double scale = 5;
/**
* The colors for varying parts of the curve.
*/
public Color curveColor = Color.BLACK;
public Color pointColor = Color.RED;
public Color boxColor = Color.GREEN;
public BezierProperties() {
}
public BezierProperties(int parts, double scale, Color curveColor, Color pointColor, Color boxColor) {
this.parts = parts;
this.scale = scale;
this.curveColor = curveColor;
this.pointColor = pointColor;
this.boxColor = boxColor;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((boxColor == null) ? 0 : boxColor.hashCode());
result = prime * result + ((curveColor == null) ? 0 : curveColor.hashCode());
result = prime * result + parts;
result = prime * result + ((pointColor == null) ? 0 : pointColor.hashCode());
long temp;
temp = Double.doubleToLongBits(scale);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
BezierProperties other = (BezierProperties) obj;
if (boxColor == null) {
if (other.boxColor != null)
return false;
} else if (!boxColor.equals(other.boxColor))
return false;
if (curveColor == null) {
if (other.curveColor != null)
return false;
} else if (!curveColor.equals(other.curveColor))
return false;
if (parts != other.parts)
return false;
if (pointColor == null) {
if (other.pointColor != null)
return false;
} else if (!pointColor.equals(other.pointColor))
return false;
if (Double.doubleToLongBits(scale) != Double.doubleToLongBits(other.scale))
return false;
return true;
}
@Override
public String toString() {
return "BezierProperties [parts=" + parts + ", scale=" + scale + ", curveColor=" + curveColor + ", pointColor="
+ pointColor + ", boxColor=" + boxColor + "]";
}
}
|