Julia set

From Rosetta Code
Revision as of 11:19, 25 March 2016 by Trizen (talk | contribs) (→‎{{header|Perl}}: mor code simplifications)
Julia set is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Generate and draw a Julia set.

Cf


J

<lang j>load '~addons/graphics/fvj4/complex_dynamics.ijs' pal2=: 255,~0,<.(254$1 0.8 0.6)*Hue 5r6*(i.%<:)254 g=: [: %: 0.3746j0.102863 0.132565j0.389103 _0.373935j_0.353777 1&p. view_image pal2;b=:g escapetc (10 255) 500 zl_clur _1.5 1.5j1.5</lang>

See also: Fractals Visualization and J, 4th edition, Part 1 (by Clifford A. Reiter), Chapter 6

See http://webbox.lafayette.edu/~reiterc/mvp/ec_julia/index.html for some other examples. (That said, note that this is a link into a small college site and it might drift over time. In the past, for example, you would have had to use 'www' where it currently says 'webbox')

Java

Works with: Java version 8

<lang java>import java.awt.*; import java.awt.image.BufferedImage; import javax.swing.*;

public class JuliaSet extends JPanel {

   private final int maxIter = 300;
   private final double zoom = 1;
   private double cY, cX;
   public JuliaSet() {
       setPreferredSize(new Dimension(800, 600));
       setBackground(Color.white);
   }
   void drawJuliaSet(Graphics2D g) {
       int w = getWidth();
       int h = getHeight();
       BufferedImage image = new BufferedImage(w, h,
               BufferedImage.TYPE_INT_RGB);
       cX = -0.7;
       cY = 0.27015;
       double moveX = 0, moveY = 0;
       double zx, zy;
       for (int x = 0; x < w; x++) {
           for (int y = 0; y < h; y++) {
               zx = 1.5 * (x - w / 2) / (0.5 * zoom * w) + moveX;
               zy = (y - h / 2) / (0.5 * zoom * h) + moveY;
               float i = maxIter;
               while (zx * zx + zy * zy < 4 && i > 0) {
                   double tmp = zx * zx - zy * zy + cX;
                   zy = 2.0 * zx * zy + cY;
                   zx = tmp;
                   i--;
               }
               int c = Color.HSBtoRGB((maxIter / i) % 1, 1, i > 0 ? 1 : 0);
               image.setRGB(x, y, c);
           }
       }
       g.drawImage(image, 0, 0, null);
   }
   @Override
   public void paintComponent(Graphics gg) {
       super.paintComponent(gg);
       Graphics2D g = (Graphics2D) gg;
       g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
               RenderingHints.VALUE_ANTIALIAS_ON);
       drawJuliaSet(g);
   }
   public static void main(String[] args) {
       SwingUtilities.invokeLater(() -> {
           JFrame f = new JFrame();
           f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           f.setTitle("Julia Set");
           f.setResizable(false);
           f.add(new JuliaSet(), BorderLayout.CENTER);
           f.pack();
           f.setLocationRelativeTo(null);
           f.setVisible(true);
       });
   }

}</lang>

Perl

<lang perl>use Imager;

my($w, $h, $zoom) = (800, 600, 1); my $img = Imager->new(xsize => $w, ysize => $h, channels => 3);

my $maxIter = 255; my ($cX, $cY) = (-0.7, 0.27015); my ($moveX, $moveY) = (0, 0);

my $color = Imager::Color->new('#000000');

foreach my $x (0 .. $w - 1) {

   foreach my $y (0 .. $h - 1) {
       my $zx = (1.5 * ($x - $w / 2) / (0.5 * $zoom * $w) + $moveX);
       my $zy = (($y - $h / 2) / (0.5 * $zoom * $h) + $moveY);
       my $i = $maxIter;
       while ($zx**2 + $zy**2 < 4 and --$i >= 0) {
           ($zy, $zx) = (2 * $zx * $zy + $cY, $zx**2 - $zy**2 + $cX);
       }
       $color->set(hsv => [$i / $maxIter * 360, 1, $i > 0 ? 1 : 0]);
       $img->setpixel(x => $x, y => $y, color => $color);
   }

}

$img->write(file => 'julia_set.png');</lang>

Perl 6

Translation of: Perl

with the pallette swapped, just because.

Works with: Rakudo version 2016.03

<lang perl6>use Image::PNG::Portable;

my ($w, $h) = (800, 600);

my $out = Image::PNG::Portable.new: :width($w), :height($h);

my $maxIter = 255;

my ($cX, $cY) = (-0.7, 0.27015);

julia($out);

$out.write: 'Julia-set-perl6.png';

sub julia ( $png ) {

   for ^$w -> $x {
       for ^$h -> $y {
           my $zx = (1.5 * ($x - $w / 2) / (0.5 * $w));
           my $zy = (($y - $h / 2) / (0.5 * $h));
           my $i = $maxIter;
           while ($zx² + $zy² < 4 and $i > 1) {
               ($zy, $zx) = (2.0 * $zx * $zy + $cY, $zx² - $zy² + $cX);
               $i--;
           }
           $png.set( $x, $y, |hsv2rgb($i / $maxIter * 360, 1, $i > 1 ?? 1 !! 0) );
       }
   }

}

sub hsv2rgb ( $h, $s, $v ){

   my $c = $v * $s;
   my $x = $c * (1 - abs( (($h/60) % 2) - 1 ) );
   my $m = $v - $c;
   my ($r, $g, $b);
   given $h {
       when   0..^60  { ($r, $g, $b) = ($c, $x, 0) }
       when  60..^120 { ($r, $g, $b) = ($x, $c, 0) }
       when 120..^180 { ($r, $g, $b) = (0, $c, $x) }
       when 180..^240 { ($r, $g, $b) = (0, $x, $c) }
       when 240..^300 { ($r, $g, $b) = ($x, 0, $c) }
       when 300..^360 { ($r, $g, $b) = ($c, 0, $x) }
   }
   ( $r, $g, $b ) = map { (($_+$m) * 255).Int }, $r, $g, $b;
   return $b, $g, $r;

}</lang>

zkl

Uses the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl

Translation of: Java

<lang zkl>fcn juliaSet{

  w,h,zoom:=800,600, 1;
  bitmap:=PPM(w,h,0xFF|FF|FF);  // White background
  cX,cY:=-0.7, 0.27015;
  moveX,moveY:=0.0, 0.0;
  maxIter:=255;
  foreach x,y in (w,h){
     zx:=1.5*(x - w/2)/(0.5*zoom*w) + moveX;
     zy:=1.0*(y - h/2)/(0.5*zoom*h) + moveY;
     i:=maxIter;
     while(zx*zx + zy*zy < 4 and i > 1){

tmp:=zx*zx - zy*zy + cX; zy,zx=2.0*zx*zy + cY, tmp; i-=1;

     }
     // convert byte to RGB (3 bytes), kinda magic to get nice colors
     bitmap[x,y]=i.shiftLeft(21) + i.shiftLeft(10) + i*8;
  }
  bitmap.writeJPGFile("juliaSet.jpg",True);

}();</lang>