Not a member of Pastebin yet?
                        Sign Up,
                        it unlocks many cool features!                    
                - import java.io.BufferedWriter;
 - import java.io.File;
 - import java.io.FileWriter;
 - import java.io.IOException;
 - import java.util.ArrayList;
 - import java.util.HashMap;
 - import javax.swing.JFrame;
 - import java.awt.*;
 - import java.applet.Applet;
 - import javax.swing.*;
 - import javax.imageio.*;
 - import java.awt.image.*;
 - import java.io.OutputStream;
 - import java.io.FileOutputStream;
 - import java.io.FileNotFoundException;
 - public class Factorizer {
 - private static HashMap<Integer, HashMap<Integer, ArrayList<String>>> map = new HashMap<>();
 - // ADD PATH HERE
 - private static String path = "output.csv";
 - private static String image_path = "output.png";
 - private static int col = 0;
 - private static BufferedImage image = null;
 - private static int iMax = 512;
 - private static int xMin = -64;
 - private static int yMin = 0;
 - private static int xMax = 64;
 - private static int yMax = 64;
 - private static int setSize = 12;
 - private static boolean use_bmp = true;
 - public static void main(String[] args) {
 - Factorizer f = new Factorizer();
 - File image_file = null;
 - OutputStream outStream = null;
 - try{
 - image_file = new File(image_path);
 - outStream = new FileOutputStream(image_file);
 - } catch(FileNotFoundException err){
 - }
 - image = new BufferedImage(2000, 2000, BufferedImage.TYPE_4BYTE_ABGR);
 - int a_col = 255;
 - int r_col = 0;
 - int g_col = 0;
 - int b_col = 0;
 - col = (a_col << 24) | (r_col << 16) | (g_col << 8) | b_col;
 - for(int i_col = 0; i_col < 2000; i_col++){
 - for(int j_col = 0; j_col < 2000; j_col++){
 - image.setRGB(i_col, j_col, col);
 - }
 - }
 - r_col = 255;
 - g_col = 255;
 - b_col = 255;
 - col = (a_col << 24) | (r_col << 16) | (g_col << 8) | b_col;
 - f.createGrid(true, true, false);
 - try{
 - ImageIO.write(image, "png", image_file);
 - } catch (IOException err2){
 - }
 - }
 - public void createGrid(boolean parseMap, boolean output, boolean showGraph) {
 - //used for integrating with graphing library
 - ArrayList<Integer> xList = new ArrayList<>();
 - ArrayList<Integer> yList = new ArrayList<>();
 - for (int i = 0; i < iMax; i++) {
 - for (int j = 0; j < i; j++) {
 - //x-intercept of the line that goes through the point containing the factors of c is a + 1
 - int a = i - j;
 - int b = i + j;
 - int c = a * b;
 - int d = (int)Math.sqrt(c);
 - int e = c - ((int)Math.pow(d,2));
 - int f = e - ((2 * d) + 1);
 - int n = i - d;
 - int nSquaredPlusD = (int)(Math.pow(n, 2) + d);
 - int x = d - a;
 - //to add a graph,
 - //add variable to use as x and variable to use as y here
 - xList.add(c);
 - yList.add(nSquaredPlusD);
 - if (parseMap) {
 - createGridImpl(e, n, d, x, a, b, f, i, j);
 - /**
 - * THIS IS WHERE THE PIXELS ARE ALTERED
 - */
 - image.setRGB(e, n, col);
 - }
 - }
 - }
 - if (output) {
 - outputGrid();
 - }
 - if (showGraph) {
 - //showGraph(xList, yList, "c", "n^2 + d");
 - }
 - }
 - private void createGridImpl(int e, int n, int d, int x, int a, int b, int f, int i, int j) {
 - if (!map.containsKey(e)) {
 - map.put(e, new HashMap<Integer, ArrayList<String>>());
 - }
 - if (!map.get(e).containsKey(n)) {
 - map.get(e).put(n, new ArrayList<>());
 - }
 - if (!map.containsKey(f)) {
 - map.put(f, new HashMap<Integer, ArrayList<String>>());
 - }
 - if (!map.get(f).containsKey(n - 1)) {
 - map.get(f).put(n - 1, new ArrayList<String>());
 - }
 - String formatTemplate = "%1$s:%2$s:%3$s:%4$s:%5$s:%6$s";
 - String text = "{" + String.format(formatTemplate, e, n, d, x, a, b) + "}";
 - map.get(e).get(n).add(text);
 - text = "{" + String.format(formatTemplate, f, b, a, x + 1, d + 1, n - 1) + "}";
 - map.get(f).get(n - 1).add(text);
 - }
 - /*
 - TO ADD THESE METHODS DOWNLOAD A LIBRARY CALLED
 - JFREECHART
 - public void showGraph(ArrayList<Integer> xList, ArrayList<Integer> yList, String xName, String yName) {
 - String title = "Values of "+ yName +" for values of "+ xName;
 - JFrame frame = new JFrame(title);
 - frame.setLayout(new BorderLayout());
 - JFreeChart chart = ChartFactory.createXYLineChart(title,
 - xName, yName,
 - parseData(xList, yList), PlotOrientation.VERTICAL, true, true, false);
 - frame.add(new ChartPanel(chart), BorderLayout.CENTER);
 - frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
 - frame.pack();
 - frame.setVisible(true);
 - }
 - public XYSeriesCollection parseData(ArrayList<Integer> xList, ArrayList<Integer> yList) {
 - XYSeries series = new XYSeries("");
 - long x;
 - long y;
 - for (int i = 0; i < xList.size(); i++) {
 - x = xList.get(i);
 - y = yList.get(i);
 - series.add((double) x, (double) y);
 - }
 - XYSeriesCollection dataSet = new XYSeriesCollection();
 - dataSet.addSeries(series);
 - return dataSet;
 - }
 - */
 - private void outputGrid() {
 - File f = new File(path);
 - try {
 - BufferedWriter bw = new BufferedWriter(new FileWriter(f));
 - for (int y = 0; y < yMax; y++) {
 - for (int z = 0; z < setSize; z++) {
 - for (int x = xMin; x < xMax; x++) {
 - if ((map.containsKey(x)) && (map.get(x).containsKey(y)) && (map.get(x).get(y).size() > z)) {
 - bw.write(map.get(x).get(y).get(z) + ",");
 - } else {
 - bw.write(",");
 - }
 - }
 - bw.write("\n");
 - }
 - }
 - bw.close();
 - } catch (IOException ioe) {
 - ioe.printStackTrace();
 - }
 - }
 - }
 
Advertisement
 
                    Add Comment                
                
                        Please, Sign In to add comment