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 #974

Open
wants to merge 5 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 @@ -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 @@ -93,4 +95,30 @@ public Complex times(Complex b) {
public double lengthSQ() {
return re * re + im * im;
}
public Complex divide(Complex other) {
double denom = other.re * other.re + other.im * other.im;
return new Complex((this.re * other.re + this.im * other.im) / denom,
(this.im * other.re - this.re * other.im) / denom);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Complex complex = (Complex) o;
return Double.compare(complex.re, re) == 0 && Double.compare(complex.im, im) == 0;
}

@Override
public int hashCode() {
return Objects.hash(re, im);
}
public double getRe() {
return re;
}

public double getIm() {
return im;
}

}
25 changes: 25 additions & 0 deletions src/demo/parallel/ComplexTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package demo.parallel;

import static org.junit.Assert.assertEquals;


public class ComplexTest {

private static final double DELTA = 1e-9;
@org.junit.Test
public void testAddition() {
Complex c1 = new Complex(1, 1);
Complex c2 = new Complex(2, 2);
assertEquals(new Complex(3, 3), c1.plus(c2));
}
@org.junit.Test
public void testDivision() {
Complex c1 = new Complex(4, 2);
Complex c2 = new Complex(2, 2);
Complex result = c1.divide(c2);

// Сравнение реальной и мнимой частей с допуском
assertEquals(1.5, result.getRe(), DELTA);
assertEquals(-0.5, result.getIm(), DELTA);
}
}
68 changes: 34 additions & 34 deletions src/demo/parallel/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@

