使用哪种算法获取实心圆点?
int start_X = 30; // center point
int start_Y = 30;
int r = 5;
// current point
int x;
int y;
if(?==true)
{
map2D[x][y] = 1; // for filled circle points
}
答案 0 :(得分:11)
你得到一个圆的等式:
其中a& b是中心点坐标。所有x&满足该等式的y个点是圆的一部分。要查看某个点(x1,y1)是否为,请检查是否
((x1 - start_X) * (x1 - start_X) + (y1 - start_Y) * (y1 - start_Y)) <= r * r
&lt; =符号也包括圆圈内的点。您可以安全地限制间隔[start_X - r; startX + r]和[start_Y - r; startY + r]。
答案 1 :(得分:3)
您可以使用中心(r
,r
)搜索2 start_X
2 start_Y
的正方形区域:
std::vector< std::pair<int> > circlePoints;
for(int i = start_X - r; i <= start_X + r; i++)
{
for(int j = start_Y - r; j <= start_Y + r; j++)
{
if((i-r)*(i-r) + (j-r)*(j-r) <= r*r)
{
circlePoints.push_back(std::pair<int>(i,j));
}
}
}
答案 2 :(得分:1)
如果你想在没有检查的情况下直接去圆圈中的所有点,这就是方法。
SatY = CenterY;//StartY + R
for (int i = StartX; i < EndX; i++)
{
int StartY = (int)(SatY - Math.Sqrt(Math.Abs((R + i - StartX) * (R - i + StartX))));
int EndY = (int)(SatY + Math.Sqrt(Math.Abs((R + i - StartX) * (R - i + StartX))));
for (int j = StartY; j < EndY; j++)
{
// Do Job
}
}
答案 3 :(得分:1)
这是Java中的完整解决方案。您可以轻松地将其转换为C ++。从一个预定义为0的空矩阵开始,如果点(x,y)位于一个圆内,则用1填充,否则用9填充外圆。 (为什么9-只是为了让您看到矩阵中清楚地画出了圆圈)。下面的代码适用于奇数大小的矩阵。让我知道是否有人有更好的解决方案。
private static void drawCircle(int[][] emptyMatrix, int diameter) {
int startX = diameter/2;
int startY = diameter/2;
int radius = diameter/2;
drawCircleRecursive(emptyMatrix, diameter, startX, startY, radius, radius);
System.out.println("Filled matrix: ");
for (int i = 0; i < emptyMatrix.length; i++) {
for (int j = 0; j < emptyMatrix[0].length; j++) {
System.out.print(emptyMatrix[i][j] + " ");
}
System.out.println();
}
}
private static void drawCircleRecursive(int[][] emptyMatrix, int d, int startX, int startY, int x, int y) {
if(x >= emptyMatrix.length || y >= emptyMatrix[0].length || x < 0 || y < 0 || emptyMatrix[x][y] == 1)
return;
else if(emptyMatrix[x][y] == 9)
return;
int r = d/2;
if (((x - startX) * (x - startX) + (y - startY) * (y - startY)) <= (r * r))
emptyMatrix[x][y] = 1;
else
emptyMatrix[x][y] = 9;
drawCircleRecursive(emptyMatrix, d, startX, startY, x+1, y); // down
drawCircleRecursive(emptyMatrix, d, startX, startY, x, y+1); // right
drawCircleRecursive(emptyMatrix, d, startX, startY, x-1, y); //up
drawCircleRecursive(emptyMatrix, d, startX, startY, x, y-1); //left
drawCircleRecursive(emptyMatrix, d, startX, startY, x-1, y-1); // diagonal up-left
drawCircleRecursive(emptyMatrix, d, startX, startY, x+1, y+1); // diagonal right-down
drawCircleRecursive(emptyMatrix, d, startX, startY, x+1, y-1); // diagonal left-down
drawCircleRecursive(emptyMatrix, d, startX, startY, x-1, y+1); // diagonal right-up
}