php iterator_to_array class,PHP中的iterator_to_array函数是什么?

简而言之,iterator_to_array()函数接受Traversable类型的迭代器,并将其转换为关联/非关联数组,具体取决于提供的参数.从

the documentation,

array iterator_to_array ( Traversable $iterator [, bool $use_keys = true ] )

该函数采用以下两个参数,

>第一个参数是Traversal类型,它是一个接口. IteratorAggregate和Iterator类都扩展了这个接口.您可以在自定义类中实现这两个类,如下所示:

class myIterator implements IteratorAggregate {

private $array = array('key1'=>'value1', 'value2', 'value3', 'value4');

public function getIterator(){

return new ArrayIterator($this->array);

}

}

$obj = new myIterator;

$array = iterator_to_array($obj->getIterator(), true);

var_dump($array);

要么,

class myIterator implements Iterator {

private $key;

private $array = array('key1'=>'value1', 'value2', 'value3', 'value4');

public function __construct(){

$this->key = key($this->array);

}

public function rewind(){

reset($this->array);

$this->key = key($this->array);

}

public function current(){

return $this->array[$this->key];

}

public function key(){

return $this->key;

}

public function next(){

next($this->array);

$this->key = key($this->array);

}

public function valid(){

return isset($this->array[$this->key]);

}

}

$obj = new myIterator;

$array = iterator_to_array($obj, true);

var_dump($array);

这里要注意的最重要的一点是,传递给iterator_to_array()函数的参数1必须实现Traversable接口,因此不能直接将任何其他类型的数组或对象传递给此函数.请参阅以下示例,

$array = array('key1'=>'value1', 'value2', 'value3', 'value4');

$array = iterator_to_array($array, true); // wrong

