Showing posts with label CONVEX HULL. Show all posts
Showing posts with label CONVEX HULL. Show all posts

Wednesday, June 10, 2015

CONVEX HULL

AIM

To write a Java program for the implementation of convex hull

ALGORITHM

Step1:  Start the program

Step2:  Create a class convexhullalg

Step3:  Read the number of points

Step4:  Get the x and y co-ordinate values

Step5:  Sort the values using sort function
           
Step6:  To sort two values swap the values of i and j
                                   
Step7:  Call the function display to display the boundary points

Step8:  The function check id used to check whether the point is angular or not(180▫)

Step9:  End of the program





PROGRAM
CONVEX HULL
import java.io.*;
class convexhullalg
{
        int x[],y[],n;
        boolean status[];
        void insert()
        {
              try
              {
                DataInputStream in=new DataInputStream(System.in);
                System.out.println("Enter number of points:");
                n=Integer.parseInt(in.readLine());
                x=new int[n];
                y=new int[n];
                status=new boolean[n];
                System.out.println("Enter x and y coordinates for ");
                for(int i=0;i<n;i++)
                {
                System.out.println("point "+(i+1));
                x[i]=Integer.parseInt(in.readLine());
                y[i]=Integer.parseInt(in.readLine());
                status[i]=false;
                }
               }
             catch(Exception e){}
                sort();
                check(0,'L');
                check(0,'H');
                display();
        }
        void sort()
        {
                for(int i=0;i<n-1;i++)
                {
                  for(int j=i+1;j<n;j++)
                    if((x[i]>x[j]) || ((x[i]==x[j]) && (y[i]>y[j])))
                        swap(i, j);
                }
        }
        void swap(int i,int j)
        {
                int temp=x[i];
                x[i]=x[j];
                x[j]=temp;
                temp=y[i];
                y[i]=y[j];
                y[j]=temp;
        }
        void display()
        {
                System.out.println("Boundary points are");
                for(int i=0;i<n;i++)
                   if(status[i]==true)
                        System.out.println("("+x[i]+", "+y[i]+")");

        }
        void check(int p,char c)
        {
                double slope=0,degree=0,deg=0;
                int next=0;
                status[p]=true;
                for(int i=p+1;i<n;i++)
                {
                 try
                 {
                   slope=(double)(x[i]-x[p])/(double)(y[i]-y[p]);
                   degree=Math.toDegrees(Math.atan(slope));
                   if(degree < 0)
                      degree+=180;
                 }

Sunday, February 23, 2014

CONVEX HULL

PROGRAM:


import java.io.*;
import java.lang.*;

class convexhull
{
            int a[][];
            public int n;
            boolean b[];
            double ndeg=0;
            int next=0;

            void nextfn(double deg,int i)
            {
                        ndeg=deg;
                        next=i;            
            }
           
            void insertpts() throws Exception
            {
                        DataInputStream in=new DataInputStream(System.in);                  

                        System.out.println("enter the size:");
                        n=Integer.parseInt(in.readLine());

                        a=new int[2][n];
                        b=new boolean[n];
                       
                        for(int i=0;i<n;i++)
                        {
                        System.out.println("enter the "+(i+1)+" x:");
                        a[0][i]=Integer.parseInt(in.readLine());
                        System.out.println("enter the "+(i+1)+" y:");
                        a[1][i]=Integer.parseInt(in.readLine());
                       
                        b[i]=false;
                       
                        for(int j=0;j<i;j++)
                                    {
                                    if(a[0][i]<a[0][j])
                                                swap(j,i);
                                    if(a[0][i]==a[0][j] && a[1][i]<a[1][j])
                                                swap(j,i);
                                    }
                        }
                        display(false);
            }
            void swap(int i,int j)
            {
                        int temp=a[0][i];
                        a[0][i]=a[0][j];
                        a[0][j]=temp;
                        temp=a[1][i];
                        a[1][i]=a[1][j];
                        a[1][j]=temp;
            }
            public void display(boolean k)
            {
                        if(k==true)
                                    System.out.println("boundry pt are");
                        for(int i=0;i<n;i++)
                        {
                        if(k==true && b[i]==false)
                                    continue;                    
                        System.out.println("pt:("+a[0][i]+","+a[1][i]+")");
                        }
            }

            public void traverse(int x,char c)
            {
                        b[x]=true;
                        double slope=0,deg=0;
                        int i=0;
                        next=0;
                        ndeg=0;
                        for(i=x+1;i<n;i++)
                        {                                 
                                    try{
                                    slope=(double)(a[0][i]-a[0][x]);
                                    slope/=(double)(a[1][i]-a[1][x]);
                                   
                                    deg=Math.toDegrees(Math.atan(slope));
                                    if(deg < 0)
                                                deg=180+deg;
                                    }
                                    catch(Exception ex)
                                    {                                             
                                                deg=90;
                                    }                                                         
                                    if(i==x+1)
                                    {
                                                nextfn(deg,i);
                                    }
                                    else
                                    {
                                                if(c=='L')
                                                {
                                                if(ndeg>deg)
                                                            nextfn(deg,i);
                                                }
                                                else{
                                                if(ndeg<deg)
                                                            nextfn(deg,i);
                                                }
                                                if(deg==ndeg)
                                                {
                                                if(deg==90 && a[1][i]<a[1][i])
                                                            nextfn(deg,i);
                                                else if(a[0][i]<a[0][next])
                                                            nextfn(deg,i);
                                                }
                                    }                     
                        }
                        if(next!=0)
                        {                                             
                                    traverse(next,c);
                                   
                        }
                       
            }

            public static void main(String arg[]) throws Exception
            {
                        convexhull c=new convexhull();
                        c.insertpts();
                        if (c.n>0)
                                    {
                                    c.traverse(0,'L');
                                    c.traverse(0,'H');
                                    }
                        c.display(true);
            }

}