Home > Back-end >  Calculate the number of points with integer coordinates inside the ellipse
Calculate the number of points with integer coordinates inside the ellipse

Time:05-15

Calculate the number of points with integer coordinates inside the ellipse1. Hint: check those values for x and y for which

1 < 1.

Can

2 and this be fulfilled? And what about y?

CodePudding user response:

There is no point inside the ellipse whose |x| is greater than 13.

If you want to count the number of points with integer coordinates inside the ellipse I would do something like this:

int Points = 0;
for(int x = -13; x <= 13; x  )
{
    for(int y = -16; y <= 16; y  )
    {
        if((Math.Pow(x, 2)/169)   (Math.Pow(y, 2)/256) <= 1)
        {
            Points  ;
        }
}

Clarify the question if you want a more detailed answer because it is hard to understand what you are asking.

CodePudding user response:

You can query all the points within

 x = {-13 .. 13} 
 y = {-16 .. 16}

square (follow the hint provided: you should analyze points such that |x| < 13 and |y| < 16). Let's do it with a help of Linq:

  int a = 13;
  int b = 16;

  int result = Enumerable
    .Range(-a, a * 2   1)
    .SelectMany(x => Enumerable
       .Range(-b, 2 * b   1)
       .Select(y => (x: x, y: y)))
    .Count(p => (double) p.x * p.x / a / a   (double) p.y * p.y / b / b < 1); 

  Console.Write(result);

Outcome:

  647

If you want to include points which on the ellipse (e.g. {13, 0}), just change < 1 into <= 1. In this case you'll have 651 points (points {-13, 0}, {13, 0}, {0, -16}, {0, 16} are added).

  • Related