Find all integer coordinates in a given radius

前端 未结 4 917
臣服心动
臣服心动 2020-12-31 18:09

Given a two-dimensional coordinate system how can I find all points with integer coordinates in a radius from a given point? I want the points as x-coordinate and y-coordina

相关标签:
4条回答
  • 2020-12-31 18:43

    Simplest solution: take a square and filter it:

    Point point(100, 100);
    for(int x = -radius; x <= radius; ++x)
    for(int y = -radius; y <= radius; ++y)
    if(x*x + y*y <= radius* radius)   {
        points.insert(Point(x + point.x, y + point.y));
    }
    
    0 讨论(0)
  • 2020-12-31 18:53

    The following code just parses the boundary along a quarter circle to determine the inner area. It does not need to compute the distance for the outer points, nor for the inner points. (edit: but finally, all points of the filled circle are added)

    In some mini-Java-benchmarks, for small radius (<10), it is of the same speed as the simple approach with parsing the full square. For radius 20-40 it is about 2 times faster, and it achieves a speedup of about 4 times for radius > 50. For some much larger radius (>200) the speedup decreases again, since for any approach the dominating time is then needed for creating and adding the >100k points - regardless of how they are determined.

    // add the full length vertical center line once
    for (int y = -radius + point.y; y <= radius + point.y; ++y)
        points.insert(Point(point.x, y));
    
    int sqRadius = radius * radius;
    
    // add the shorter vertical lines to the left and to the right
    int h = radius;
    for (int dx = 1; dx <= radius; ++dx) {
        // decrease h
        while (dx*dx + h*h > sqRadius && h > 0)
            h--;
    
        for (int y = -h + point.y; y <= h + point.y; ++y) {
            points.insert(Point(point.x + dx, y));
            points.insert(Point(point.x - dx, y));
        }
    }
    
    0 讨论(0)
  • 2020-12-31 18:57

    One way is an outer loop on x from -R to +R and an inner loop on y according to the y values of the circle at that x value (from -sqrt(r^2 - x^2) to sqrt(r^2 - x^2) if the center is at 0,0), if the center is at X,Y - simply add X or Y to all loop ranges in the same manner as you did in your example

    0 讨论(0)
  • 2020-12-31 19:00

    You can make a small modification to the midpoint circle algorithm to get a filled circle.

    First generate the coordinates:

    data = new int[radius];
    int f = 1 - radius, ddF_x = 1;
    int ddF_y = -2 * radius;
    int x = 0, y = radius;
    while (x < y)
    {
        if (f >= 0)
        {
            y--;
            ddF_y += 2; f += ddF_y;
        }
        x++;
        ddF_x += 2; f += ddF_x;
        data[radius - y] = x; data[radius - x] = y;
    }
    

    Then visit all the interior points:

    int x0 = center.X;
    int y0 = center.Y - Radius;
    int y1 = center.Y + Radius - 1;
    
    for (int y = 0; y < data.Length; y++)
    {
        for (int x = -data[y]; x < data[y]; x++)
        {
            doSomething(x + x0, y + y0);
            doSomething(x + x0, y1 - y);
        }
    }
    

    That saves some work visiting points that won't be in the circle, but at the expense of a little pre-processing. It definitely won't help for smaller circles, and for bigger ones, well, I honestly don't know. You'd have to benchmark it.

    0 讨论(0)
提交回复
热议问题