Thursday, May 2, 2019

Polygon

  • Problem Description

    A convex polygon is a simple polygon whose interior is a convex set. The following properties of a simple polygon are all equivalent to convexity:
    - Every internal angle is less than 180 degrees.
    - Every line segment between two vertices remains inside or on the boundary of the polygon.
    Given the vertices of a polygon( x,y) ,your job is to find the area of polygon.

    Input
    Input will begin with an integer T, the number of test cases. Each test case start with integer N, the number of coordinates in the polygon followed by x1,x2,x3.............xN,y1,y2,y3......yN in each line.1<=T<=50,1<=N<=20 and -10,000 <=x or y<=10,000

    Output
    For every test case, output a single line containing the area of polygon.
  • Test Case 1

    Input (stdin)
    2

    3

    0 0 1 1 2 1

    3

    1 1 -1 1 -1 0

    Expected Output
    0.5

    2.0
  • Test Case 2

    Input (stdin)
    2

    3

    1 2 1 1 2 1

    3

    1 2 -1 -1 -1 0

    Expected Output
    0.0

    0.5
Solution

#include<stdio.h>
#include<math.h>
int main()
{
int test,x[22],y[22],n,i,j;
long long int sum;
double area;
scanf("%d",&test);
while(test--)
{
sum=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&x[i]);
}
for(i=0;i<n;i++)
{
scanf("%d",&y[i]);
}
for(i=0;i<n;i++)
{
if(i==(n-1))
{
j=0;
}
else
j=i+1;
sum=sum+((x[i]*y[j])-(y[i]*x[j]));
}
area=sum/2.0;
if(area<0)
area=-area;
printf("%0.1lf\n",area);
}
return 0;
}

No comments:

Post a Comment

Parity

Problem Description Ram and Sita playing the parity game. Two types of parity are there. One is odd parity and next is even parity. Ram will...