如何通过值的二维数组圆你循环? [英] How do you loop through a circle of values in a 2d array?

查看:181
本文介绍了如何通过值的二维数组圆你循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过二维数组的方形截面循环很容易,但如何通过圆形截面你循环?

Looping through a square section of a 2d array is easy but how do you loop through a circular section?

推荐答案

就是我这样做了就是做一个双for循环,就像您通过二维数组循环正常。在这个循环中,但是,做一次检查,看看是否有问题的数组元素是半径为r的圆内使用距离公式。

The way I've done this is to do a double for-loop like you would for looping through the 2d array normally. Inside this loop, however, do a check to see if the array element in question is within a circle of radius r using the distance formula.

例如,给定的10×10阵列,并以选择的中心的阵列(X,Y):

For example, given a 10x10 array, and a chosen "center" of the array at (x,y):

for i from 0 to 9 {
    for j from 0 to 9 {
        a = i - x
        b = j - y
        if a*a + b*b <= r*r {
            // Do something here
        }
    }
}

(code只是伪code,没有任何特定语言)。

(Code is just pseudocode, not any particular language).

这篇关于如何通过值的二维数组圆你循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