-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessApp2.java
580 lines (511 loc) · 16.3 KB
/
ProcessApp2.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
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
import java.util.*;
import java.awt.geom.*;
import java.awt.Polygon;
import java.text.*;
public class ProcessApp{
private int maxX = 0;
private int maxY = 0;
private boolean polygonStat = false;
private PointExt queryPoint = new PointExt();
private PointList pointContainer = new PointList();
private PointList sortedContainer = new PointList();
private PointList vertexContainer = new PointList();
private PointList sortedVertex = new PointList();
private LineList perpendicularList = new LineList();
private LineList sortedLine = new LineList();
private LineList euclidianList = new LineList();
private Path2D.Double polygon = new Path2D.Double();
private Polygon poly = new Polygon();
/**
CONSTRUCTOR
**/
public ProcessApp(){
}
public ProcessApp(PointList pointContainer, PointExt queryPoint){
this.pointContainer = pointContainer;
this.queryPoint = queryPoint;
}
public ProcessApp(PointList pointContainer, PointExt queryPoint, int x, int y){
this.pointContainer = pointContainer;
this.queryPoint = queryPoint;
this.maxX = x;
this.maxY = y;
}
/**
MAIN
*/
public static void main(String[] args) {
}
/**
Encapsulation
*/
public boolean getPolygonStat(){return this.polygonStat;}
public PointList getVertex(){return this.vertexContainer;}
public PointExt getQueryPoint(){return this.queryPoint;}
public PointList getPointContainer(){return this.pointContainer;}
public PointList getSortedContainer(){return this.sortedContainer;}
public PointList getVertexContainer(){return this.vertexContainer;}
public PointList getSortedVertex(){return this.sortedVertex;}
public LineList getPerpendicularList(){return this.perpendicularList;}
public LineList getLine(){return this.perpendicularList;}
public LineList getSortedLine(){return this.sortedLine;}
public LineList getEuclidianList(){return this.euclidianList;}
public Path2D.Double getPolygon(){return this.polygon;}
public Polygon getPoly(){return this.poly;}
public void setMax(int x, int y){
this.maxX = x;
this.maxY = y;
}
public void setPointContainer(PointList pointContainer){
this.pointContainer = pointContainer;
}
public void setQueryPoint(PointExt queryPoint){
this.queryPoint = queryPoint;
}
/**
PRINT POINT RELATED
*/
public void printContainer(){
this.getPointContainer().printPoint();
}
public void printQueryPoint(){
System.out.print("Query Point : "+this.getQueryPoint().printPoint());
}
/**
PROCESS
*/
// SORTING
public void sortingContainer(){
if (this.getQueryPoint().getName() == "") {
return;
}
this.getSortedContainer().add(this.getQueryPoint());
for (int i = 0;i<this.getPointContainer().size();i++ ) {
if (!(this.getPointContainer().get(i).getName()==this.getQueryPoint().getName())) {
this.getSortedContainer().add(this.getPointContainer().get(i));
}
}
this.bubbleSort(this.getSortedContainer());
}
public void bubbleSort(PointList a){
int n = a.size();
boolean swapped = false;
for (int i=0;i<n-1 ;i++ ) {
for (int j = i+1;j<n ;j++) {
if (a.get(i).distance(a.get(0))>a.get(j).distance(a.get(0))) {
PointExt temp = a.get(i);
a.set(i, a.get(j));
a.set(j, temp);
}
}
}
}
// STEP 1 PROCESS
public void startProcess(){
this.sortingContainer();
this.lineProcess();
//this.getVertexContainer().printLine();
/*this.jarvisMarch(this.getVertexContainer());*/
this.getSortedVertex().printLine();
/*System.out.println(getPolygonStat());*/
if (this.getPolygonStat()) {
this.createPolygon(this.getSortedVertex());
this.createPoly(this.getSortedVertex());
}
}
public void lineProcess(){
PointExt a = this.getQueryPoint();
PointExt b = null;
LineExt tempLine = null;
for (int i = 1; i<this.getSortedContainer().size();i++ ) {
b = this.getSortedContainer().get(i);
if (this.getPerpendicularList().size()<3) {
tempLine = this.createBisector(a, b);
tempLine.setName(b.getName());
boolean flag = true;
for (int j = 0;j<this.getPerpendicularList().size() ;j++ ) {
if (this.checkParalel(tempLine, this.getPerpendicularList().get(j))) {
flag = false;
}
}
//System.out.println(flag);
if(flag){
this.getPerpendicularList().add(tempLine);
}
if(this.getPerpendicularList().size()==3){
if (this.findClosedPolygon()) {
this.polygonStat = true;
}
}
}else{
if (this.findClosedPolygon()){
this.polygonStat = true;
}else{
tempLine = this.createBisector(a, b);
tempLine.setName(b.getName());
boolean flag = true;
for (int j = 0;j<this.getPerpendicularList().size() ;j++ ){
if (this.checkParalel(tempLine, this.getPerpendicularList().get(j))) {
flag = false;
}
}
if(flag){
this.getPerpendicularList().add(tempLine);
}
if (this.findClosedPolygon()) {
this.polygonStat = true;
}
}
}
}
}
public boolean findClosedPolygon(){
boolean closed = false;
if (this.checkParalel(this.getPerpendicularList().get(0), this.getPerpendicularList().get(this.getPerpendicularList().size()-1))) {
closed = false; // check paralel atau tidaknya garis pertama dan terakhir.
}else{
if (this.checkInitPosition()) {
closed = true;
}
}
return closed;
}
public boolean checkInitPosition(){
// Line Sort berdasarkan mana yang saling berpotongan
LineExt tempLine = null;
for (LineExt temp : this.getPerpendicularList()) {
this.getSortedLine().add(temp);
}
for (int i=0;i<this.getSortedLine().size()-1;i++) {
for (int j=0;j<this.getSortedLine().size();j++ ) {
if (this.getSortedLine().get(i).intersectsLine(this.getSortedLine().get(j))) {
tempLine = this.getSortedLine().get(j);
this.getSortedLine().remove(j);
this.getSortedLine().add(i, tempLine);
}
}
}
this.generateVertex();
PointList flag = new PointList();
LineExt checkLine = null;
PointExt checkPoint = null;
boolean flagVertex = false;
// Debug Here
for ( PointExt temp : this.getVertexContainer() ) {
checkLine = new LineExt(temp, this.getQueryPoint()); // Garis antara query point dengan vertex
checkLine.setName("l("+temp.getName()+":"+this.getQueryPoint().getName()+")");
flagVertex = false;
for (LineExt bisect : this.getPerpendicularList()) { // Untuk setiap bisector
if (checkLine.intersectsLine(bisect)) { // Check apakah melewati suatu bisector
checkPoint = this.getIntersectionPoint(checkLine, bisect);
double ax = this.roundDouble(checkPoint.getX());
double ay = this.roundDouble(checkPoint.getY());
double bx = this.roundDouble(temp.getX());
double by = this.roundDouble(temp.getY());
if ((ax==bx)&&(ay==by)) {
flagVertex = false;
}else{
flagVertex = true;
break;
}
}
}
if (flagVertex) {
flag.add(temp);
}
}
for (PointExt temp : flag ) {
if (this.getVertexContainer().contains(temp)) {
int i = this.getVertexContainer().indexOf(temp);
this.getVertexContainer().remove(i);
}
}
this.getSortedVertex().clear();
this.jarvisMarch(this.getVertexContainer());
Path2D.Double path = new Path2D.Double();
path.moveTo(this.getSortedVertex().get(0).getX(), this.getSortedVertex().get(0).getY());
for (int i = 1;i<this.getSortedVertex().size();i++) {
path.lineTo(this.getSortedVertex().get(i).getX(), this.getSortedVertex().get(i).getY());
}
path.closePath();
if (path.contains(this.getQueryPoint())) {
return true;
}else{
return false;
}
}
/**
Draw related.
*/
public void createPolygon(PointList a){
double x = (double) a.get(0).getX();
double y = (double) a.get(0).getY();
this.getPolygon().moveTo(x, y);
for (int i = 1;i<a.size() ;i++ ) {
x = (double) a.get(i).getX();
y = (double) a.get(i).getY();
this.getPolygon().lineTo(x, y);
}
x = (double) a.get(0).getX();
y = (double) a.get(0).getY();
this.getPolygon().moveTo(x, y);
this.getPolygon().closePath();
}
public void createPoly(PointList a){
/*System.out.println(a.size());*/
int[] xPoint = new int[a.size()];
int[] yPoint = new int[a.size()];
int n = a.size();
for (int i=0; i<a.size(); i++ ) {
xPoint[i] = (int) a.get(i).getX();
yPoint[i] = (int) a.get(i).getY();
}
poly = new Polygon(xPoint, yPoint, n);
}
public LineExt createBisector(PointExt a, PointExt b){
LineExt line = new LineExt();
PointExt tempPoint = new PointExt(((b.getX()+a.getX())/2),((b.getY()+a.getY())/2));
if (a.getX() == b.getX()) {
line.setLine(0, tempPoint.getY(), this.maxX, tempPoint.getY());
}else if(a.getY() == b.getY()) {
line.setLine(tempPoint.getX(), 0, tempPoint.getX(), this.maxY);
}else{
LineExt tempLine = null;
if (a.getX()<b.getX()) {
tempLine = new LineExt(a,b);
}else{
tempLine = new LineExt(b,a);
}
double m = -1 / tempLine.getM();
double y1 = 0;
double y2 = maxY;
double x1 = (((y1-tempPoint.getY())+(m*tempPoint.getX()))/m);
double x2 = (((y2-tempPoint.getY())+(m*tempPoint.getX()))/m);
line.setLine(x1, y1, x2, y2);
}
return line;
}
public void generateVertex(){
for (int i = 0; i<this.getPerpendicularList().size()-1; i++) {
for (int j = i+1; j<this.getPerpendicularList().size() ; j++) {
if (this.getPerpendicularList().get(i).intersectsLine(this.getPerpendicularList().get(j))) {
String name = "v("+this.getPerpendicularList().get(i).getName()+":"+this.getPerpendicularList().get(j).getName()+")";
PointExt p = new PointExt(name, this.getIntersectionPoint(this.getPerpendicularList().get(i), this.getPerpendicularList().get(j)));
if (!this.getVertexContainer().contains(p)) {
this.getVertexContainer().add(name, p);
}
}else if (!checkParalel(this.getPerpendicularList().get(i), this.getPerpendicularList().get(j))) {
PointExt[] p = new PointExt[2];
p[0] = new PointExt(this.getPerpendicularList().get(i).getP1());
p[1] = new PointExt(this.getPerpendicularList().get(i).getP2());
LineExt l = new LineExt(p[0], this.getQueryPoint());
double[] d = new double[2];
d[0] = this.angleOfTwoLines(this.getPerpendicularList().get(i), l);
l = new LineExt(p[1], this.getQueryPoint());
d[1] = this.angleOfTwoLines(this.getPerpendicularList().get(i), l);
if (d[0]<d[1]) {
if (!this.getVertexContainer().contains(p[0])) {
this.getVertexContainer().add(p[0]);
}
}else{
if (!this.getVertexContainer().contains(p[1])) {
this.getVertexContainer().add(p[1]);
}
}
p[0] = new PointExt(this.getPerpendicularList().get(j).getP1());
p[1] = new PointExt(this.getPerpendicularList().get(j).getP2());
l = new LineExt(p[0], this.getQueryPoint());
d[0] = this.angleOfTwoLines(this.getPerpendicularList().get(i), l);
l = new LineExt(p[1], this.getQueryPoint());
d[1] = this.angleOfTwoLines(this.getPerpendicularList().get(i), l);
if (d[0]<d[1]) {
if (!this.getVertexContainer().contains(p[0])) {
this.getVertexContainer().add(p[0]);
}
}else{
if (!this.getVertexContainer().contains(p[1])) {
this.getVertexContainer().add(p[1]);
}
}
}
}
}
}
/**
Utilities
*/
public void jarvisMarch(PointList a){
// converting to array to reduce overhead in operation
PointExt[] q = a.toArray();
int currPoint = 0;
int minPoint = 0;
int maxPoint = 0;
int minAngle = 0;
int maxAngle = 0;
int[] usedPoint = new int[q.length];
for (int i=0;i<usedPoint.length ; i++) {
usedPoint[i] = -1;
}
for (int i=0; i<q.length ; i++ ) {
if (q[i].getY()>q[maxPoint].getY()) {
maxPoint = i; // Find Max Point *highest y
}
}
for (int i=0;i<q.length ; i++ ) {
if (q[i].getY()<q[minPoint].getY()) {
minPoint = i;
}
}
this.addUsedPoint(usedPoint, minPoint);
//System.out.println(q[minPoint].getName()+" : "+q[maxPoint].getName());
currPoint = minPoint;
while(currPoint!=maxPoint){
maxAngle = currPoint;
for (int i=0;i<q.length ;i++ ) {
//System.out.println("Angle : "+findAngle(q[currPoint], q[maxAngle])+" : "+findAngle(q[currPoint], q[i]));
if ((findAngle(q[currPoint], q[maxAngle])<findAngle(q[currPoint],q[i])) && (notUsed(usedPoint, i)||i==maxPoint) && (findAngle(q[currPoint], q[i])<=180)) {
maxAngle = i;
//System.out.println("maxAngle : "+q[i].getName());
}
}
currPoint = maxAngle;
this.addUsedPoint(usedPoint, currPoint);
}
//System.out.println("Left Side Completed");
//currPoint = minPoint;
int z = 0;
currPoint = maxPoint;
while (currPoint!=minPoint) {
minAngle = minPoint;
for (int i = 0; i<q.length ; i++ ) {
if ((findAngle(q[currPoint], q[minAngle])<findAngle(q[currPoint], q[i])) && (notUsed(usedPoint, i)) &&(findAngle(q[currPoint], q[i])>=180)) {
minAngle = i;
}
}
currPoint = minAngle;
this.addUsedPoint(usedPoint, currPoint);
}
for (int i = 0;i<usedPoint.length ;i++ ) {
this.getSortedVertex().add(q[usedPoint[i]]);
}
/*while (currPoint!=maxPoint) {
minAngle = maxPoint;
for (int i=0;i<q.length ;i++ ) {
if (q[i].getName().equals("v(E:D)")) {
System.out.println(q[currPoint].getName());
System.out.println("Angle : "+findAngle(q[currPoint], q[minAngle])+" : "+findAngle(q[currPoint], q[i])+":"+(notUsed(usedPoint,i)));
}
if ((findAngle(q[currPoint], q[minAngle])>findAngle(q[currPoint], q[i])) && (notUsed(usedPoint,i)||i==maxPoint) && (findAngle(q[currPoint], q[i])>=0)) {
minAngle = i;
}
}
currPoint = minAngle;
this.addUsedPoint(usedPoint, currPoint);
}
if (usedPoint.length % 2 == 0) {
System.out.println("true");
for (int i=0;i<=(usedPoint.length/2) ; i++) {
this.getSortedVertex().add(q[usedPoint[i]]);
}
for (int i = usedPoint.length-1; i>(usedPoint.length/2) ; i-- ) {
this.getSortedVertex().add(q[usedPoint[i]]);
}
}else{
System.out.println("false");
for (int i=0;i<=(usedPoint.length/2) ; i++) {
this.getSortedVertex().add(q[usedPoint[i]]);
}
for (int i = usedPoint.length-1; i>(usedPoint.length/2) ; i-- ) {
this.getSortedVertex().add(q[usedPoint[i]]);
}
}*/
/*for (int i=0;i<usedPoint.length ;i++ ) {
this.getSortedVertex().add(q[usedPoint[i]]);
}*/
}
public boolean notUsed(int[] arr, int a){
for (int i = 0; i<arr.length; i++ ) {
if (arr[i]==a) {
return false;
}
}
return true;
}
public void addUsedPoint(int[] arr, int a){
int i = 0;
while ((arr[i]!=-1)&&(i<arr.length)) {
i++;
if(i == arr.length){
break;
}
}
if (i<arr.length) {
arr[i] = a;
//System.out.println(this.getVertexContainer().get(a).getName()+" added at index "+i);
}
}
public double findAngle(double x1, double x2, double y1, double y2){
double deltaX = (double) (x2-x1);
double deltaY = (double) (y2-y1);
double angle;
if (deltaX == 0 && deltaY == 0) {
return 0;
}
angle = Math.toDegrees(Math.atan2(deltaY, deltaX));
if (angle < 0) {
angle += 360.0;
}
return angle;
}
public double findAngle(PointExt p1, PointExt p2){
double angle = this.findAngle(p1.getX(), p2.getX(), p1.getY(), p2.getY());
return angle;
}
public static double angleOfTwoLines(LineExt line1, LineExt line2){
double angle1 = Math.atan2(line1.getY1() - line1.getY2(),
line1.getX1() - line1.getX2());
double angle2 = Math.atan2(line2.getY1() - line2.getY2(),
line2.getX1() - line2.getX2());
angle1 = angle1-angle2;
if (angle1<0) {
angle1 = -angle1;
}
return angle1;
}
public boolean contains(int[] a, int idx){
for (int i=0; i<a.length ; i++ ) {
if (a[i]==idx) {
return false;
}
}
return true;
}
public boolean checkParalel(LineExt a, LineExt b){
if (a.getM() == b.getM()) {
return true;
}
return false;
}
public double roundDouble(double d){
DecimalFormat a = new DecimalFormat("#.####");
return Double.valueOf(a.format(d));
}
public PointExt getIntersectionPoint(LineExt lineA, LineExt lineB){
double x1 = lineA.getX1();
double y1 = lineA.getY1();
double x2 = lineA.getX2();
double y2 = lineA.getY2();
double x3 = lineB.getX1();
double y3 = lineB.getY1();
double x4 = lineB.getX2();
double y4 = lineB.getY2();
PointExt p = null;
double d = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
if (d != 0) {
double xi = ((x3 - x4) * (x1 * y2 - y1 * x2) - (x1 - x2) * (x3 * y4 - y3 * x4)) / d;
double yi = ((y3 - y4) * (x1 * y2 - y1 * x2) - (y1 - y2) * (x3 * y4 - y3 * x4)) / d;
p = new PointExt(xi, yi);
}
return p;
}
}