Advertisement
Guest User

Untitled

a guest
Dec 6th, 2017
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.14 KB | None | 0 0
  1. import java.io.BufferedWriter;
  2. import java.io.File;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import javax.swing.JFrame;
  8. import java.awt.*;
  9. import java.applet.Applet;
  10. import javax.swing.*;
  11. import javax.imageio.*;
  12. import java.awt.image.*;
  13. import java.io.OutputStream;
  14. import java.io.FileOutputStream;
  15. import java.io.FileNotFoundException;
  16.  
  17.  
  18. public class Factorizer {
  19. private static HashMap<Integer, HashMap<Integer, ArrayList<String>>> map = new HashMap<>();
  20.  
  21. // ADD PATH HERE
  22. private static String path = "output.csv";
  23. private static String image_path = "output.png";
  24. private static int col = 0;
  25. private static BufferedImage image = null;
  26.  
  27. private static int iMax = 512;
  28. private static int xMin = -64;
  29. private static int yMin = 0;
  30. private static int xMax = 64;
  31. private static int yMax = 64;
  32. private static int setSize = 12;
  33. private static boolean use_bmp = true;
  34.  
  35. public static void main(String[] args) {
  36. Factorizer f = new Factorizer();
  37. File image_file = null;
  38. OutputStream outStream = null;
  39. try{
  40. image_file = new File(image_path);
  41. outStream = new FileOutputStream(image_file);
  42. } catch(FileNotFoundException err){
  43. }
  44. image = new BufferedImage(2000, 2000, BufferedImage.TYPE_4BYTE_ABGR);
  45. int a_col = 255;
  46. int r_col = 0;
  47. int g_col = 0;
  48. int b_col = 0;
  49. col = (a_col << 24) | (r_col << 16) | (g_col << 8) | b_col;
  50. for(int i_col = 0; i_col < 2000; i_col++){
  51. for(int j_col = 0; j_col < 2000; j_col++){
  52. image.setRGB(i_col, j_col, col);
  53. }
  54. }
  55. r_col = 255;
  56. g_col = 255;
  57. b_col = 255;
  58. col = (a_col << 24) | (r_col << 16) | (g_col << 8) | b_col;
  59. f.createGrid(true, true, false);
  60.  
  61. try{
  62. ImageIO.write(image, "png", image_file);
  63. } catch (IOException err2){
  64. }
  65. }
  66.  
  67. public void createGrid(boolean parseMap, boolean output, boolean showGraph) {
  68. //used for integrating with graphing library
  69. ArrayList<Integer> xList = new ArrayList<>();
  70. ArrayList<Integer> yList = new ArrayList<>();
  71.  
  72. for (int i = 0; i < iMax; i++) {
  73. for (int j = 0; j < i; j++) {
  74.  
  75. //x-intercept of the line that goes through the point containing the factors of c is a + 1
  76. int a = i - j;
  77. int b = i + j;
  78. int c = a * b;
  79.  
  80. int d = (int)Math.sqrt(c);
  81. int e = c - ((int)Math.pow(d,2));
  82.  
  83. int f = e - ((2 * d) + 1);
  84. int n = i - d;
  85. int nSquaredPlusD = (int)(Math.pow(n, 2) + d);
  86.  
  87. int x = d - a;
  88.  
  89. //to add a graph,
  90. //add variable to use as x and variable to use as y here
  91. xList.add(c);
  92. yList.add(nSquaredPlusD);
  93.  
  94. if (parseMap) {
  95. createGridImpl(e, n, d, x, a, b, f, i, j);
  96.  
  97. /**
  98. * THIS IS WHERE THE PIXELS ARE ALTERED
  99. */
  100.  
  101. image.setRGB(e, n, col);
  102. }
  103. }
  104. }
  105.  
  106. if (output) {
  107. outputGrid();
  108. }
  109.  
  110. if (showGraph) {
  111. //showGraph(xList, yList, "c", "n^2 + d");
  112. }
  113. }
  114.  
  115. private void createGridImpl(int e, int n, int d, int x, int a, int b, int f, int i, int j) {
  116.  
  117.  
  118. if (!map.containsKey(e)) {
  119. map.put(e, new HashMap<Integer, ArrayList<String>>());
  120. }
  121.  
  122. if (!map.get(e).containsKey(n)) {
  123. map.get(e).put(n, new ArrayList<>());
  124. }
  125.  
  126. if (!map.containsKey(f)) {
  127. map.put(f, new HashMap<Integer, ArrayList<String>>());
  128. }
  129.  
  130. if (!map.get(f).containsKey(n - 1)) {
  131. map.get(f).put(n - 1, new ArrayList<String>());
  132. }
  133.  
  134. String formatTemplate = "%1$s:%2$s:%3$s:%4$s:%5$s:%6$s";
  135.  
  136. String text = "{" + String.format(formatTemplate, e, n, d, x, a, b) + "}";
  137. map.get(e).get(n).add(text);
  138.  
  139. text = "{" + String.format(formatTemplate, f, b, a, x + 1, d + 1, n - 1) + "}";
  140. map.get(f).get(n - 1).add(text);
  141. }
  142.  
  143. /*
  144. TO ADD THESE METHODS DOWNLOAD A LIBRARY CALLED
  145. JFREECHART
  146.  
  147.  
  148. public void showGraph(ArrayList<Integer> xList, ArrayList<Integer> yList, String xName, String yName) {
  149. String title = "Values of "+ yName +" for values of "+ xName;
  150. JFrame frame = new JFrame(title);
  151. frame.setLayout(new BorderLayout());
  152.  
  153. JFreeChart chart = ChartFactory.createXYLineChart(title,
  154. xName, yName,
  155. parseData(xList, yList), PlotOrientation.VERTICAL, true, true, false);
  156.  
  157. frame.add(new ChartPanel(chart), BorderLayout.CENTER);
  158.  
  159. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  160. frame.pack();
  161. frame.setVisible(true);
  162. }
  163.  
  164. public XYSeriesCollection parseData(ArrayList<Integer> xList, ArrayList<Integer> yList) {
  165. XYSeries series = new XYSeries("");
  166.  
  167. long x;
  168. long y;
  169.  
  170. for (int i = 0; i < xList.size(); i++) {
  171. x = xList.get(i);
  172. y = yList.get(i);
  173.  
  174. series.add((double) x, (double) y);
  175. }
  176.  
  177. XYSeriesCollection dataSet = new XYSeriesCollection();
  178. dataSet.addSeries(series);
  179.  
  180. return dataSet;
  181. }
  182. */
  183.  
  184. private void outputGrid() {
  185. File f = new File(path);
  186.  
  187. try {
  188. BufferedWriter bw = new BufferedWriter(new FileWriter(f));
  189.  
  190. for (int y = 0; y < yMax; y++) {
  191. for (int z = 0; z < setSize; z++) {
  192. for (int x = xMin; x < xMax; x++) {
  193.  
  194. if ((map.containsKey(x)) && (map.get(x).containsKey(y)) && (map.get(x).get(y).size() > z)) {
  195. bw.write(map.get(x).get(y).get(z) + ",");
  196. } else {
  197. bw.write(",");
  198. }
  199.  
  200. }
  201. bw.write("\n");
  202. }
  203. }
  204.  
  205. bw.close();
  206. } catch (IOException ioe) {
  207. ioe.printStackTrace();
  208. }
  209. }
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement