Friday, April 18, 2014

Test a point is inside a polygon[Odd-even & Winding no. method] in Turbo C++

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<stdio.h>
int seek(int , int []);

void main()
{
int gd=DETECT,gm=0; //Detect Display Device
initgraph(&gd,&gm," ");
int n;
cout<<"Enter the total number of sides 
(max sides 10) \n ";
cin>>n; //Input Edges
int m=n;
int count=1;
n=2*n+2;
int p[20];

for(int i=0;count<=m;i=i+2)
{
cout<<"Enter the x co-ordinate of
"<<count<<" point \n";
cin>>p[i];
cout<<"Enter the y co-ordinate of
"<<count<<" point \n";
cin>>p[i+1];
count++;
}

      p[n-2]=p[0];
      p[n-1]=p[1];


cleardevice(); //ClearDisplay

drawpoly(m+1,p); //DrawPolygon


cout<<"Enter the point to be searched! \n";
int get=seek(m,p);
if(get==1)
cout<<"The point is within the polygon \n";
if(get==0)
cout<<"The point is outside the polygon \n";

getch();
closegraph();
}
int seek(int m,int p[])
{
int x;
int y;

cin>>x>>y;
putpixel(x,y,RED); //PutPixel
setcolor(RED);
circle(x,y,5);
int color;
int cnt=0;
int xs=x;
int ys=y;
int flag=0;
for(int i=0;i<xs;i++)
{
color=getpixel(i,ys); //GetPixel

if(color==WHITE)
{

cnt++;


for(int j=1;j<m;j++)
{
if(i==p[2*j] && ys==p[2*j+1] && flag!=1 )
{
if((p[2*j+3]<p[2*j+1] && p[2*j-1]<p[2*j+1])
|| (p[2*j+3]>p[2*j+1]&& p[2*j-1]>p[2*j+1]))
{
cnt=cnt+1;
flag=1;
}
}
}
}
    flag=0;
}

if(cnt%2==0 )
return 0;
else
return 1;
}


No comments:

Post a Comment

Perform a suitable assignment using Xen Hypervisor or equivalent open source to configure it. Give necessary GUI.

 To install kvm on Fedora:  yum install kvm  yum install virt-manager libvirt libvirt-python python-virtinst  su -c "yum install @v...