Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Волков П.А. 230501 #949

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions src/Tests/TestOperations.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package Tests;

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 TestOperations {

@Test
public void testAddition() {
Complex c1 = new Complex(1, 2);
Complex c2 = new Complex(3, 4);
Complex result = c1.plus(c2);

assertEquals(new Complex(4, 6), result);
}

@Test
public void testSubtraction() {
Complex c1 = new Complex(5, 7);
Complex c2 = new Complex(2, 3);
Complex result = c1.minus(c2);

assertEquals(new Complex(3, 4), result);
}

@Test
public void testMultiplication() {
Complex c1 = new Complex(1, 2);
Complex c2 = new Complex(3, 4);
Complex result = c1.times(c2);

assertEquals(new Complex(-5, 10), result);
}


@Test
public void testDivisionByZero() {
Complex c1 = new Complex(1, 2);
Complex c2 = new Complex(0, 0);

assertThrows(ArithmeticException.class, () -> {
c1.divide(c2);
});
}

@Test
public void testLengthSQ() {
Complex c = new Complex(3, 4);
double result = c.lengthSQ();

assertEquals(25, result);
}

@Test
public void testEquality() {
Complex c1 = new Complex(1, 1);
Complex c2 = new Complex(1, 1);

assertEquals(c1, c2);
}

@Test
public void testInequality() {
Complex c1 = new Complex(1, 1);
Complex c2 = new Complex(2, 1);

assertNotEquals(c1, c2);
}
}
28 changes: 27 additions & 1 deletion src/demo/parallel/Complex.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
package demo.parallel;


import java.util.Objects;

/**
* A complex number is a number that can be expressed in the form a + b * i, where
* a and b are real numbers and i is the imaginary unit, which satisfies the
Expand Down Expand Up @@ -84,7 +86,20 @@ public Complex times(Complex b) {
im = imag;
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);
}
/**
* Square of Complex object's length, we're using square of length to
* eliminate the computation of square root
Expand All @@ -93,4 +108,15 @@ 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);
}
}
18 changes: 9 additions & 9 deletions src/demo/parallel/MandelbrotSetTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

/**
* Task to render Mandelbrot set using given parameters. See {@link
* #MandelbrotRendererTask(boolean, javafx.scene.image.PixelWriter, int, int,

* double, double, double, double, double, double, double, double, boolean)
* constructor} for parameters list. The task returns time in milliseconds as
* its calculated value.
Expand Down Expand Up @@ -275,7 +275,7 @@ private int calc(Complex comp) {
int count = 0;
Complex c = new Complex(0, 0);
do {
c = c.times(c).plus(comp);
c = c.times(c).times(comp).plus(comp).plus(c).times(c).divide(comp).minus(c);
count++;
} while (count < CAL_MAX_COUNT && c.lengthSQ() < LENGTH_BOUNDARY);
return count;
Expand Down Expand Up @@ -351,13 +351,13 @@ 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.GREEN,
Color.WHITE,
Color.GREEN,
Color.rgb(0, 100, 0),
Color.GREEN,
Color.rgb(0, 50, 0)
};

/**
Expand Down