Guest User

Java VQC

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