>第二个参数是一个布尔值,用于指示是否使用迭代器元素键作为索引.见Example #1 here.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
class SpiralIterator: def init(self, source, x=810, y=500, length=None): self.source = source self.row = np.shape(self.source)[0]#第一个元素是行数 self.col = np.shape(self.source)[1]#第二个元素是列数 if length: self.length = min(length, np.size(self.source)) else: self.length = np.size(self.source) if x: self.x = x else: self.x = self.row // 2 if y: self.y = y else: self.y = self.col // 2 self.i = self.x self.j = self.y self.iteSize = 0 geo_transform = dsm_data.GetGeoTransform() self.x_origin = geo_transform[0] self.y_origin = geo_transform[3] self.pixel_width = geo_transform[1] self.pixel_height = geo_transform[5] def hasNext(self): return self.iteSize < self.length # 不能取更多值了 def get(self): if self.hasNext(): # 还能再取一个值 # 先记录当前坐标的值 —— 准备返回 i = self.i j = self.j val = self.source[i][j] # 计算下一个值的坐标 relI = self.i - self.x # 相对坐标 relJ = self.j - self.y # 相对坐标 if relJ > 0 and abs(relI) < relJ: self.i -= 1 # 上 elif relI < 0 and relJ > relI: self.j -= 1 # 左 elif relJ < 0 and abs(relJ) > relI: self.i += 1 # 下 elif relI >= 0 and relI >= relJ: self.j += 1 # 右 #判断索引是否在矩阵内 x = self.x_origin + (j + 0.5) * self.pixel_width y = self.y_origin + (i + 0.5) * self.pixel_height z = val self.iteSize += 1 return x, y, z dsm_path = 'C:\sanwei\jianmo\Productions\Production_2\Production_2_DSM_part_2_2.tif' dsm_data = gdal.Open(dsm_path) dsm_array = dsm_data.ReadAsArray() spiral_iterator = SpiralIterator(dsm_array,x=810,y=500) while spiral_iterator.hasNext(): x, y, z = spiral_iterator.get() print(f'Value at ({x},{y}):{z}')这段代码如何添加共线方程
05-24
共线方程是用于判断三个点是否共线的方程,可以使用向量叉积的方式表示。假设我们要判断三个点 $A(x_1,y_1,z_1)$,$B(x_2,y_2,z_2)$,$C(x_3,y_3,z_3)$ 是否共线,可以使用以下公式: $$(\vec{AB} \times \vec{AC}) \cdot \vec{n} = 0$$ 其,$\vec{AB}$ 和 $\vec{AC}$ 分别表示从 $A$ 到 $B$,从 $A$ 到 $C$ 的向量,$\vec{n}$ 表示法向量,可以任意选择一组垂直于 $\vec{AB}$ 的向量。 在代码,我们可以将获取的三个点坐标分别赋值给 $A(x_1,y_1,z_1)$,$B(x_2,y_2,z_2)$,$C(x_3,y_3,z_3)$,然后使用 numpy 库的 cross() 函数计算向量叉积,再将结果与 $\vec{n}$ 做点积,判断是否为零即可。 以下是修改后的代码: ```python class SpiralIterator: def __init__(self, source, x=810, y=500, length=None): self.source = source self.row = np.shape(self.source)[0]#第一个元素是行数 self.col = np.shape(self.source)[1]#第二个元素是列数 if length: self.length = min(length, np.size(self.source)) else: self.length = np.size(self.source) if x: self.x = x else: self.x = self.row // 2 if y: self.y = y else: self.y = self.col // 2 self.i = self.x self.j = self.y self.iteSize = 0 geo_transform = dsm_data.GetGeoTransform() self.x_origin = geo_transform[0] self.y_origin = geo_transform[3] self.pixel_width = geo_transform[1] self.pixel_height = geo_transform[5] def hasNext(self): return self.iteSize < self.length # 不能取更多值了 def get(self): if self.hasNext(): # 还能再取一个值 # 先记录当前坐标的值 —— 准备返回 i = self.i j = self.j val = self.source[i][j] # 计算下一个值的坐标 relI = self.i - self.x # 相对坐标 relJ = self.j - self.y # 相对坐标 if relJ > 0 and abs(relI) < relJ: self.i -= 1 # 上 elif relI < 0 and relJ > relI: self.j -= 1 # 左 elif relJ < 0 and abs(relJ) > relI: self.i += 1 # 下 elif relI >= 0 and relI >= relJ: self.j += 1 # 右 # 判断索引是否在矩阵内 x = self.x_origin + (j + 0.5) * self.pixel_width y = self.y_origin + (i + 0.5) * self.pixel_height z = val self.iteSize += 1 # 判断前三个点是否共线 if self.iteSize == 3: A = np.array([self.x0, self.y0, self.z0]) B = np.array([self.x1, self.y1, self.z1]) C = np.array([x, y, z]) AB = B - A AC = C - A n = np.array([AB[1]*AC[2]-AB[2]*AC[1], AB[2]*AC[0]-AB[0]*AC[2], AB[0]*AC[1]-AB[1]*AC[0]]) if np.dot(n, n) == 0: print("Three points are collinear!") # 更新前两个点的坐标和值 self.x0 = self.x1 self.y0 = self.y1 self.z0 = self.z1 self.x1 = x self.y1 = y self.z1 = z return x, y, z dsm_path = 'C:\sanwei\jianmo\Productions\Production_2\Production_2_DSM_part_2_2.tif' dsm_data = gdal.Open(dsm_path) dsm_array = dsm_data.ReadAsArray() spiral_iterator = SpiralIterator(dsm_array,x=810,y=500) while spiral_iterator.hasNext(): x, y, z = spiral_iterator.get() print(f'Value at ({x},{y}):{z}') ``` 在 get() 函数加入了判断前三个点是否共线的代码。其,self.x0、self.y0、self.z0 表示第一个点的坐标和值,self.x1、self.y1、self.z1 表示第二个点的坐标和值,x、y、z 表示第三个点的坐标和值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值