Page 1 of 4

10823 - Of Circles and Squares

Posted: Sun Mar 06, 2005 11:40 am

precision error..

Posted: Sun Mar 06, 2005 2:44 pm
watch out for precision error...

11 / 2 = 5.49999999999999 and not 5.50000000000000
So add necessary eps to handle this case.

Posted: Sun Mar 06, 2005 3:09 pm
Or don't use floating point numbers at all. When computing a/b (a,b positive integers), you can use:

a/b - if rounding down
(a+b-1)/b - if rounding up
(2*a+b)/(2*b) - if rounding to the nearest integer (with 0.5 up)

Where / is the integer division.

Thank you very much!

Posted: Fri Mar 11, 2005 8:04 am
I got it accepted. You were right.

Posted: Fri Mar 11, 2005 2:35 pm
One small doubt:

Suppose a point lies on the boundary of a square which is overlapped by some other circle/square.What should the output color be?I assumed it would be black, but is it that we find the color using the same avg formula described in the problem?

Regards,
Suman.

Posted: Fri Mar 11, 2005 2:47 pm
...The problem...

The color of a point is computed as the average red, average green and average blue values of the geometric objects that this point falls into. If the point is on the borderline of any of the geometric object, then its color would be black; if it falls in the empty space, the color of the point would be white.

So you do not compute it using the average formula..., regards,

Yandry.

Re: precision error..

Posted: Sat Mar 12, 2005 8:56 am
sohel wrote:watch out for precision error...

11 / 2 = 5.49999999999999 and not 5.50000000000000
So add necessary eps to handle this case.
I think this can be solved by:

Code: Select all

double ar = R / inCount + 0.5;
int iar = (int)ar;
When I participate the contest, this problem really confuses me....>"<

Posted: Sat Mar 12, 2005 11:27 am
The heart and soul of my program

Code: Select all

int onCircle(point p, point c, int len )
{
long r2 = len*len;
long d = (p.x-c.x)*(p.x-c.x) + (p.y-c.y)*(p.x-c.y);
if ( d == r2 )
return 0;
else if ( d < r2 )
return 1;
else return -1;
}
int onSquare(point p, point c, int len )
{
if ( p.x == c.x )
{
if ( (p.y >= c.y) && (p.y <= c.y + len) )
return 1;
}
if ( p.y == c.y )
{
if ( (p.x >= c.x) && (p.x <= c.x + len) )
return 1;
}
return 0;
}

int inSquare(point p, point c, int len)
{
return ( (p.x > c.x) && (p.x < c.x + len) &&
(p.y > c.y) && (p.y < c.y + len) ) ? 1 : 0;
}
Can I get some i/o please ? I am tired with this.

Regards,
Suman.

Posted: Sat Mar 12, 2005 1:29 pm
Souldn't your function onSquare( (4,4) , (1,1), 3) return 1?

Posted: Sat Mar 12, 2005 2:12 pm
Right on! You caught me napping.Thanks Little Joey.

Code: Select all

int onSquare(point p, point c, int len )
{
if ( p.x == c.x )
{
if ( (p.y >= c.y) && (p.y <= c.y + len) )
return 1;
}
if ( p.x == c.x + len )
{
if ( (p.y >= c.y) && (p.y <= c.y + len) )
return 1;
}
if ( p.y == c.y || (p.y == c.y+len) )
{
if ( (p.x >= c.x) && (p.x <= c.x + len) )
return 1;
}
if ( p.y == c.y + len )
{
if ( (p.x >= c.x) && (p.x <= c.x + len) )
return 1;
}
return 0;
}
Trying to get it right the simplest way possible.Any more guesses ?
Regards,
Suman.[/code]

Posted: Sat Mar 12, 2005 2:19 pm
Yeah, your function OnSquare is wrong, I am sure there is the mistake, try it checking the 4 sides of the square..., I mean:

bool side1, side2, side3, side4;

side1=...;
side2=...;
side3=...;
side4=...;

return side1 || side2 || side3 || side4;

sometimes partitioning makes things easier... , good luck,

Yandry.

Btw I was not accepted this ex within the 5 hours of the contest

Posted: Sat Mar 12, 2005 2:22 pm
Even the function I posted now is wrong?
Did you check it?
Suman.

Posted: Sat Mar 12, 2005 3:19 pm

Code: Select all

int tria(point a, point b, point c)
{
return (a.x*(b.y-c.y) + b.x*(c.y-a.y) + c.x*(a.y-b.y));
}

int onSquare(point p, point c, int len )
{
point d, e, f;
d.x = c.x; d.y = c.y + len;
f.x = c.x + len; f.y = c.y;
e.x = c.x + len; e.y = c.y + len;
return ( !tria(c, p, d) ||
!tria(d, p, e) ||
!tria(e, p, f) ||
!tria(f, p, c) );
}
suman

Posted: Sat Mar 12, 2005 5:56 pm
I think this will be fine:

int onSquare(point p, point c, int len )
{
if (c.x == p.x || c.x + len == p.x)
if (p.y >= c.y && p.y <= c.y + len)
return 1;

if (c.y == p.y || c.y + len == p.y)
if (p.x >= c.x && p.x <= c.x + len)
return 1;

return 0;
}

hope it helps...

Posted: Sat Mar 12, 2005 8:02 pm
why make it so complex... keep it simple...

Code: Select all

int onSquare(point p, point c, int len)
{
if(inSquare(p,c,len)) return 0;
return ( (p.x >= c.x) && (p.x <= c.x + len) &&
(p.y >= c.y) && (p.y <= c.y + len) ) ? 1 : 0;
}