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

com.almasb.fxgl.pathfinding.heuristic.OctileDistance Maven / Gradle / Ivy

The newest version!
/*
 * FXGL - JavaFX Game Library. The MIT License (MIT).
 * Copyright (c) AlmasB ([email protected]).
 * See LICENSE for details.
 */

package com.almasb.fxgl.pathfinding.heuristic;

import com.almasb.fxgl.core.collection.grid.Cell;

import static java.lang.Math.*;

/**
 * See https://theory.stanford.edu/~amitp/GameProgramming/Heuristics.html#diagonal-distance
 * for definition.
 *
 * @author Jean-René Lavoie ([email protected])
 */
public final class OctileDistance extends DiagonalHeuristic {

    private final int diagonalFactor;

    public OctileDistance() {
        this(DEFAULT_WEIGHT);
    }

    public OctileDistance(int weight) {
        super(weight, (int)(sqrt(2) * weight));
        diagonalFactor = getDiagonalWeight() - weight;
    }

    @Override
    public int getCost(int startX, int startY, int targetX, int targetY) {
        int dx = abs(startX - targetX);
        int dy = abs(startY - targetY);

        // D * max(dx, dy) + (D2-D) * min(dx, dy), where
        // D - 4-directional weight
        // D2 - diagonal weight
        return getWeight() * max(dx, dy) + diagonalFactor * min(dx, dy);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy