Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

initial commit

  • Loading branch information...
commit fd9030cc01d34ed231e4add4faeefda9b18cbcc5 0 parents
Piotr Wasilewski authored
2  src/conf/MANIFEST.MF
... ... @@ -0,0 +1,2 @@
  1 +Manifest-Version: 1.0
  2 +
131 src/java/wykres/Complex.java
... ... @@ -0,0 +1,131 @@
  1 +package wykres;
  2 +
  3 +/*************************************************************************
  4 + * Compilation: javac Complex.java
  5 + * Execution: java Complex
  6 + *
  7 + * Data type for complex numbers.
  8 + *
  9 + * The data type is "immutable" so once you create and initialize
  10 + * a Complex object, you cannot change it. The "final" keyword
  11 + * when declaring re and im enforces this rule, making it a
  12 + * compile-time error to change the .re or .im fields after
  13 + * they've been initialized.
  14 + *
  15 + * % java Complex
  16 + * a = 5.0 + 6.0i
  17 + * b = -3.0 + 4.0i
  18 + * Re(a) = 5.0
  19 + * Im(a) = 6.0
  20 + * b + a = 2.0 + 10.0i
  21 + * a - b = 8.0 + 2.0i
  22 + * a * b = -39.0 + 2.0i
  23 + * b * a = -39.0 + 2.0i
  24 + * a / b = 0.36 - 1.52i
  25 + * (a / b) * b = 5.0 + 6.0i
  26 + * conj(a) = 5.0 - 6.0i
  27 + * |a| = 7.810249675906654
  28 + * tan(a) = -6.685231390246571E-6 + 1.0000103108981198i
  29 + *
  30 + *************************************************************************/
  31 +
  32 +public class Complex {
  33 + private final double re; // the real part
  34 + private final double im; // the imaginary part
  35 +
  36 + // create a new object with the given real and imaginary parts
  37 + public Complex(double real, double imag) {
  38 + re = real;
  39 + im = imag;
  40 + }
  41 +
  42 + // return a string representation of the invoking Complex object
  43 + public String toString() {
  44 + if (im == 0) return re + "";
  45 + if (re == 0) return im + "i";
  46 + if (im < 0) return re + " - " + (-im) + "i";
  47 + return re + " + " + im + "i";
  48 + }
  49 +
  50 + public double phase() { return Math.atan2(im, re); } // between -pi and pi
  51 +
  52 + // return a new Complex object whose value is (this + b)
  53 + public Complex plus(Complex b) {
  54 + Complex a = this; // invoking object
  55 + double real = a.re + b.re;
  56 + double imag = a.im + b.im;
  57 + return new Complex(real, imag);
  58 + }
  59 +
  60 + // return a new Complex object whose value is (this - b)
  61 + public Complex minus(Complex b) {
  62 + Complex a = this;
  63 + double real = a.re - b.re;
  64 + double imag = a.im - b.im;
  65 + return new Complex(real, imag);
  66 + }
  67 +
  68 + // return a new Complex object whose value is (this * b)
  69 + public Complex times(Complex b) {
  70 + Complex a = this;
  71 + double real = a.re * b.re - a.im * b.im;
  72 + double imag = a.re * b.im + a.im * b.re;
  73 + return new Complex(real, imag);
  74 + }
  75 +
  76 + // scalar multiplication
  77 + // return a new object whose value is (this * alpha)
  78 + public Complex times(double alpha) {
  79 + return new Complex(alpha * re, alpha * im);
  80 + }
  81 +
  82 + // return a new Complex object whose value is the conjugate of this
  83 + public Complex conjugate() { return new Complex(re, -im); }
  84 +
  85 + // return a new Complex object whose value is the reciprocal of this
  86 + public Complex reciprocal() {
  87 + double scale = re*re + im*im;
  88 + return new Complex(re / scale, -im / scale);
  89 + }
  90 +
  91 + // return the real or imaginary part
  92 + public double re() { return re; }
  93 + public double im() { return im; }
  94 +
  95 + // return a / b
  96 + public Complex divides(Complex b) {
  97 + Complex a = this;
  98 + return a.times(b.reciprocal());
  99 + }
  100 +
  101 + // return a new Complex object whose value is the complex exponential of this
  102 + public Complex exp() {
  103 + return new Complex(Math.exp(re) * Math.cos(im), Math.exp(re) * Math.sin(im));
  104 + }
  105 +
  106 + // return a new Complex object whose value is the complex sine of this
  107 + public Complex sin() {
  108 + return new Complex(Math.sin(re) * Math.cosh(im), Math.cos(re) * Math.sinh(im));
  109 + }
  110 +
  111 + // return a new Complex object whose value is the complex cosine of this
  112 + public Complex cos() {
  113 + return new Complex(Math.cos(re) * Math.cosh(im), -Math.sin(re) * Math.sinh(im));
  114 + }
  115 +
  116 + // return a new Complex object whose value is the complex tangent of this
  117 + public Complex tan() {
  118 + return sin().divides(cos());
  119 + }
  120 +
  121 +
  122 +
  123 + // a static version of plus
  124 + public static Complex plus(Complex a, Complex b) {
  125 + double real = a.re + b.re;
  126 + double imag = a.im + b.im;
  127 + Complex sum = new Complex(real, imag);
  128 + return sum;
  129 + }
  130 +
  131 +}
