diff --git a/src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosCanvas.java b/src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosCanvas.java index 0b67b54882d6a3138f1f2f553df125caa00e0881..2953501a5b4f4a08ef16957b4175dd92d558e3db 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosCanvas.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosCanvas.java @@ -1,6 +1,5 @@ package edu.ntnu.stud.chaosgame.controller.game; -import edu.ntnu.stud.chaosgame.controller.PopupManager; import edu.ntnu.stud.chaosgame.model.data.Matrix2x2; import edu.ntnu.stud.chaosgame.model.data.Vector2D; import edu.ntnu.stud.chaosgame.model.transformations.AffineTransform2D; @@ -58,6 +57,7 @@ public class ChaosCanvas { this.height = height; this.minCoords = minCoords; this.maxCoords = maxCoords; + System.out.println("Min: " + minCoords.getX0() + " " + maxCoords.getX0() + ", Max: " + maxCoords.getX0()+ " " + maxCoords.getX1()); this.canvas = new int[width][height]; @@ -188,19 +188,22 @@ public class ChaosCanvas { } /** - * Updates the minimum and maximum coordinates of the canvas based on the given center coordinates and zoom level. + * Updates the minimum and maximum coordinates of the canvas based on the + * given center coordinates and zoom level. * * @param centreX the x-coordinate of the center of the subsection * @param centreY the y-coordinate of the center of the subsection * @param zoomLevel the zoom level */ - public void updateCoords(double centreX, double centreY, int zoomLevel) { + public void updateCoords(double centreX, double centreY, int zoomLevel) { // Compute the size of the subsection based on the zoom level double size = Math.pow(4, -zoomLevel); // Adjust this formula as needed // Update the minimum and maximum coordinates - minCoords = new Vector2D(centreX - 5*size / 2, centreY - 5*size / 2); - maxCoords = new Vector2D(centreX + 5*size / 2, centreY + 5*size / 2); + minCoords = new Vector2D(centreX - size / 2, centreY - size / 2); + maxCoords = new Vector2D(centreX + size / 2, centreY + size / 2); + // TODO: remove tests + System.out.println("Min: " + minCoords.getX0() + " " + maxCoords.getX0() + ", Max: " + maxCoords.getX0()+ " " + maxCoords.getX1()); // Update the affine transformation for converting coordinates to canvas indices transformCoordsToIndices = new AffineTransform2D( @@ -216,7 +219,7 @@ public class ChaosCanvas { * @param point the point to check * @return true if the point is within the subsection, false otherwise */ - public boolean isPointInSubsection(Vector2D point) { + public boolean isPointInCanvasRange(Vector2D point) { return point.getX0() >= minCoords.getX0() && point.getX0() <= maxCoords.getX0() && point.getX1() >= minCoords.getX1() && point.getX1() <= maxCoords.getX1(); } diff --git a/src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosGame.java b/src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosGame.java index 574288d6b25107690dd2b6ecfd8015f439ccf2d3..2ac835abba762c4c9f193780c3b545b8ed6c386a 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosGame.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosGame.java @@ -87,6 +87,22 @@ public class ChaosGame { */ public Vector2D getCurrentPoint() { return this.currentPoint; } + /** + * Get the random number generator for this chaos game. + * + * @return the RNG. + */ + public Random getRandom() { return this.random; } + + /** + * Get the description for this choas game. + * + * @return the description. + */ + public ChaosGameDescription getDescription() { + return this.description; + } + /** * Get the chaos game description. * @@ -144,14 +160,29 @@ public class ChaosGame { //System.out.println("Before transform: " + currentPoint.getX0() + " " + currentPoint.getX1()); Vector2D newPoint = this.description.getTransforms().get(j).transform(currentPoint); - if (this.canvas.isPointInSubsection(newPoint)) { - this.currentPoint = newPoint; - this.canvas.putPixel(currentPoint); - } else { - //System.out.println("Point out of bounds: " + newPoint.getX0() + " " + newPoint.getX1()); - } + + this.currentPoint = this.findValidPoint(newPoint); + this.canvas.putPixel(currentPoint); + } } + /** + * If the point is out of bounds, iterate until it is within bounds. + * + * @param point the starting point. + * @return the resulting valid point within bounds. + */ + public Vector2D findValidPoint(Vector2D point) { + if (!this.canvas.isPointInCanvasRange(point)) { + while (!this.canvas.isPointInCanvasRange(point)) { + int j = this.random.nextInt(this.numOfTransforms); + System.out.println("Before transform: " + point.getX0() + " " + point.getX1()); // Test + point = this.description.getTransforms().get(j).transform(currentPoint); + } + } + return point; + } + } diff --git a/src/main/java/edu/ntnu/stud/chaosgame/view/ChaosGameImageView.java b/src/main/java/edu/ntnu/stud/chaosgame/view/ChaosGameImageView.java index 918a77a18cc0481c22eb728e97d3e464da2fb65a..5b16ccf666606044a60ccb0a6d5e9bd9d839d644 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/view/ChaosGameImageView.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/view/ChaosGameImageView.java @@ -78,26 +78,9 @@ public class ChaosGameImageView extends ImageView { private synchronized void zoom(ScrollEvent event) { double newZoomFactor = event.getDeltaY() > 0 ? 1.20 : 1 / 1.05; try { - // Get the old values - double oldScaleX = transform.getMxx(); - double oldScaleY = transform.getMyy(); - double oldTranslateX = transform.getTx(); - double oldTranslateY = transform.getTy(); - - // Compute the new values - double newScaleX = oldScaleX * newZoomFactor; - double newScaleY = oldScaleY * newZoomFactor; - double newTranslateX = oldTranslateX - (event.getX() * (newScaleX - oldScaleX)); - double newTranslateY = oldTranslateY - (event.getY() * (newScaleY - oldScaleY)); - - // Update the transform - transform.setMxx(newScaleX); - transform.setMyy(newScaleY); - transform.setTx(newTranslateX); - transform.setTy(newTranslateY); - this.zoomFactor *= newZoomFactor; this.zoomLevel = (int) (Math.log(this.zoomFactor) / Math.log(4)); // Update zoom level. + System.out.println(zoomLevel); // Set the actual centre coordinates (in the Cartesian plane). These are then used to update the calculations of fractal detail. ChaosCanvas canvas = this.controller.getCanvas();