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

Kabacheuskiy_Dmitriy_250504 #975

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
Binary file added junit-jupiter-api-5.8.1.jar
Binary file not shown.
78 changes: 38 additions & 40 deletions src/demo/parallel/Complex.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,52 +45,50 @@
* @author Alexander Kouznetsov, Tristan Yan
*/
public class Complex {

private double re; // the real part
private double im; // the imaginary part
private final double re; // Real part
private final double im; // Imaginary part

/**
* create a new object with the given real and imaginary parts
*
* @param real a complex number real part
* @param imag a complex number imaginary part
*/
public Complex(double real, double imag) {
re = real;
im = imag;
public Complex(double re, double im) {
this.re = re;
this.im = im;
}

/**
* Add operation.
* @param b summand
* @return this Complex object whose value is (this + b)
*/
public Complex plus(Complex b) {
re += b.re;
im += b.im;
return this;
// Method to calculate the squared length (magnitude) of the complex number
public double lengthSQ() {
return re * re + im * im; // |z|^2 = a^2 + b^2
}

/**
* Multiply operation.
* @param b multiplier
* @return this Complex object whose value is this * b
*/
public Complex times(Complex b) {
Complex a = this;
double real = a.re * b.re - a.im * b.im;
double imag = a.re * b.im + a.im * b.re;
re = real;
im = imag;
return this;
// Addition
public Complex plus(Complex other) {
return new Complex(this.re + other.re, this.im + other.im);
}

/**
* Square of Complex object's length, we're using square of length to
* eliminate the computation of square root
* @return square of length
*/
public double lengthSQ() {
return re * re + im * im;
// Multiplication
public Complex times(Complex other) {
return new Complex(this.re * other.re - this.im * other.im,
this.re * other.im + this.im * other.re);
}

// Subtraction
public Complex minus(Complex other) {
return new Complex(this.re - other.re, this.im - other.im);
}

// Division
public Complex divide(Complex other) {
double denominator = other.re * other.re + other.im * other.im;
return new Complex((this.re * other.re + this.im * other.im) / denominator,
(this.im * other.re - this.re * other.im) / denominator);
}

// Power
public Complex power(int exponent) {
Complex result = new Complex(1, 0); // Initial value for 0th power
for (int i = 0; i < exponent; i++) {
result = result.times(this);
}
return result;
}

// Other methods (e.g., conjugate, etc.) can be added as needed
}
41 changes: 15 additions & 26 deletions src/demo/parallel/MandelbrotSetTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,15 @@
package demo.parallel;


import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;
import javafx.concurrent.Task;
import javafx.scene.image.PixelWriter;
import javafx.scene.paint.Color;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;



/**
* 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 @@ -268,22 +254,21 @@ protected Long call() throws Exception {
* This number is used to choose a color for this pixel for precalculated
* color tables.
*
* @param comp a complex number used for calculation
* @return number of iterations a value stayed within a given disk.
*/
private int calc(Complex comp) {
int count = 0;
Complex c = new Complex(0, 0);
Complex z = new Complex(0, 0);
do {
c = c.times(c).plus(comp);
z = z.times(z).plus(comp).divide(new Complex(1, 1)); // (z^2 + c) / (1 + i)
count++;
} while (count < CAL_MAX_COUNT && c.lengthSQ() < LENGTH_BOUNDARY);
} while (count < CAL_MAX_COUNT && z.lengthSQ() < LENGTH_BOUNDARY);
return count;
}

/**
* Calculates a color of a given pixel on the image using
* {@link #calc(demo.parallel.Complex) } method.

* @param x x coordinate of the pixel in the image
* @param y y coordinate of the pixel in the image
* @return calculated color of the pixel
Expand Down Expand Up @@ -329,15 +314,19 @@ private double clamp(double val) {

/**
* Returns a color for a given iteration count.
* @param count number of iterations return by
* {@link #calc(demo.parallel.Complex)} method
* @param count number of iterations return by
* @return color from pre-calculated table
*/
private Color getColor(int count) {
if (count >= colors.length) {
return Color.BLACK;
}
return colors[count];
double gamma = 0.5; // Измените значение, чтобы настроить гамму
double normalizedCount = (double) count / (colors.length - 1);
double correctedValue = Math.pow(normalizedCount, gamma);

int index = (int) (correctedValue * (colors.length - 1));
return colors[index];
}

/**
Expand Down
47 changes: 47 additions & 0 deletions src/demo/parallel/Tests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package demo.parallel;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class Tests {

@Test
public void testAddition() {
Complex a = new Complex(1, 1);
Complex b = new Complex(2, 3);
Complex result = a.plus(b);
assertEquals(new Complex(3, 4), result);
}

@Test
public void testSubtraction() {
Complex a = new Complex(5, 5);
Complex b = new Complex(2, 3);
Complex result = a.minus(b);
assertEquals(new Complex(3, 2), result);
}

@Test
public void testMultiplication() {
Complex a = new Complex(1, 2);
Complex b = new Complex(3, 4);
Complex result = a.times(b);
assertEquals(new Complex(-5, 10), result);
}


@Test
public void testPower() {
Complex a = new Complex(1, 1);
Complex result = a.power(2);
assertEquals(new Complex(0, 2), result);
}

@Test
public void testLengthSQ() {
Complex a = new Complex(3, 4);
double result = a.lengthSQ();
assertEquals(25, result);
}
}