bhaskar talla
bhaskar talla

Reputation: 1

polygon "contains" function in java not working

when i'm trying to check whether the particular point is inside the polygon or not using polygon's "contains" function it is giving a wrong output.

why the "contains" function is returning false values even if the point(16,14) is on the polygon....

  import java.awt.Polygon;
  import java.util.Scanner;

    public class ACircleandaSquare
    {
        Scanner sc=new Scanner(System.in);
        int xc,yc,r,w,h,x1,y1,x3,y3,x2,y2,x4,y4,m1,m2,vb1,vb2,vd1,vd2,cm1,cm2;

        void getData()
        {
            w=sc.nextInt();
            h=sc.nextInt();
            xc=sc.nextInt();
            yc=sc.nextInt();
            r=sc.nextInt();
            x1=sc.nextInt();
            y1=sc.nextInt();
            x3=sc.nextInt();
            y3=sc.nextInt();     

            m1=((x3+x1)/2);
            m2=((y3+y1)/2);
            cm1=x3-m1;
            cm2=y3-m2;
            vb1=cm2;
            vb2=-cm1;
            vd1=-cm2;
            vd2=cm1;
            x2=(m1+vb1);
            y2=(m2+vb2);
            x4=(m1+vd1);
            y4=(m2+vd2);       
        }

        void perform()
        {
            int x[]={x1,x2,x3,x4};
            int y[]={y1,y2,y3,y4};
            Polygon P=new Polygon(x,y,4);

            if(P.contains(16, 14))
            {
                System.out.println("yes");
            }
            else
            {
                System.out.println("no");

            }
            for(int i=0;i<h;i++)
            {
                for(int j=0;j<w;j++)
                {
                    if(isInside(j,i) || P.contains(j, i))
                    {
                        System.out.print("#");
                    }
                    else
                    {
                        System.out.print(".");
                    }                
                }
                System.out.println();
            }
        }



        boolean isInside(int i,int j)
        {
            int z=((i-xc)*(i-xc))+(j-yc)*(j-yc);
            if(z<=(r*r))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public static void main(String[] z)
        {
            ACircleandaSquare A1=new ACircleandaSquare();
            A1.getData();
            A1.perform();       
        }
    }

why the answer is printing as "no" instead of yes...?

Upvotes: 0

Views: 1784

Answers (1)

AxelH
AxelH

Reputation: 14572

From the documentation of java.awt.Polygon#contains(int, int)

Determines whether the specified coordinates are inside this Polygon.

Since you are on the border, this is not inside the Polygon.

EDIT :

This is the Shape insideness explanation :

definition of insideness :

A point is considered to lie inside a Shape if and only if:

  • it lies completely inside theShape boundary or
  • it lies exactly on the Shape boundary and the space immediately adjacent to the point in the increasing X direction is entirely inside the boundary or
  • it lies exactly on a horizontal boundary segment and the space immediately adjacent to the point in the increasing Y direction is inside the boundary.

The explanation is that

  • it lies exactly on the Shape boundary : true
  • the space immediately adjacent to the point in the increasing X direction is entirely inside the boundary : false

Your values :

int[] x = {16,12,8,12}

The next x value is 12, so decreasing. With 8, the value start to increase so this will match

Upvotes: 4

Related Questions