Skip to content
Snippets Groups Projects
Commit 0558b21c authored by Tam Minh Le's avatar Tam Minh Le
Browse files

Merge branch 'JuliaTransformation' into 'dev'

Julia transformation

See merge request !18
parents 63b5d49a 31c3e357
No related branches found
No related tags found
2 merge requests!54Final release,!18Julia transformation
Showing
with 180 additions and 20 deletions
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GitToolBoxProjectSettings">
<option name="commitMessageIssueKeyValidationOverride">
<BoolValueOverride>
<option name="enabled" value="true" />
</BoolValueOverride>
</option>
<option name="commitMessageValidationEnabledOverride">
<BoolValueOverride>
<option name="enabled" value="true" />
</BoolValueOverride>
</option>
</component>
</project>
\ No newline at end of file
......@@ -66,7 +66,7 @@
<id>default-cli</id>
<configuration>
<mainClass>
edu.ntnu.idatt2003.mappevurderingprog2/edu.ntnu.idatt2003.mappevurderingprog2.HelloApplication
edu.ntnu.idatt2003.mappevurderingprog2.MyApp
</mainClass>
<launcher>app</launcher>
<jlinkZipName>app</jlinkZipName>
......
package edu.ntnu.idatt2003.mappevurderingprog2;
import edu.ntnu.idatt2003.mappevurderingprog2.views.View;
import javafx.application.Application;
import javafx.stage.Stage;
public class MyApp extends Application{
@Override
public void start(Stage primaryStage) {
View view = new View();
primaryStage.setScene(view.createScene());
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
package edu.ntnu.idatt2003.mappevurderingprog2.controllers;
public class Controller{
}
\ No newline at end of file
......@@ -48,11 +48,18 @@ public class Complex extends Vector2D {
*/
public Complex sqrt() {
double r = Math.sqrt(Math.pow(getX0(), 2) + Math.pow(getX1(), 2));
double theta = Math.atan2(getX1(), getX0());
return new Complex(Math.sqrt(r) * Math.cos(theta / 2), Math.sqrt(r) * Math.sin(theta / 2));
double theta = Math.atan2(getX1(), getX0()) / 2.0;
double sqrtMag = Math.sqrt(r);
double realPart = sqrtMag * Math.cos(theta);
double imaginaryPart = sqrtMag * Math.sin(theta);
return new Complex(realPart, imaginaryPart);
}
public double magnitudeSquared() {
return getX0() * getX0() + getX1() * getX1();
}
public String toString() {
return "(" + getX0() + " + " + getX1() + "i)";
}
}
\ No newline at end of file
......@@ -53,7 +53,6 @@ public class JuliaTransform implements Transform2D {
if (sign == -1) {
subtracted = new Complex(-subtracted.getX0(), -subtracted.getX1());
}
return subtracted;
}
}
......
......@@ -79,9 +79,14 @@ public class ChaosCanvas{
Vector2D indices = transformCoordsToIndices.transform(point);
int x = (int) indices.getX0();
int y = (int) indices.getX1();
if (x >= 0 && x < width && y >= 0 && y < height) {
if (canvas[x][y] != 1) { // Check if the pixel is not already set
canvas[x][y] = 1;
}
} else {
System.out.println("Indices out of bounds");
}
}
/**
* Gets the canvas array.
......
......@@ -47,7 +47,7 @@ public class ChaosGame {
ui.start();
}
private static ChaosGameDescription createAffineChaosGameDescription() {
public static ChaosGameDescription createAffineChaosGameDescription() {
List<Transform2D> transforms = new ArrayList<>();
transforms.add(new AffineTransform2D(new Matrix2x2(0.5, 0, 0, 0.5), new Vector2D(0, 0)));
transforms.add(new AffineTransform2D(new Matrix2x2(0.5, 0, 0, 0.5), new Vector2D(0.5, 0)));
......@@ -55,13 +55,4 @@ public class ChaosGame {
ChaosGameDescription description = new ChaosGameDescription(transforms, new Vector2D(0, 0), new Vector2D(1, 1));
return description;
}
private static ChaosGameDescription createJuliaChaosGameDescription() {
List<Transform2D> transforms = new ArrayList<>();
Complex julaPoint = new Complex(-0.74543, 0.11301);
int sign = -1;
transforms.add(new JuliaTransform(julaPoint, sign));
ChaosGameDescription description = new ChaosGameDescription(transforms, new Vector2D(-1.6, -1), new Vector2D(1.6, 1));
return description;
}
}
\ No newline at end of file
......@@ -32,7 +32,9 @@ public class ChaosGameFileHandler {
if (transformType.equals("Affine2D")) {
transforms.add(parseAffineTransform2D(line));
} else if (transformType.equals("Julia")) {
transforms.add(parseJuliaTransform(line));
JuliaTransform transformation = parseJuliaTransform(line);
transforms.add(transformation);
transforms.add(new JuliaTransform(transformation.getPoint(), -transformation.getSign()));
}
}
}
......
package edu.ntnu.idatt2003.mappevurderingprog2.utils;
import javafx.scene.paint.Color;
public final class Colorpalette{
public static final Color White = Color.web("#FFFFFF");
public static final Color Black = Color.web("#000000");
public static final Color Primary = Color.web("#778899");
}
\ No newline at end of file
package edu.ntnu.idatt2003.mappevurderingprog2.views.Components;
import edu.ntnu.idatt2003.mappevurderingprog2.models.chaos.ChaosGame;
import edu.ntnu.idatt2003.mappevurderingprog2.views.View;
import javafx.scene.control.Button;
import javafx.scene.control.ToolBar;
// In your MainBar class
public class MainBar extends ToolBar {
private ChaosGame chaosGame;
private View view;
public MainBar(ChaosGame chaosGame, View view){
this.chaosGame = chaosGame;
this.view = view;
Button transformButton = new Button();
transformButton.setText("Transform");
transformButton.setOnAction(event -> performTransformation());
getItems().add(transformButton);
}
private void performTransformation() {
// Run steps on the existing ChaosGame instance
chaosGame.runSteps(10000);
// Get the ChaosCanvas and draw it on your application's canvas
view.redrawCanvas(chaosGame.getCanvas());
}
}
\ No newline at end of file
package edu.ntnu.idatt2003.mappevurderingprog2.views;
import edu.ntnu.idatt2003.mappevurderingprog2.models.chaos.ChaosGame;
import edu.ntnu.idatt2003.mappevurderingprog2.models.chaos.ChaosGameDescription;
import edu.ntnu.idatt2003.mappevurderingprog2.utils.Colorpalette;
import edu.ntnu.idatt2003.mappevurderingprog2.views.Components.MainBar;
import edu.ntnu.idatt2003.mappevurderingprog2.models.chaos.ChaosCanvas;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.CornerRadii;
// In your View class
public class View extends BorderPane {
public int [][] canvas;
private Canvas affineTransformationCanvas;
private GraphicsContext gc;
public Scene createScene() {
this.setBackground(new Background(new BackgroundFill(Colorpalette.Primary, CornerRadii.EMPTY, Insets.EMPTY)));
ChaosGameDescription description = ChaosGame.createAffineChaosGameDescription();
ChaosGame chaosGame = new ChaosGame(description, 600, 600);
affineTransformationCanvas = new Canvas(600, 400);
gc = affineTransformationCanvas.getGraphicsContext2D();
MainBar mainBar = new MainBar(chaosGame, this);
this.setTop(mainBar);
this.setCenter(affineTransformationCanvas);
return new Scene(this, 900, 700);
}
public void redrawCanvas(ChaosCanvas chaosCanvas) {
int[][] newCanvas = chaosCanvas.getCanvasArray();
this.canvas = newCanvas;
// Get the width and height of the canvas
double canvasWidth = affineTransformationCanvas.getWidth() - 20;
double canvasHeight = affineTransformationCanvas.getHeight() - 20;
// Calculate the size of each pixel
double pixelWidth = canvasWidth / canvas[0].length;
double pixelHeight = canvasHeight / canvas.length;
// Loop through each pixel in the canvas array
for (int i = 0; i < canvas.length; i++) {
for (int j = 0; j < canvas[i].length; j++) {
// Calculate the coordinates of the pixel on the canvas
double x = j * pixelWidth + 3;
double y = i * pixelHeight + 3;
// Set the fill color based on the pixel value
if (canvas[i][j] == 0) {
gc.setFill(Colorpalette.White);
} else {
gc.setFill(Colorpalette.Black);
}
// Fill a rectangle representing the pixel
gc.fillRect(x, y, pixelWidth, pixelHeight);
}
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment