Skip to content

Commit e6b9925

Browse files
committed
Format
1 parent 03c80a3 commit e6b9925

File tree

5 files changed

+48
-50
lines changed

5 files changed

+48
-50
lines changed

com/williamfiset/algorithms/datastructures/linkedlist/DoublyLinkedList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public void addAt(int index, T data) throws Exception {
102102

103103
size++;
104104
}
105-
105+
106106
// Check the value of the first node if it exists, O(1)
107107
public T peekFirst() {
108108
if (isEmpty()) throw new RuntimeException("Empty list");

com/williamfiset/algorithms/geometry/ConvexPolygonCutWithLineSegment.java

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
2-
* This algorithm cuts a ordered convex polygon with a line segment and returns the two resulting pieces.
2+
* This algorithm cuts a ordered convex polygon with a line segment and returns the two resulting
3+
* pieces.
34
*
45
* <p>Time Complexity: O(nlogn)
56
*
@@ -8,7 +9,6 @@
89
package com.williamfiset.algorithms.geometry;
910

1011
import java.util.*;
11-
import java.lang.*;
1212

1313
public class ConvexPolygonCutWithLineSegment {
1414

@@ -28,27 +28,28 @@ public String toString() {
2828
return "(" + x + "," + y + ")";
2929
}
3030
}
31-
32-
33-
//sorts the points in CW direction.
34-
public static List<Pt> sortCW(List<Pt> poly){
35-
36-
int l = poly.size();
37-
double centroidX = 0;
38-
double centroidY = 0;
39-
for(int i = 0; i<l; i++){
40-
centroidX+= poly.get(i).x;
41-
centroidY+= poly.get(i).y;
42-
}
43-
centroidX = centroidX/l;
44-
centroidY = centroidY/l;
45-
Pt center = new Pt(centroidX, centroidY);
46-
47-
Collections.sort(poly, (a, b) -> {
48-
double a1 = (Math.toDegrees(Math.atan2(a.x - center.x, a.y - center.y)) + 360) % 360;
49-
double a2 = (Math.toDegrees(Math.atan2(b.x - center.x, b.y - center.y)) + 360) % 360;
50-
return (int) (a1 - a2);
51-
});
31+
32+
// sorts the points in CW direction.
33+
public static List<Pt> sortCW(List<Pt> poly) {
34+
35+
int l = poly.size();
36+
double centroidX = 0;
37+
double centroidY = 0;
38+
for (int i = 0; i < l; i++) {
39+
centroidX += poly.get(i).x;
40+
centroidY += poly.get(i).y;
41+
}
42+
centroidX = centroidX / l;
43+
centroidY = centroidY / l;
44+
Pt center = new Pt(centroidX, centroidY);
45+
46+
Collections.sort(
47+
poly,
48+
(a, b) -> {
49+
double a1 = (Math.toDegrees(Math.atan2(a.x - center.x, a.y - center.y)) + 360) % 360;
50+
double a2 = (Math.toDegrees(Math.atan2(b.x - center.x, b.y - center.y)) + 360) % 360;
51+
return (int) (a1 - a2);
52+
});
5253
return poly;
5354
}
5455

@@ -85,38 +86,38 @@ private static int orientation(double ax, double ay, double bx, double by, doubl
8586
double cross = bx * cy - by * cx;
8687
return cross < -EPS ? -1 : cross > EPS ? 1 : 0;
8788
}
88-
89-
//takes Pt[] as an argument and returns List<Pt>
90-
public static List<Pt> makeList(Pt[] squarePolygon){
89+
90+
// takes Pt[] as an argument and returns List<Pt>
91+
public static List<Pt> makeList(Pt[] squarePolygon) {
9192
List<Pt> list = new ArrayList<Pt>();
92-
for (int i=0; i<squarePolygon.length; i++){
93-
list.add(squarePolygon[i]);
93+
for (int i = 0; i < squarePolygon.length; i++) {
94+
list.add(squarePolygon[i]);
9495
}
9596
return list;
9697
}
97-
98-
//takes List<Pt> as an argument and returns Pt[]
99-
public static Pt[] makeArray(List<Pt> list){
100-
int l = list.size();
101-
Pt[] temp = new Pt[l];
102-
for (int i=0; i<l; i++){
103-
temp[i] = list.get(i);
98+
99+
// takes List<Pt> as an argument and returns Pt[]
100+
public static Pt[] makeArray(List<Pt> list) {
101+
int l = list.size();
102+
Pt[] temp = new Pt[l];
103+
for (int i = 0; i < l; i++) {
104+
temp[i] = list.get(i);
104105
}
105106
return temp;
106107
}
107108

108109
// Example usage
109110
public static void main(String[] args) {
110-
111+
111112
Pt[] squarePolygon = {new Pt(0, 0), new Pt(0, 4), new Pt(4, 0), new Pt(4, 4), new Pt(0, 2)};
112113
Pt p1 = new Pt(-1, -1);
113114
Pt p2 = new Pt(5, 5);
114-
115+
115116
int l = squarePolygon.length;
116117
List list = makeList(squarePolygon);
117118
list = sortCW(list);
118119
squarePolygon = makeArray(list);
119-
120+
120121
Pt[] poly1 = cut(squarePolygon, p1, p2);
121122
Pt[] poly2 = cut(squarePolygon, p2, p1);
122123

@@ -131,7 +132,7 @@ public static void main(String[] args) {
131132

132133
System.out.println("\nSecond polygon:");
133134
for (Pt pt : poly2) System.out.println(pt);
134-
//Prints:
135+
// Prints:
135136
// Second polygon:
136137
// (4.0,4.0)
137138
// (4.0,0.0)

com/williamfiset/algorithms/graphtheory/treealgorithms/TreeIsomorphism.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
/**
2-
* Determines if two unrooted trees are isomorphic. This algorithm
3-
* can easily be modified to support checking if two rooted trees are
4-
* isomorphic.
1+
/**
2+
* Determines if two unrooted trees are isomorphic. This algorithm can easily be modified to support
3+
* checking if two rooted trees are isomorphic.
54
*
65
* <p>Tested code against: https://uva.onlinejudge.org/external/124/p12489.pdf
76
*
87
* @author William Fiset, william.alexandre.fiset@gmail.com
98
*/
10-
119
package com.williamfiset.algorithms.graphtheory.treealgorithms;
1210

1311
import java.util.*;
@@ -175,4 +173,4 @@ public static void main(String[] args) {
175173
System.out.println("Oops something is not right.");
176174
}
177175
}
178-
}
176+
}

javatests/com/williamfiset/algorithms/datastructures/linkedlist/LinkedListTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void testAddLast() {
6262
list.addLast(5);
6363
assertEquals(list.size(), 2);
6464
}
65-
65+
6666
@Test
6767
public void testAddAt() throws Exception {
6868
list.addAt(0, 1);

javatests/com/williamfiset/algorithms/graphtheory/treealgorithms/TreeIsomorphismTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// To run this test in isolation from root folder:
22
//
3-
// $ gradle test --tests javatests.com.williamfiset.algorithms.graphtheory.treealgorithms.TreeIsomorphismTest
3+
// $ gradle test --tests
4+
// javatests.com.williamfiset.algorithms.graphtheory.treealgorithms.TreeIsomorphismTest
45

56
package javatests.com.williamfiset.algorithms.graphtheory.treealgorithms;
67

78
import static com.google.common.truth.Truth.assertThat;
8-
99
import static com.williamfiset.algorithms.graphtheory.treealgorithms.TreeIsomorphism.addUndirectedEdge;
1010
import static com.williamfiset.algorithms.graphtheory.treealgorithms.TreeIsomorphism.createGraph;
1111
import static com.williamfiset.algorithms.graphtheory.treealgorithms.TreeIsomorphism.treesAreIsomorphic;
@@ -125,5 +125,4 @@ public void differentNumberOfNodes() {
125125

126126
assertThat(treesAreIsomorphic(tree1, tree2)).isEqualTo(false);
127127
}
128-
129128
}

0 commit comments

Comments
 (0)