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

main.com.enniovisco.space.Grid.kt Maven / Gradle / Ivy

The newest version!
package com.enniovisco.space

import io.github.moonlightsuite.moonlight.core.space.*
import io.github.moonlightsuite.moonlight.space.*

class Grid(val rows: Int, val columns: Int) {
    /**
     * The spatial model built for the current grid
     */
    val model = generateModel(rows, columns)

    /**
     * The 1-d size of the grid
     * @return [rows] x [columns]
     */
    val size = model.size()

    private fun generateModel(rows: Int, columns: Int) =
        RegularGridModel(rows, columns, 1)

    /**
     * from 1-d location to (X,Y) pairs
     */
    fun toXY(location: Int): Pair {
        val coords = model.toCoordinates(location)
        return Pair(coords[0], coords[1])
    }

    /**
     * from (X,Y) pairs to 1-d location
     */
    fun toNode(xyCoords: Pair): Int {
        return model.fromCoordinates(xyCoords.first, xyCoords.second)
    }

    /**
     * It calculates the proper distance, given a spatial model
     *
     * @return a DoubleDistance object, meaningful in the given Spatial Model
     */
    fun distance(max: Int = size): DistanceStructure {
        val isParallel = true
        val baseline = 0
        return IntManhattanDistanceStructure(isParallel, baseline, max, model)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy