package ru.dbotthepony.kstarbound.world

import it.unimi.dsi.fastutil.objects.ObjectArrayList
import ru.dbotthepony.kstarbound.math.vector.Vector2i
import kotlin.math.pow
import kotlin.math.roundToInt

fun tileAreaBrush(pos: Vector2i, radius: Double, square: Boolean = false): List<Vector2i> {
	if (radius <= 0.0)
		return emptyList()

	val result = ObjectArrayList<Vector2i>()
	val squareRadius = (if (square) Double.POSITIVE_INFINITY else radius).pow(2.0)
	val intRadius = radius.roundToInt()

	for (x in -intRadius .. intRadius) {
		for (y in -intRadius .. intRadius) {
			val length = x * x + y * y

			if (length <= squareRadius) {
				result.add(Vector2i(x + pos.x, y + pos.y))
			}
		}
	}

	result.sortWith { o1, o2 -> o1.distanceSquared(pos).compareTo(o2.distanceSquared(pos)) }
	return result
}
