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

Моисеев В.С. 250504 #967

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
28 changes: 28 additions & 0 deletions src/demo/parallel/Complex.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,32 @@ public Complex times(Complex b) {
public double lengthSQ() {
return re * re + im * im;
}

public Complex divide(Complex b) {
double denom = b.re * b.re + b.im * b.im;
double real = (re * b.re + im * b.im) / denom;
double imag = (im * b.re - re * b.im) / denom;
return new Complex(real, imag);
}

public double modulus() {
return Math.sqrt(re * re + im * im);
}

public Complex pow(int n) {
Complex result = new Complex(1, 0);
Complex base = this;
for (int i = 0; i < n; i++) {
result = result.times(base);
}
return result;
}

public double getRe() {
return re;
}

public double getIm() {
return im;
}
}
24 changes: 4 additions & 20 deletions src/demo/parallel/MandelbrotSetTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,6 @@
import javafx.scene.paint.Color;


/**
* 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.
*
* <p><i>
* This source code is provided to illustrate the usage of a given feature
* or technique and has been deliberately simplified. Additional steps
* required for a production-quality application, such as security checks,
* input validation and proper error handling, might not be present in
* this sample code.</i>
*
* @author Alexander Kouznetsov, Tristan Yan
*/
class MandelbrotSetTask extends Task<Long> {

/**
Expand Down Expand Up @@ -275,7 +259,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(comp).plus(comp);
count++;
} while (count < CAL_MAX_COUNT && c.lengthSQ() < LENGTH_BOUNDARY);
return count;
Expand Down Expand Up @@ -352,11 +336,11 @@ private Color getColor(int count) {
*/
Color[] cc = {
Color.rgb(40, 0, 0),
Color.RED,
Color.BLUE,
Color.WHITE,
Color.RED,
Color.BLUE,
Color.rgb(100, 0, 0),
Color.RED,
Color.BLUE,
Color.rgb(50, 0, 0)
};

Expand Down
27 changes: 27 additions & 0 deletions src/test/demo/parallel/ComplexTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package test.demo.parallel;
import demo.parallel.Complex;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ComplexTest {
@Test
void testDivide() {
Complex c1 = new Complex(2, 3);
Complex c2 = new Complex(1, -1);
Complex result = c1.divide(c2);
assertEquals(-0.5, result.getRe(), 1e-9);
assertEquals(2.5, result.getIm(), 1e-9);
}
@Test
void testModulus() {
Complex c = new Complex(3, 4);
double result = c.modulus();
assertEquals(5, result, 1e-9); // 5.0 is the modulus of 3 + 4i
}
@Test
void testPow() {
Complex c = new Complex(2, 3);
Complex result = c.pow(2);
assertEquals(-5, result.getRe());
assertEquals(12, result.getIm());
}
}