Sunday, 22 November 2015

Update to lego thing as well

Link to MIT algorithms course, bought a book then omg this is hard.

Listening to podcasts https://codecombat.com/ came up, actually looks fun but bad memories of javascript under pressure.

I had another go at the lego picture maker, does seem to have a tendency to make the colours purple for some reason. Also IntStream just makes some of this stuff so easy.

Once it is in a better state I would like to get it on github. I get a bit hung up about poor code, I come back to it weeks later with wtf was I thinking. Need to add a front end, accept different input and output file types, and those actual lego nubs. 

Must get away from magic numbers. 

public static int[] getLegoColourEquiv(int[] rgb) {
    int[] returnColourRGB = {1,2,3};    
    if(rgb == null) {
        return
returnColourRGB;
    }
    int rgbAverage = IntStream.of(rgb).sum() / rgb.length;
    int gapFromColour = Integer.MAX_VALUE;
    int colourAverage;
    int rgbDiff;

    for (int[] colour : colours) {
        colourAverage = IntStream.of(colour).sum() / colour.length;
        rgbDiff = rgbAverage - colourAverage ;
        if(rgbDiff < 0) { rgbDiff *= -1;}
        if(rgbDiff < gapFromColour) {
            gapFromColour = rgbDiff;
            returnColourRGB = Arrays.copyOf(colour, colour.length);
        }
    }
    return returnColourRGB;
}

No comments:

Post a Comment