Skip to content
Snippets Groups Projects
Commit 395bcc4a authored by Håvard Daleng's avatar Håvard Daleng
Browse files

Attempted to implement more zoom functionality; proving difficult.

parent 769870ed
No related branches found
No related tags found
No related merge requests found
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();
}
......
......@@ -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;
}
}
......@@ -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();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment