ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1215. Exactness of Projectile Hit

Could anyone help...I can't find what's wrong..may be I'm a very BAD programmer
Posted by Vladimir Milenov Vasilev 19 May 2003 14:35
Here is my C++ source...



#include <stdio.h>
#include <math.h>

struct SPoint
{
    double x,y;
}a[101];

#define sqr(a) ((a)*(a))
#define min(a,b) ((a>b) ? b : a)

//Distance between point x and point y
double l(SPoint x, SPoint y)
{
    return sqrt(sqr(x.x-y.x)+sqr(x.y-y.y));
}

//Squaer of the distance between point x and point y
double l2(SPoint x, SPoint y)
{
    return sqr(x.x-y.x)+sqr(x.y-y.y);
}

//cos(<xyz)
double cos(SPoint x, SPoint y, SPoint z)
{
    return (l2(x,y)+l2(z,y)-l2(x,z))/(2*l(x,y)*l(z,y));
}

//face of triangle with points x,y,z
double face(SPoint x, SPoint y, SPoint z)
{
    return fabs((x.x - y.x)*(y.y - z.y)+(z.x - y.x)*(x.y -
y.y))/2;
}


double h(SPoint x, SPoint y, SPoint z)
{
    return (2*face(x,y,z))/l(x,z);
}


int n, i;
double rez, face_all, face_point;

void main()
{
    scanf("%lf%lf%d",&a[0].x, &a[0].y, &n);
    scanf("%lf%lf",&a[1].x, &a[1].y);
    rez = l(a[0],a[1]);
    face_all = 0.00000;
    face_point = 0.00000;
    for(i = 2; i <= n; i++)
    {
        scanf("%lf%lf",&a[i].x, &a[i].y);
        if(i > 2) face_all += face(a[1], a[i-1], a[i]);
        face_point += face(a[0],a[i-1],a[i]);
        if(cos(a[i-1],a[0],a[i]) > 0.0000000 && cos(a[i],a
[0],a[i-1]) > 0.0000000)
        {
            rez = min(rez, l(a[0], a[i-1]));
            rez = min(rez, l(a[0], a[i]));
        }
        else rez = min(rez, h(a[i-1],a[0],a[i]));
    }
    if(cos(a[1],a[0],a[n]) > 0.000000 && cos(a[n],a[0],a[1]) >
0.0000000)
    {
        rez = min(rez, l(a[0], a[1]));
        rez = min(rez, l(a[0], a[n]));
    }
    else rez = min(rez, h(a[1],a[0],a[n]));
    face_point += face(a[0],a[n],a[1]);
    if(fabs(face_all-face_point)<0.000000001) rez = 0.0000;
    printf("%.3lf\n",2*rez);
}