75 src/java/wykres/mandelbrot.java
... ... @@ -0,0 +1,75 @@
  1 +package wykres;
  2 +
  3 +import java.io.*;
  4 +import javax.servlet.*;
  5 +import javax.servlet.http.*;
  6 +import java.awt.*;
  7 +import java.awt.image.*;
  8 +import javax.imageio.*;
  9 +import javax.servlet.annotation.WebServlet;
  10 +
  11 +@WebServlet(name = "mandelbrot", urlPatterns = {"/"})
  12 +public class mandelbrot extends HttpServlet
  13 +{
  14 +
  15 + private double abs(Complex z) {
  16 + return Math.sqrt(Math.pow(z.re(), 2)+Math.pow(z.im(), 2));
  17 + }
  18 +
  19 + private int mand(Complex z0, int max) {
  20 + Complex z = z0;
  21 + for (int t = 0; t < max; t++) {
  22 + if (abs(z) > 2.0) return t;
  23 + z = z.times(z).plus(z0);
  24 + }
  25 + return max;
  26 + }
  27 +
  28 + public void doGet(HttpServletRequest req,HttpServletResponse res)
  29 + {
  30 +
  31 + //Set the mime type of the image
  32 + res.setContentType("image/png");
  33 +
  34 + double size=100;
  35 + double xc = 2500;
  36 + double yc = 2500;
  37 +
  38 + int N = 512; // create N-by-N image
  39 + int max = 255; // maximum number of iterations
  40 +
  41 + try
  42 + {
  43 + BufferedImage bufferedImage = new BufferedImage(N, N,
  44 + BufferedImage.TYPE_INT_RGB);
  45 +
  46 + Graphics g = bufferedImage.getGraphics();
  47 + g.setColor(Color.white);
  48 +
  49 + for (int i = 0; i < N; i++) {
  50 + for (int j = 0; j < N; j++) {
  51 + double x0 = xc - size/2 + size*i/N;
  52 + double y0 = yc - size/2 + size*j/N;
  53 + Complex z0 = new Complex(x0, y0);
  54 + int gray = max - mand(z0, max);
  55 + Color color = new Color(gray, gray, gray);
  56 + g.setColor(color);
  57 + g.drawLine(i, N-i-j, i, N-i-j);
  58 + }
  59 + }
  60 + //Free graphic resources
  61 + g.dispose();
  62 +
  63 + //Write the image as a jpg
  64 + ImageIO.write(bufferedImage, "png", res.getOutputStream());
  65 + }
  66 + catch (IOException ioe)
  67 + {
  68 +
  69 + }
  70 + }
  71 +
  72 +
  73 +
  74 +
  75 +}
20 web/index.jsp
... ... @@ -0,0 +1,20 @@
  1 +<%--
  2 + Document : index
  3 + Created on : 2011-12-29, 18:57:19
  4 + Author : wasil
  5 +--%>
  6 +
  7 +<%@page contentType="text/html" pageEncoding="UTF-8"%>
  8 +<!DOCTYPE html>
  9 +<html>
  10 + <head>
  11 + <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  12 + <title>JSP Page</title>
  13 + </head>
  14 + <body>
  15 + <h1>Hello World!</h1>
  16 + <form action="mandelbrot" method="GET">
  17 + <input type="submit" name="Wyslij" value="Pokaż fraktal">
  18 + </form>
  19 + </body>
  20 +</html>

0 comments on commit fd9030c

Please sign in to comment.
Something went wrong with that request. Please try again.