/**
* UI main class for MandelbrotSet demo.
*
*
* <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
*/
public class Main extends Application {
Expand All @@ -84,7 +84,7 @@ public class Main extends Application {
* Current position in fractal
*/
private Position position;

/**
* Position that shows the whole fractal
*/
Expand Down Expand Up @@ -119,12 +119,12 @@ public class Main extends Application {
* Calculation task
*/
private MandelbrotSetTask task;

/**
* Sequential calculation task
*/
private MandelbrotSetTask sequentialTask;

/**
* Parallel calculation task
*/
Expand All @@ -134,17 +134,17 @@ public class Main extends Application {
* Image to draw fractal offscreen
*/
private WritableImage wiOffscreen;

/**
* Snapshot of the last fractal image
*/
private WritableImage wiSnapshot;

/**
* Snapshot of the whole fractal
*/
private WritableImage wiGlobalSnapshot;

/**
* Parameters used to make fractal snapshots
*/
Expand All @@ -154,12 +154,12 @@ public class Main extends Application {
* Canvas to present fractal on screen
*/
private Canvas canvas;

/**
* Image view to present canvas snapshot on screen
*/
private ImageView ivCanvasSnapshot = new ImageView();

/**
* Image view to present whole fractal snapshot during fly animation
*/
Expand All @@ -169,7 +169,7 @@ public class Main extends Application {
* Old rootPane origin coordinates
*/
private double oldX, oldY;

/**
* New rootPane origin coordinates
*/
Expand All @@ -179,42 +179,42 @@ public class Main extends Application {
* Property to disable all app controls during fly animation
*/
private BooleanProperty disable;

/**
* Property to update stage title
*/
private StringProperty stageTitle;
private StringProperty stageTitle;

/**
* Time bar relative length for parallel calculation
*/
private DoubleProperty parallelTimeBar;

/**
* Time bar relative length for sequential calculation
*/
private DoubleProperty sequentialTimeBar;

/**
* Progress of the current task
*/
private DoubleProperty progress;

/**
* Time in milliseconds of parallel calculation
*/
private final LongProperty parallelTimeValue = new SimpleLongProperty();

/**
* Time in milliseconds of sequential calculation
*/
private final LongProperty sequentialTimeValue = new SimpleLongProperty();

/**
* Total time of sequential calculation (for comparison)
*/
private double sequentialTotalTime;

/**
* Instance of current flying animation
*/
Expand Down Expand Up @@ -422,9 +422,9 @@ private void flyToPosition(int loc) {
}

/**
* The following method is invoked each JavaFX frame to update current image
* The following method is invoked each JavaFX frame to update current image
* with what was done on other threads.
*
*
* It also reacts on window resize
*/
private void handleFrame() {
Expand Down Expand Up @@ -545,7 +545,7 @@ private void handleWindowResize() {

double minR = position.getMinReal() + position.scale * moveX;
double minI = position.getMinImg() + position.scale * moveY;

double oldWidth = winWidth;
double oldHeight = winHeight;
winWidth = rootPane.getWidth();
Expand Down Expand Up @@ -630,7 +630,7 @@ private void render(boolean compareMode, boolean parallel, Runnable onDone) {
}

/**
* Renders the whole image for the current position in fast mode (not
* Renders the whole image for the current position in fast mode (not
* antialiased)
* @param onDone Runnable to execute when task is finished
*/
Expand All @@ -639,9 +639,9 @@ private void renderFast(Runnable onDone) {
}

/**
* Renders the whole image except for a rectangular area
* Renders the whole image except for a rectangular area
* for the current position in parallel mode
*
*
* @param onDone Runnable to execute when task is finished
* @param minX min x coordinate of a rectangular area to be skipped
* @param minY min y coordinate of a rectangular area to be skipped
Expand All @@ -651,11 +651,11 @@ private void renderFast(Runnable onDone) {
private void render(Runnable onDone, double minX, double minY, double maxX, double maxY) {
render(false, true, onDone, minX, minY, maxX, maxY, false);
}

/**
* Renders a MandelbrotSet image using provided parameters. See {@link
* Renders a MandelbrotSet image using provided parameters. See {@link
* MandelbrotSetTask} for more information.
*
*
* @param compareMode true if in comparison mode
* @param parallel true for parallel, false for sequential
* @param onDone Runnable to execute when task is finished
Expand Down Expand Up @@ -741,7 +741,7 @@ private void render(boolean compareMode, boolean parallel, Runnable onDone, doub
return;
}
}

Scene scene = new Scene(createContent(minR, minI, maxR, maxI), MandelbrotSetTask.colors[1]);
scene.setOnKeyPressed(t -> {
if (t.getCode() == KeyCode.I) {
Expand Down Expand Up @@ -786,7 +786,7 @@ private void clearComparisonValues() {
private void rerender() {
rerender(false, true, null);
}

private void rerender(boolean compareMode, boolean parallel, Runnable onDone) {
stopTask();
canvas.getGraphicsContext2D().setFill(Color.rgb(0, 0, 0, 0.5));
Expand Down Expand Up @@ -1168,7 +1168,7 @@ public void abort() {
canvas.setTranslateX(0);
canvas.setTranslateY(0);
canvas.setScaleX(1);
canvas.setScaleY(1);
canvas.setScaleY(1);
reset();
}
}
Expand All @@ -1177,13 +1177,13 @@ public void abort() {
public void stop() {
super.stop();
}

private void reset() {
running = false;
flyingAnimation = null;
disable.set(false);
}

private void finish() {
stopTask();
ivCanvasSnapshot.setTranslateX(canvas.getTranslateX());
Expand Down
11 changes: 6 additions & 5 deletions src/demo/parallel/MandelbrotSetTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,8 @@ 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(c).plus(comp);
c = c.divide(comp);
count++;
} while (count < CAL_MAX_COUNT && c.lengthSQ() < LENGTH_BOUNDARY);
return count;
Expand Down Expand Up @@ -351,13 +352,13 @@ private Color getColor(int count) {
* Color stops for colors table: color values
*/
Color[] cc = {
Color.rgb(40, 0, 0),
Color.RED,
Color.rgb(0, 0, 40),
Color.BLUE,
Color.WHITE,
Color.RED,
Color.rgb(100, 0, 0),
Color.rgb(0, 0, 100),
Color.RED,
Color.rgb(50, 0, 0)
Color.rgb(0, 0, 50)
};

/**
Expand Down