This is a Java Program to Implement Jarvis Algorithm. Jarvis algorithm or the gift wrapping algorithm is an algorithm for computing the convex hull of a given set of points.
Here is the source code of the Java Program to Implement Jarvis Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
/**
** Java Program to Implement Jarvis Algorithm
**/
import java.util.Scanner;
import java.util.Arrays;
/** Class point **/
class Point
{
int x, y;
}
/** Class Jarvis **/
public class Jarvis
{
private boolean CCW(Point p, Point q, Point r)
{
int val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
if (val >= 0)
return false;
return true;
}
public void convexHull(Point[] points)
{
int n = points.length;
/** if less than 3 points return **/
if (n < 3)
return;
int[] next = new int[n];
Arrays.fill(next, -1);
/** find the leftmost point **/
int leftMost = 0;
for (int i = 1; i < n; i++)
if (points[i].x < points[leftMost].x)
leftMost = i;
int p = leftMost, q;
/** iterate till p becomes leftMost **/
do
{
/** wrapping **/
q = (p + 1) % n;
for (int i = 0; i < n; i++)
if (CCW(points[p], points[i], points[q]))
q = i;
next[p] = q;
p = q;
} while (p != leftMost);
/** Display result **/
display(points, next);
}
public void display(Point[] points, int[] next)
{
System.out.println("\nConvex Hull points : ");
for (int i = 0; i < next.length; i++)
if (next[i] != -1)
System.out.println("("+ points[i].x +", "+ points[i].y +")");
}
/** Main function **/
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Jarvis Algorithm Test\n");
/** Make an object of Jarvis class **/
Jarvis j = new Jarvis();
System.out.println("Enter number of points n :");
int n = scan.nextInt();
Point[] points = new Point[n];
System.out.println("Enter "+ n +" x, y cordinates");
for (int i = 0; i < n; i++)
{
points[i] = new Point();
points[i].x = scan.nextInt();
points[i].y = scan.nextInt();
}
j.convexHull(points);
}
}
Jarvis Algorithm Test Enter number of points n : 8 Enter 8 x, y cordinates 0 3 4 2 3 5 5 3 3 0 1 1 1 2 2 2 Convex Hull points : (0, 3) (3, 5) (5, 3) (3, 0) (1, 1)
Related posts:
Lớp Collections trong Java (Collections Utility Class)
Java – Write a Reader to File
Updating your Password
Java Program to Perform LU Decomposition of any Matrix
Java Program to Implement Counting Sort
Java Program to Check Whether a Given Point is in a Given Polygon
Java Program to Check Whether an Undirected Graph Contains a Eulerian Path
Spring 5 Testing with @EnabledIf Annotation
Hướng dẫn Java Design Pattern – Command
Java Program to Implement First Fit Decreasing for 1-D Objects and M Bins
Netflix Archaius with Various Database Configurations
Hướng dẫn Java Design Pattern – Singleton
Using Java Assertions
Guide to PriorityBlockingQueue in Java
Spring Boot - Cloud Configuration Server
Java Program to Implement Gale Shapley Algorithm
OAuth2 for a Spring REST API – Handle the Refresh Token in Angular
Java Program to Implement Quick Sort with Given Complexity Constraint
ETags for REST with Spring
Java Program to Find the Longest Path in a DAG
Guide to Mustache with Spring Boot
Java Program to Implement wheel Sieve to Generate Prime Numbers Between Given Range
Java Program to implement Bit Set
Using JWT with Spring Security OAuth
Guide to CopyOnWriteArrayList
Java 8 Streams peek() API
Adding Shutdown Hooks for JVM Applications
Java Program to Check whether Graph is a Bipartite using 2 Color Algorithm
Compact Strings in Java 9
Allow user:password in URL
Converting Iterator to List
Jackson Ignore Properties on Marshalling