public void ritaUt() { Complex z, hej; boolean isInside; int antalN = 0; background(0); for (int i = 0; i < width * height; i++) { isInside = true; z = toComplex(indexToPixel(i)[0], indexToPixel(i)[1]); for (int a = 0; a <= maxIterations; a++) { if ((z.getRe() * z.getRe() + z.getIm() * z.getIm()) > 4.0) { isInside = false; antalN = a; break; } hej = z.mul(z); z = hej.add(c); } if (isInside) { stroke(0); point(indexToPixel(i)[0], indexToPixel(i)[1]); } else { // the smmoth fade from black to red to yellow to white to yellow to black. if (antalN <= 12) { stroke(antalN * 21, 0, 0); } else if (antalN <= 24) { stroke(252, (antalN - 12) * 21, 0); } else if (antalN >= 76) { stroke(255 - (antalN - 76) * 7, 140 - (antalN - 76) * 8, 0); } else if (antalN >= 40) { stroke(252, 252 - (antalN - 40) * 4, 140 - (antalN - 40) * 9); } else { stroke(255 /*-((antalN-24)/6)*/, 252, (antalN - 24) * 9); } point(indexToPixel(i)[0], indexToPixel(i)[1]); } } }
@Test public void testDeMoivres() throws Exception { Complex c; c = new Complex(1, 1).deMoivres(2); assertEquals(c.getRe(), 0, DELTA); assertEquals(c.getIm(), 2, DELTA); c = new Complex(3, 4).deMoivres(3); assertEquals(c.getRe(), Math.pow(5, 3) * Math.cos(3 * Math.atan2(4, 3)), DELTA); assertEquals(c.getIm(), Math.pow(5, 3) * Math.sin(3 * Math.atan2(4, 3)), DELTA); }
public int[] toCoordinate(Complex punkt) { int[] koordinater = { (int) (punkt.getRe() * pixelPerRe + (bredd / 2)), (int) ((höjd / 2) + (-1 * punkt.getIm() * pixelPerIm)) }; return koordinater; }
public void draw() { fill(0); noStroke(); rect(0, höjd, bredd, höjd + 21); stroke(220); fill(230); mouse = toComplex(mouseX, mouseY); text(" X: " + Math.round(mouse.getRe() * 1000000) / 1000000.0, 5, höjd + 16); text("Y: " + Math.round(mouse.getIm() * 1000000) / 1000000.0 + "i", bredd / 2, höjd + 16); }
public void mouseReleased() { mouse = toComplex(mouseX, mouseY); if (mouseButton == LEFT) { background(0); minX = (mouse.getRe() - 0.25 * breddZ); maxX = (mouse.getRe() + 0.25 * breddZ); minY = (mouse.getIm() - 0.25 * höjdZ); maxY = (mouse.getIm() + 0.25 * höjdZ); // ------- breddZ = (Math.abs(maxX - minX)); höjdZ = (Math.abs(maxY - minY)); pixelPerRe = (double) (bredd / breddZ); pixelPerIm = (double) (höjd / höjdZ); ritaUt(); } else if (mouseButton == RIGHT) { background(0); minX = (mouse.getRe() - 1.5 * breddZ); maxX = (mouse.getRe() + 1.5 * breddZ); minY = (mouse.getIm() - 1.5 * höjdZ); maxY = (mouse.getIm() + 1.5 * höjdZ); // ------- breddZ = (Math.abs(maxX - minX)); höjdZ = (Math.abs(maxY - minY)); pixelPerRe = (double) (bredd / breddZ); pixelPerIm = (double) (höjd / höjdZ); ritaUt(); } }