Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reading Data from file and stored in array.. how that array is input to another method ?? #1

Open
murali5003 opened this issue Feb 15, 2016 · 1 comment

Comments

@murali5003
Copy link
Owner

`/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor.
    */
    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);

    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;

}
}

`

@murali5003
Copy link
Owner Author

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

12
12
Reading X,Y Values From File
(x,y) values are:1 2
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 12, Size: 12
(x,y) values are:2 6
(x,y) values are:6 4
(x,y) values are:4 8
(x,y) values are:8 7
(x,y) values are:7 2
(x,y) values are:2 3
(x,y) values are:3 12
(x,y) values are:12 13
(x,y) values are:13 9
(x,y) values are:9 1
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at jarvis.Jarvis.readfiles(Jarvis.java:143)
at jarvis.Jarvis.main(Jarvis.java:97)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant