extends Object

class_name UTIL

# chunks array into 'chunk_size' pieces
static func chunk_array(arr: Array, chunk_size: int) -> Array:
	var result_chunks = []
	var i = 0
	while i < arr.size():
		var chunk = arr.slice(i, i + chunk_size)
		result_chunks.append(chunk)
		i += chunk_size
	return result_chunks

static func vector2_to_vector3(v: Vector2) -> Vector3:
	return Vector3(v.x, v.y, 0)

static func get_bounding_rect(vectors: PackedVector2Array) -> Rect2:
	if vectors.is_empty():
		return Rect2()
		
	var rect := Rect2(vectors[0], Vector2.ZERO) # Initialize with the first point
	for point in vectors:
		rect = rect.expand(point)
		
	# The 'position' of the Rect2 will be the minimum corner
	return rect

static func get_polygon_area(vectors: PackedVector2Array) -> float:
	var result := 0.0
	var num_vertices := vectors.size()
	
	for q in range(num_vertices):
		var p = (q - 1 + num_vertices) % num_vertices
		result += vectors[q].cross(vectors[p])
	
	return abs(result * 0.5)
