package handwriting.editor;
public class Drawing {
	private boolean[][] image;
	
	private void init(int width, int height) {
		image = new boolean[width][height];
	}
	
	public Drawing(int width, int height) {
		init(width, height);
	}
	
	public Drawing(String encoded) {
		String[] tokens = encoded.split("\\|");
		int width = Integer.parseInt(tokens[0]);
		int height = Integer.parseInt(tokens[1]);
		init(width, height);

		for (int t = 2, x = 0; t < tokens.length; ++t, ++x) {
			String line = tokens[t];
			for (int y = 0; y < line.length(); ++y) {
				set(x, y, line.charAt(y) == 'X');
			}
		}
	}
	
	public Drawing(double[] elements, int width, int height) {
		if (elements.length != width * height) {
			throw new IllegalArgumentException("elements.length: " + elements.length + " (" + width + "," + height+")");
		}
		
		init(width,height);
		for (int x = 0, i = 0; x < width; ++x) {
			for (int y = 0; y < height; ++y, ++i) {
				set(x, y, elements[i] > 0.5);
			}
		}
	}
	
	public int getWidth() {
		return image.length;
	}
	
	public int getHeight() {
		return image[0].length;
	}
	
	public boolean inBounds(int x, int y) {
		return x >= 0 && x < getWidth() && y >= 0 && y < getHeight();
	}
	
	public void set(int x, int y, boolean on) {
		if (inBounds(x, y)) {
			image[x][y] = on;
		}
	}
	
	public boolean isSet(int x, int y) {
		return image[x][y];
	}
	
	public void clear() {
		for (int x = 0; x < getWidth(); ++x) {
			for (int y = 0; y < getHeight(); ++y) {
				set(x, y, false);
			}
		}
	}
	
	public Drawing makeDilated() {
		return null;
	}
}
