diff --git a/src/demo/parallel/Complex.java b/src/demo/parallel/Complex.java index 134a37946..1e8e7f416 100644 --- a/src/demo/parallel/Complex.java +++ b/src/demo/parallel/Complex.java @@ -30,6 +30,7 @@ */ package demo.parallel; +import java.util.Objects; /** * A complex number is a number that can be expressed in the form a + b * i, where @@ -71,6 +72,22 @@ public Complex plus(Complex b) { return this; } + public Complex minus(Complex b) { + re -= b.re; + im -= b.im; + return this; + } + + public Complex divide(Complex b) { + if (b.re == 0 && b.im == 0) { + throw new ArithmeticException("Division by zero"); + } + double denominator = b.re * b.re + b.im * b.im; + double real = (this.re * b.re + this.im * b.im) / denominator; + double imag = (this.im * b.re - this.re * b.im) / denominator; + return new Complex(real, imag); + } + /** * Multiply operation. * @param b multiplier @@ -93,4 +110,16 @@ public Complex times(Complex b) { public double lengthSQ() { return re * re + im * im; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Complex complex)) return false; + return Double.compare(re, complex.re) == 0 && Double.compare(im, complex.im) == 0; + } + + @Override + public int hashCode() { + return Objects.hash(re, im); + } } \ No newline at end of file diff --git a/src/demo/parallel/MandelbrotSetTask.java b/src/demo/parallel/MandelbrotSetTask.java index adbb217b8..cc1d748c5 100644 --- a/src/demo/parallel/MandelbrotSetTask.java +++ b/src/demo/parallel/MandelbrotSetTask.java @@ -274,8 +274,16 @@ protected Long call() throws Exception { private int calc(Complex comp) { int count = 0; Complex c = new Complex(0, 0); + Complex temp = new Complex(0, 0); do { - c = c.times(c).plus(comp); + temp = c.times(c); + c = temp.plus(comp); + if (count % 5 == 0) { + c = c.minus(new Complex(0.1, 0)); + } + if (count > 5) { + c = c.divide(new Complex(2, 0)); + } count++; } while (count < CAL_MAX_COUNT && c.lengthSQ() < LENGTH_BOUNDARY); return count; @@ -351,13 +359,14 @@ private Color getColor(int count) { * Color stops for colors table: color values */ Color[] cc = { - Color.rgb(40, 0, 0), - Color.RED, - Color.WHITE, - Color.RED, - Color.rgb(100, 0, 0), - Color.RED, - Color.rgb(50, 0, 0) + + Color.rgb(0, 40, 0), + Color.BLUE, + Color.WHITE, + Color.BLUE, + Color.rgb(0, 100, 0), + Color.BLUE, + Color.rgb(0, 50, 0) }; /** diff --git a/src/test/TestComplexOperation.java b/src/test/TestComplexOperation.java new file mode 100644 index 000000000..0423b22fc --- /dev/null +++ b/src/test/TestComplexOperation.java @@ -0,0 +1,48 @@ +package test; + +import demo.parallel.Complex; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +public class TestComplexOperation { + + Complex firstComplexNum; + Complex secondComplexNum; + + @BeforeEach + void setUp() { + firstComplexNum = new Complex(1, 1); + secondComplexNum = new Complex(2, 2); + } + + @Test + void testPlusOperation() { + Complex result = firstComplexNum.plus(secondComplexNum); + assertEquals(new Complex(3, 3), result); + } + + @Test + void testMinusOperation() { + Complex result = firstComplexNum.minus(secondComplexNum); + assertEquals(new Complex(-1, -1), result); + } + + @Test + void testDivideOperation() { + Complex result = firstComplexNum.divide(secondComplexNum); + assertEquals(new Complex(0.5, 0), result); + } + + @Test + void testTimesOperation() { + Complex result = firstComplexNum.times(secondComplexNum); + assertEquals(new Complex(0, 4), result); + } + + @Test + void testDivideByZero() { + Complex zeroComplex = new Complex(0, 0); + assertThrows(ArithmeticException.class, () -> firstComplexNum.divide(zeroComplex)); + } +}