You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@author ADMIN
*/
public class Jarvis {
private static Point[] points;
private static 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 static 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 static void display(Point[] points, int[] next) {
System.out.println("\nJarvis March Convex Hull points : ");
for (int i = 0; i < next.length; i++) {
if (next[i] != -1) {
System.out.println("(" + points[i].x + ", " + points[i].y + ")");
}
}
}
/**
@param args the command line arguments
*/
public static void main(String[] args) throws IOException {
int[] data = readfiles("nani.txt");
System.out.println(Arrays.toString(data));
Jarvis j = new Jarvis();
Jarvis.convexHull(points);
}
public static int[] readfiles(String file)
{
try
{
File f = new File(file);
Scanner scan = new Scanner(f);
List arr;
arr = new ArrayList<>();
int crr = 0;
while(scan.hasNextInt())
{
arr.add(scan.nextInt());
System.out.println(arr.get(crr++));
}
System.out.println("Jarvis Algorithm Test\n");
System.out.println(crr);
scan.useDelimiter(",|\\s*");
/** Make an object of Jarvis class **/
int n = crr;
System.out.println(n);
System.out.println("Reading X,Y Values From File");
for (int i = 0; i < n; i++) {
Point[] points = new Point[n];
points[i] = new Point();
points[i].x = arr.get(i);
points[i].y = arr.get(i+1);
//i=i+1;
System.out.println("(x,y) values are:" + points[i].x + "\t" + points[i].y);
}
scan.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
return null;
}
}
`
The text was updated successfully, but these errors were encountered:
Iam Getting output like this.. here reading is also fault.. 1,2 as first point and 6,4 as my second point.. like this it should be read.. but here fault
run:
1
2
6
4
8
7
2
3
12
13
9
1
Jarvis Algorithm Test
`/*
*/
package jarvis;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
/**
Class point *
*/
class Point {
int x, y;
}
/**
*
@author ADMIN
*/
public class Jarvis {
private static Point[] points;
private static 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);
}
public static 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);
}
public static void display(Point[] points, int[] next) {
System.out.println("\nJarvis March Convex Hull points : ");
for (int i = 0; i < next.length; i++) {
if (next[i] != -1) {
System.out.println("(" + points[i].x + ", " + points[i].y + ")");
}
}
}
/**
@param args the command line arguments
*/
public static void main(String[] args) throws IOException {
int[] data = readfiles("nani.txt");
System.out.println(Arrays.toString(data));
Jarvis j = new Jarvis();
Jarvis.convexHull(points);
}
public static int[] readfiles(String file)
{
try
{
File f = new File(file);
Scanner scan = new Scanner(f);
List arr;
arr = new ArrayList<>();
int crr = 0;
}
}
`
The text was updated successfully, but these errors were encountered: