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:
Cơ chế Upcasting và Downcasting trong java
The “final” Keyword in Java
Comparing Objects in Java
Java Program to Find Nearest Neighbor Using Linear Search
Constructor Injection in Spring with Lombok
Java Program to Implement RenderingHints API
Hướng dẫn Java Design Pattern – Decorator
Các nguyên lý thiết kế hướng đối tượng – SOLID
Java Program to Find the Peak Element of an Array O(n) time (Naive Method)
Join and Split Arrays and Collections in Java
Java Program to Implement Extended Euclid Algorithm
Java Program to Describe the Representation of Graph using Incidence List
Java Program to Check Cycle in a Graph using Topological Sort
Java Program to Implement Dijkstra’s Algorithm using Set
Java Program to Generate Random Numbers Using Probability Distribution Function
Guide to Apache Commons CircularFifoQueue
Java Web Services – Jersey JAX-RS – REST và sử dụng REST API testing tools với Postman
Java Program to Implement First Fit Decreasing for 1-D Objects and M Bins
Java Program to Implement AttributeList API
Java Program to Test Using DFS Whether a Directed Graph is Strongly Connected or Not
Spring Boot - Enabling HTTPS
Command-Line Arguments in Java
New in Spring Security OAuth2 – Verify Claims
Spring Boot Change Context Path
ClassNotFoundException vs NoClassDefFoundError
Java Program to Implement Ternary Search Algorithm
Deploy a Spring Boot WAR into a Tomcat Server
Java Program to Implement Heap’s Algorithm for Permutation of N Numbers
Java Program to Implement Euclid GCD Algorithm
Use Liquibase to Safely Evolve Your Database Schema
Java Program to Implement Find all Forward Edges in a Graph
Spring @Primary Annotation