Added sequential version
This commit is contained in:
		
							parent
							
								
									12f1744451
								
							
						
					
					
						commit
						d440949907
					
				
					 6 changed files with 64 additions and 44 deletions
				
			
		|  | @ -3,6 +3,7 @@ package com.albertoventurini; | ||||||
| import com.albertoventurini.juliaset.JuliaSetConfig; | import com.albertoventurini.juliaset.JuliaSetConfig; | ||||||
| import com.albertoventurini.juliaset.calculator.JuliaSetCalculator; | import com.albertoventurini.juliaset.calculator.JuliaSetCalculator; | ||||||
| import com.albertoventurini.juliaset.JuliaSetPanel; | import com.albertoventurini.juliaset.JuliaSetPanel; | ||||||
|  | import com.albertoventurini.juliaset.calculator.SequentialJuliaSetCalculator; | ||||||
| import com.albertoventurini.juliaset.calculator.ThreadedJuliaSetCalculator; | import com.albertoventurini.juliaset.calculator.ThreadedJuliaSetCalculator; | ||||||
| 
 | 
 | ||||||
| import javax.swing.*; | import javax.swing.*; | ||||||
|  | @ -14,7 +15,7 @@ public class MainClass { | ||||||
| 
 | 
 | ||||||
|         int width = 1600; |         int width = 1600; | ||||||
|         int height = 1000; |         int height = 1000; | ||||||
|         JuliaSetCalculator calculator = new ThreadedJuliaSetCalculator(width, height); |         JuliaSetCalculator calculator = new SequentialJuliaSetCalculator(); | ||||||
|         JuliaSetConfig config = new JuliaSetConfig(width, height, 1000, 1.0, |         JuliaSetConfig config = new JuliaSetConfig(width, height, 1000, 1.0, | ||||||
|                 -0.7, 0.27015, 0.0, 0.0); |                 -0.7, 0.27015, 0.0, 0.0); | ||||||
|         JuliaSetPanel juliaSetPanel = new JuliaSetPanel(calculator, config); |         JuliaSetPanel juliaSetPanel = new JuliaSetPanel(calculator, config); | ||||||
|  |  | ||||||
|  | @ -7,9 +7,6 @@ import java.awt.*; | ||||||
| import java.awt.image.BufferedImage; | import java.awt.image.BufferedImage; | ||||||
| 
 | 
 | ||||||
| public class JuliaSetPanel extends JPanel { | public class JuliaSetPanel extends JPanel { | ||||||
|     private static int maxIter = 1000; |  | ||||||
|     private static double zoom = 1; |  | ||||||
|     private double cY, cX; |  | ||||||
| 
 | 
 | ||||||
|     private JuliaSetCalculator juliaSetCalculator; |     private JuliaSetCalculator juliaSetCalculator; | ||||||
|     private JuliaSetConfig config; |     private JuliaSetConfig config; | ||||||
|  | @ -26,16 +23,12 @@ public class JuliaSetPanel extends JPanel { | ||||||
|         int height = getHeight(); |         int height = getHeight(); | ||||||
|         BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); |         BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); | ||||||
| 
 | 
 | ||||||
|         cX = -0.7; |  | ||||||
|         cY = 0.27015; |  | ||||||
|         double moveX = 0, moveY = 0; |  | ||||||
| 
 |  | ||||||
|         int[][] iterations = juliaSetCalculator.calculate(config); |         int[][] iterations = juliaSetCalculator.calculate(config); | ||||||
| 
 | 
 | ||||||
|         for(int x = 0; x < width; x++) { |         for(int x = 0; x < width; x++) { | ||||||
|             for(int y = 0; y < height; y++) { |             for(int y = 0; y < height; y++) { | ||||||
|                 float i = iterations[x][y]; |                 float i = iterations[x][y]; | ||||||
|                 int color = Color.HSBtoRGB(((maxIter / i) + 0.5f) % 1, 1, i > 0 ? 1 : 0); |                 int color = Color.HSBtoRGB(((config.getMaxIterations() / i) + 0.5f) % 1, 1, i > 0 ? 1 : 0); | ||||||
|                 image.setRGB(x, y, color); |                 image.setRGB(x, y, color); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  | @ -47,8 +40,7 @@ public class JuliaSetPanel extends JPanel { | ||||||
|     public void paintComponent(Graphics gg) { |     public void paintComponent(Graphics gg) { | ||||||
|         super.paintComponent(gg); |         super.paintComponent(gg); | ||||||
|         Graphics2D g = (Graphics2D) gg; |         Graphics2D g = (Graphics2D) gg; | ||||||
|         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, |         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); | ||||||
|                 RenderingHints.VALUE_ANTIALIAS_ON); |  | ||||||
|         drawJuliaSet(g); |         drawJuliaSet(g); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -6,19 +6,12 @@ import java.util.concurrent.*; | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| public class ExecutorServiceJuliaSetCalculator implements JuliaSetCalculator { | public class ExecutorServiceJuliaSetCalculator implements JuliaSetCalculator { | ||||||
|     private int width; |  | ||||||
|     private int height; |  | ||||||
|     private int[][] iterations; |  | ||||||
| 
 |  | ||||||
|     public ExecutorServiceJuliaSetCalculator(final int width, final int height) { |  | ||||||
|         this.width = width; |  | ||||||
|         this.height = height; |  | ||||||
|         iterations = new int[width][height]; |  | ||||||
|     } |  | ||||||
| 
 | 
 | ||||||
|     public int[][] calculate(final JuliaSetConfig config) { |     public int[][] calculate(final JuliaSetConfig config) { | ||||||
| 
 | 
 | ||||||
|         int[][] result = new int[config.getWidth()][config.getHeight()]; |         int width = config.getWidth(); | ||||||
|  |         int height = config.getHeight(); | ||||||
|  |         int[][] iterations = new int[width][height]; | ||||||
|         double zoom = config.getZoom(); |         double zoom = config.getZoom(); | ||||||
|         double moveX = config.getMoveX(); |         double moveX = config.getMoveX(); | ||||||
|         double moveY = config.getMoveY(); |         double moveY = config.getMoveY(); | ||||||
|  | @ -58,7 +51,7 @@ public class ExecutorServiceJuliaSetCalculator implements JuliaSetCalculator { | ||||||
| 
 | 
 | ||||||
|         long startTime = System.currentTimeMillis(); |         long startTime = System.currentTimeMillis(); | ||||||
| 
 | 
 | ||||||
|         ExecutorService executorService = Executors.newWorkStealingPool(8); |         ExecutorService executorService = Executors.newFixedThreadPool(8); | ||||||
|         for(int x = 0; x < width; x++) { |         for(int x = 0; x < width; x++) { | ||||||
|             JuliaTask task = new JuliaTask(x); |             JuliaTask task = new JuliaTask(x); | ||||||
|             executorService.submit(task); |             executorService.submit(task); | ||||||
|  |  | ||||||
|  | @ -7,20 +7,12 @@ import java.util.concurrent.RecursiveAction; | ||||||
| 
 | 
 | ||||||
| public class ForkJoinJuliaSetCalculator implements JuliaSetCalculator { | public class ForkJoinJuliaSetCalculator implements JuliaSetCalculator { | ||||||
| 
 | 
 | ||||||
|     private int width; |  | ||||||
|     private int height; |  | ||||||
|     private int[][] iterations; |  | ||||||
| 
 |  | ||||||
|     public ForkJoinJuliaSetCalculator(final int width, final int height) { |  | ||||||
|         this.width = width; |  | ||||||
|         this.height = height; |  | ||||||
|         iterations = new int[width][height]; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     @Override |     @Override | ||||||
|     public int[][] calculate(final JuliaSetConfig config) { |     public int[][] calculate(final JuliaSetConfig config) { | ||||||
| 
 | 
 | ||||||
|         int[][] result = new int[config.getWidth()][config.getHeight()]; |         int width = config.getWidth(); | ||||||
|  |         int height = config.getHeight(); | ||||||
|  |         int[][] iterations = new int[width][height]; | ||||||
|         double zoom = config.getZoom(); |         double zoom = config.getZoom(); | ||||||
|         double moveX = config.getMoveX(); |         double moveX = config.getMoveX(); | ||||||
|         double moveY = config.getMoveY(); |         double moveY = config.getMoveY(); | ||||||
|  |  | ||||||
|  | @ -0,0 +1,50 @@ | ||||||
|  | package com.albertoventurini.juliaset.calculator; | ||||||
|  | 
 | ||||||
|  | import com.albertoventurini.juliaset.JuliaSetConfig; | ||||||
|  | 
 | ||||||
|  | import java.util.concurrent.ExecutorService; | ||||||
|  | import java.util.concurrent.Executors; | ||||||
|  | import java.util.concurrent.TimeUnit; | ||||||
|  | 
 | ||||||
|  | public class SequentialJuliaSetCalculator implements JuliaSetCalculator { | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public int[][] calculate(JuliaSetConfig config) { | ||||||
|  | 
 | ||||||
|  |         int width = config.getWidth(); | ||||||
|  |         int height = config.getHeight(); | ||||||
|  |         int[][] iterations = new int[width][height]; | ||||||
|  |         double zoom = config.getZoom(); | ||||||
|  |         double moveX = config.getMoveX(); | ||||||
|  |         double moveY = config.getMoveY(); | ||||||
|  |         int maxIterations = config.getMaxIterations(); | ||||||
|  |         double cx = config.getCx(); | ||||||
|  |         double cy = config.getCy(); | ||||||
|  | 
 | ||||||
|  |         long startTime = System.currentTimeMillis(); | ||||||
|  | 
 | ||||||
|  |         for(int x = 0; x < width; x++) { | ||||||
|  |             double zx, zy; | ||||||
|  |             for(int y = 0; y < height; y++) { | ||||||
|  |                 zx = 1.5 * (x - width / 2) / (0.5 * zoom * width) + moveX; | ||||||
|  |                 zy = (y - height / 2) / (0.5 * zoom * height) + moveY; | ||||||
|  |                 int i = maxIterations; | ||||||
|  |                 while (zx * zx + zy * zy < 4 && i > 0) { | ||||||
|  |                     double tmp = zx * zx - zy * zy + cx; | ||||||
|  |                     zy = 2.0 * zx * zy + cy; | ||||||
|  |                     zx = tmp; | ||||||
|  |                     i--; | ||||||
|  |                 } | ||||||
|  |                 iterations[x][y] = i; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         long endTime = System.currentTimeMillis(); | ||||||
|  | 
 | ||||||
|  |         System.out.println(endTime - startTime); | ||||||
|  | 
 | ||||||
|  |         return iterations; | ||||||
|  | 
 | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  | @ -5,20 +5,12 @@ import com.albertoventurini.juliaset.calculator.JuliaSetCalculator; | ||||||
| 
 | 
 | ||||||
| public class ThreadedJuliaSetCalculator implements JuliaSetCalculator { | public class ThreadedJuliaSetCalculator implements JuliaSetCalculator { | ||||||
| 
 | 
 | ||||||
|     private int width; |  | ||||||
|     private int height; |  | ||||||
|     private int[][] iterations; |  | ||||||
| 
 |  | ||||||
|     public ThreadedJuliaSetCalculator(final int width, final int height) { |  | ||||||
|         this.width = width; |  | ||||||
|         this.height = height; |  | ||||||
|         iterations = new int[width][height]; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     @Override |     @Override | ||||||
|     public int[][] calculate(final JuliaSetConfig config) { |     public int[][] calculate(final JuliaSetConfig config) { | ||||||
| 
 | 
 | ||||||
|         int[][] result = new int[config.getWidth()][config.getHeight()]; |         int width = config.getWidth(); | ||||||
|  |         int height = config.getHeight(); | ||||||
|  |         int[][] iterations = new int[width][height]; | ||||||
|         double zoom = config.getZoom(); |         double zoom = config.getZoom(); | ||||||
|         double moveX = config.getMoveX(); |         double moveX = config.getMoveX(); | ||||||
|         double moveY = config.getMoveY(); |         double moveY = config.getMoveY(); | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue