Skip to content

Instantly share code, notes, and snippets.

@LOZORD
Last active August 29, 2015 14:23
Show Gist options
  • Save LOZORD/8ebb2fba992d0ab82aad to your computer and use it in GitHub Desktop.
Save LOZORD/8ebb2fba992d0ab82aad to your computer and use it in GitHub Desktop.
The Grid
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import java.lang.Math;
import java.lang.Number;
public class Grid
{
public static final boolean DEBUG = !false;
public static final int DIMENSION = 1024;
public static final int BOTTOM = DIMENSION - 1;
public static void main (String [] args)
{
System.out.println("Ready to grid!");
//creation
BufferedImage theGrid = new BufferedImage(DIMENSION, DIMENSION, BufferedImage.TYPE_INT_RGB);
//modification
//start with the horizion
int yIntercept = 0;
int i = 0;
drawLine(theGrid, 0, BOTTOM, DIMENSION - 1, BOTTOM);
for (i = 4; i < 32; i++)
{
yIntercept = DIMENSION/i + DIMENSION/2;
drawLine(theGrid, 0, yIntercept, DIMENSION, yIntercept);
}
final int HORIZON = yIntercept;
if (DEBUG)
{
System.out.println("Horizon is at y-intercept: " + HORIZON);
}
//then the center line
//drawLine(theGrid, DIMENSION/2, HORIZON, DIMENSION/2, DIMENSION);
//TODO form rectangles
int horizonIntercept = HORIZON;
int bottomIntercept = BOTTOM;
/*
for (i = 0; i < DIMENSION; i += DIMENSION/8)
{
//horizonIntercept = DIMENSION/2 - DIMENSION/i;
//horizonIntercept = Math.abs(i, i - DIMENSION/16);
drawLine(theGrid, horizonIntercept, HORIZON, i, BOTTOM);
}
*/
for (i = 0; i < DIMENSION/2; i += DIMENSION/8)
{
bottomIntercept = i + DIMENSION/16;
drawLine(theGrid, i, HORIZON, bottomIntercept, BOTTOM);
}
//then the center lines
//drawLine(theGrid, DIMENSION/2, HORIZON, DIMENSION/2, DIMENSION);
drawLine(theGrid, DIMENSION/2 + DIMENSION/16, HORIZON, DIMENSION/2, DIMENSION);
for (i = DIMENSION/2 + DIMENSION/8; i < DIMENSION - DIMENSION/16; i += DIMENSION/8)
{
horizonIntercept = i + DIMENSION/16;
//drawLine(theGrid, i, HORIZON, horizonIntercept, BOTTOM);
//drawLine(theGrid, horizonIntercept, HORIZON, i, BOTTOM);
drawLine(theGrid, horizonIntercept, HORIZON, i, BOTTOM);
}
//serialization
try
{
File outputFile = new File("java_grid.bmp");
ImageIO.write(theGrid, "bmp", outputFile);
}
catch (IOException e)
{
System.out.println("Couldn't write!");
}
if (DEBUG)
{
System.out.printf("R: %x\tG: %x\tB: %x\n", Color.red.getRGB(), Color.green.getRGB(), Color.blue.getRGB());
}
System.out.println("Done!");
}
public static void drawLine(BufferedImage img, int fromX, int fromY, int toX, int toY)
{
if (DEBUG)
{
System.out.println("Got fromX: " + fromX + " fromY: " + fromY + " toX: " + toX + " toY: " + toY);
}
if (fromX == toX && fromY == toY || (fromX > DIMENSION || fromY > DIMENSION))
{
return;
}
else
{
//img.setRGB(fromX, fromY, Color.white.getRGB());
//img.setRGB(fromX, fromY, Color.green.getRGB());
int color = 0;
if (fromX < DIMENSION / 4)
{
color = Color.red.getRGB();
}
else if (fromX < DIMENSION / 2)
{
color = Color.yellow.getRGB();
}
else if (fromX < DIMENSION * 3.0/4)
{
color = Color.green.getRGB();
}
else
{
color = Color.blue.getRGB();
}
img.setRGB(fromX, fromY, color);
int newFromX = (fromX == toX) ? fromX : (fromX < toX) ? fromX + 1 : fromX - 1;
int newFromY = (fromY == toY) ? fromY : (fromY < toY) ? fromY + 1 : fromY - 1;
drawLine(img, newFromX, newFromY, toX, toY);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment