Finding all the points within a circle in 2D space

前端 未结 7 1795
自闭症患者
自闭症患者 2021-02-13 12:58

I am representing my 2D space (consider a window), where each pixel is shown as a cell in a 2D array. i.e. a 100x100 window is represented by the array of same dimensions.

相关标签:
7条回答
  • 2021-02-13 14:03

    You can bypass the need for a conditional check:

    for(x=center-radius; x<center+radius; x++) {
        yspan = radius*sin(acos((center-x)/radius));
        for(y=center-yspan; y<center+yspan; y++) {
            // (x,y) is inside the circle
        }
    }
    

    If needed, you can round(yspan).

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