All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.ru.casperix.math.iteration.Box3Iterator.kt Maven / Gradle / Ivy

There is a newer version: 1.9.0
Show newest version
package ru.casperix.math.iteration

import ru.casperix.math.axis_aligned.int32.Box3i
import ru.casperix.math.vector.int32.Vector3i

class Box3Iterator(val source: Box3i, val step: Int = 1) : Iterator {
	private var x: Int
	private var y: Int
	private var z: Int
	private var next: Vector3i?

	init {
		x = source.min.x
		y = source.min.y
		z = source.min.z
		next = Vector3i(x, y, z)
	}

	/**
	 * Returns the next element in the iteration.
	 */
	override operator fun next(): Vector3i {
		val result = next!!

		x += step
		if (x > source.max.x) {
			x = source.min.x
			y += step
			if (y > source.max.y) {
				y = source.min.y
				z += step
				if (z > source.max.z) {
					next = null
					return result
				}
			}
		}
		next = Vector3i(x, y, z)
		return result
	}


	/**
	 * Returns `true` if the iteration has more elements.
	 */
	override operator fun hasNext(): Boolean {
		return next != null
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy