package me.hammerle.supersnuvi.util;

import javafx.scene.image.Image;
import javafx.scene.image.PixelWriter;
import javafx.scene.image.WritableImage;
import javafx.scene.paint.Color;
import me.hammerle.supersnuvi.rendering.GameRenderer;

public class Utils 
{
    public static double round(double d)
    {
        return Math.round(d * 10000) / 10000d;
    }
    
    public static double interpolateY(double x1, double y1, double x2, double y2, double x3)
    {
        double k = (y2 - y1) / (x2 - x1);
        return k * x3 + y1 - k * x1;
    }
    
    public static double interpolateX(double x1, double y1, double x2, double y2, double y3)
    {
        double k = (x2 - x1) / (y2 - y1);
        return (y3 - y1) * k + x1;
    }
    
    public static Image getColoredImage(Color c, int width, int height)
    {
        WritableImage image = new WritableImage(width, height);
        PixelWriter writer = ((WritableImage) image).getPixelWriter();
        for(int fx = 0; fx < width; fx++)
        {
            for(int fy = 0; fy < height; fy++)
            {
                writer.setColor(fx, fy, c);
            }
        }
        return image;
    }
    
    public static Image getImage(String path)
    {
        return new Image("me/hammerle/supersnuvi/resources/" + path + ".png");
    }
    
    public static Image getTileImage(String path)
    {
        return new Image("me/hammerle/supersnuvi/resources/" + path + ".png", GameRenderer.TILE_SIZE, GameRenderer.TILE_SIZE, true, false);
    }
}
