查找给定半径内的所有整数坐标

查找给定半径内的所有整数坐标

本文介绍了查找给定半径内的所有整数坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个二维坐标系我怎样才能从一个给定点的半径中找到所有具有整数坐标的点?我想要点作为x坐标和y坐标值。

在给定点周围的正方形中找到点很容易,可以这样完成:

  for(int x = -radius + point.x; x  for(int y = -radius + point.y; y  {
points.insert(point(x,y));
}

但是我怎样才能找到围绕给定点的圆圈?该算法与性能相关,但与精度无关。所以,如果一个点接近半径不超过1就没关系。换句话说,我不需要浮点精度。

解决方案

最简单的解决方案:取一个正方形并过滤它:

 指向点(100,100); 
for(int x = -radius; x for(int y = -radius; y if( x * x + y * y points.insert(Point(x + point.x,y + point.y));
}


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-coordinate value.

Finding points in a square around the given point is easy and could be done like that:

for(int x = -radius + point.x; x < radius + point.x; ++x)
for(int y = -radius + point.y; y < radius + point.y; ++y)
{
    points.insert(point(x, y));
}

But how can I find the points in a circle around the given point? This algorithm is performance related but not accuracy related. So it doesn't matter if a point closes to the radius than 1 is added or not. In other words, I do not need floating point accuracy.

解决方案

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));
}

这篇关于查找给定半径内的所有整数坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 20:27