Skip to content

Commit 9b2da4e

Browse files
crafter312PudottaPommin
authored andcommitted
Jarvis March Implementation in Java (#354)
1 parent 20576b6 commit 9b2da4e

File tree

3 files changed

+115
-1
lines changed

3 files changed

+115
-1
lines changed

CONTRIBUTORS.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
This file lists everyone, who contributed to this repo and wanted to show up here. If you're looking for information on contribution, please check the `CONTRIBUTING.md` out.
1+
This file lists everyone, who contributed to this repo and wanted to show up here. If you're looking for information on contribution, please check the `CONTRIBUTING.md` out.
22

33
# Contributors
44

@@ -37,3 +37,4 @@ This file lists everyone, who contributed to this repo and wanted to show up her
3737
- Ken Power
3838
- PaddyKe
3939
- nic-hartley
40+
- crafter312
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import java.util.*;
2+
3+
public class JarvisMarch {
4+
5+
static class Point {
6+
private double x;
7+
private double y;
8+
9+
public Point(double a, double b) {
10+
x = a;
11+
y = b;
12+
}
13+
14+
public double getX() {
15+
return x;
16+
}
17+
public double getY() {
18+
return y;
19+
}
20+
21+
public boolean equals(Point p) {
22+
if (p.getX() == x && p.getY() == y) {
23+
return true;
24+
} else {
25+
return false;
26+
}
27+
}
28+
public double magnitude() {
29+
return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
30+
}
31+
}
32+
33+
//find the angle by creating two vectors and then using a property of dot products
34+
private static double angle(Point a, Point b, Point c) {
35+
Point ab = new Point(b.getX() - a.getX(), b.getY() - a.getY());
36+
Point bc = new Point(c.getX() - b.getX(), c.getY() - b.getY());
37+
return Math.acos(-1 * ((ab.getX() * bc.getX()) + (ab.getY() * bc.getY())) /
38+
(ab.magnitude() * bc.magnitude()));
39+
}
40+
41+
public static ArrayList<Point> jarvisMarch(ArrayList<Point> arr) {
42+
ArrayList<Point> hull = new ArrayList<Point>();
43+
Point pointOnHull = new Point(Double.MAX_VALUE, 0);
44+
45+
//find leftmost point
46+
for (Point p: arr) {
47+
if (p.getX() < pointOnHull.getX()) {
48+
pointOnHull = p;
49+
}
50+
}
51+
hull.add(pointOnHull);
52+
53+
//look for the rest of the points on the hull
54+
Point ref;
55+
while (true) {
56+
if (hull.size() == 1) {
57+
ref = new Point(pointOnHull.getX(), pointOnHull.getY() + 1); //finds a third point to use in calculating the angle
58+
} else {
59+
ref = hull.get(hull.size() - 2);
60+
}
61+
Point endpoint = arr.get(0); //initial canidate for next point in hull
62+
for (Point p: arr) {
63+
if (angle(p, pointOnHull, ref) > angle(endpoint, pointOnHull, ref)) { //found a point that makes a greater angle
64+
endpoint = p;
65+
}
66+
}
67+
pointOnHull = endpoint;
68+
if (pointOnHull.equals(hull.get(0))) { //add next point to hull if not equal to the leftmost point
69+
break;
70+
} else {
71+
hull.add(pointOnHull);
72+
}
73+
}
74+
return hull;
75+
}
76+
77+
public static void main(String[] args) {
78+
79+
//test array setup
80+
ArrayList<Point> gift = new ArrayList<Point>();
81+
gift.add(new Point(-5, 2));
82+
gift.add(new Point(5, 7));
83+
gift.add(new Point(-6, -12));
84+
gift.add(new Point(-14, -14));
85+
gift.add(new Point(9, 9));
86+
gift.add(new Point(-1, -1));
87+
gift.add(new Point(-10, 11));
88+
gift.add(new Point(-6, 15));
89+
gift.add(new Point(-6, -8));
90+
gift.add(new Point(15, -9));
91+
gift.add(new Point(7, -7));
92+
gift.add(new Point(-2, -9));
93+
gift.add(new Point(6, -5));
94+
gift.add(new Point(0, 14));
95+
gift.add(new Point(2, 8));
96+
97+
//print initial array of points
98+
System.out.println("Gift:");
99+
for (Point p: gift) {
100+
System.out.println("[" + p.getX() + ", " + p.getY() + "]");
101+
}
102+
103+
//find and print the array of points in the hull
104+
ArrayList<Point> hull = jarvisMarch(gift);
105+
System.out.println("Wrapping:");
106+
for (Point p: hull) {
107+
System.out.println("[" + p.getX() + ", " + p.getY() + "]");
108+
}
109+
}
110+
111+
}

contents/jarvis_march/jarvis_march.md

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ Since this algorithm, there have been many other algorithms that have advanced t
4040
[import, lang:"python"](code/python/jarvisMarch.py)
4141
{% sample lang="cpp" %}
4242
[import, lang:"c_cpp"](code/c++/jarvis_march.cpp)
43+
{% sample lang="java" %}
44+
[import, lang:"java"](code/java/JarvisMarch.java)
4345
{% sample lang="go" %}
4446
[import, lang:"go"](code/golang/jarvis.go)
4547
{% endmethod %}

0 commit comments

Comments
 (0)