Skip to content

Commit

Permalink
INIT: CratezianAxes class
Browse files Browse the repository at this point in the history
  • Loading branch information
itays123 committed Oct 21, 2023
1 parent c4ee3b6 commit 37bb269
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 2 deletions.
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,13 @@
"java.project.sourcePaths": [""],
"java.project.outputPath": "",
"java.project.referencedLibraries": [
"c:\\Program Files\\Java\\lib\\javafx-sdk-21\\lib\\javafx.base.jar",
"c:\\Program Files\\Java\\lib\\javafx-sdk-21\\lib\\javafx.controls.jar",
"c:\\Program Files\\Java\\lib\\javafx-sdk-21\\lib\\javafx.fxml.jar",
"c:\\Program Files\\Java\\lib\\javafx-sdk-21\\lib\\javafx.graphics.jar",
"c:\\Program Files\\Java\\lib\\javafx-sdk-21\\lib\\javafx.media.jar",
"c:\\Program Files\\Java\\lib\\javafx-sdk-21\\lib\\javafx.swing.jar",
"c:\\Program Files\\Java\\lib\\javafx-sdk-21\\lib\\javafx.web.jar",
"c:\\Program Files\\Java\\lib\\javafx-sdk-21\\lib\\javafx-swt.jar"
]
}
25 changes: 23 additions & 2 deletions App.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,32 @@
import function.arithmetics.Product;
import function.elementary.PowerFunction;
import function.trigonometric.Sine;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import plot.CartesianAxes;
import vector.MapVector;
import vector.Vector;

public class App {
public class App extends Application {
public static void main(String[] args) throws Exception {
functionTest();
launch(args);
}

@Override
public void start(Stage stage) throws Exception {
Canvas canvas = new Canvas(400, 400);
CartesianAxes axes = new CartesianAxes(-8, 8, -7, 8, canvas.getWidth(), canvas.getHeight());
StackPane layout = new StackPane(canvas);
layout.setPadding(new Insets(20));

stage.setTitle("y = \u00BC(x+4)(x+1)(x-2)");
stage.setScene(new Scene(layout));
stage.show();
axes.draw(canvas.getGraphicsContext2D());
}

public static void vectorTest() {
Expand Down Expand Up @@ -86,4 +106,5 @@ public static void functionTest() {
System.out.println(sineOfNegF);
System.out.println(sineOfNegF.derive());
}

}
95 changes: 95 additions & 0 deletions plot/CartesianAxes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package plot;

import javafx.scene.canvas.GraphicsContext;

public class CartesianAxes implements Drawable {

public static final int MIN_NUM_OF_STEPS = 9;
public static final int MAX_NUM_OF_STEPS = 17;
public static final int MARKING_SIZE_PX = 10;

private double canvasWidth, canvasHeight;
private double xAxisLocation; // the y coordinate of the x axis in the canvas
private double yAxisLocation; // the x coordinate of the y axis in the canvas
private int xScale; // the scale of the x markings
private int yScale; // the scale of the y marking
private double minX, maxX, minY, maxY;

public CartesianAxes(double minX, double maxX, double minY, double maxY, double canvasWidth, double canvasHeight) {
this.minX = Math.min(minX, 0);
this.maxX = Math.max(maxX, 0);
this.minY = Math.min(minY, 0);
this.maxY = Math.max(maxY, 0);
this.canvasWidth = canvasWidth;
this.canvasHeight = canvasHeight;
yAxisLocation = getAxisLocation(this.minX, this.maxX, canvasWidth); // the x coord of the y axis
xAxisLocation = canvasHeight - getAxisLocation(this.minY, this.maxY, canvasHeight); // the y coord of the x axis
xScale = getScale(this.minX, this.maxX);
yScale = getScale(this.minY, this.maxY);
}

private double getAxisLocation(double minValue, double maxValue, double dimensionSize) {
double negativePercentage = (-minValue) / (maxValue - minValue);
return dimensionSize * negativePercentage;
}

private int getScale(double minValue, double maxValue) {
double span = maxValue - minValue, minDifference = Double.MAX_VALUE, currentDifference;
int currentScale, minDifferenceScale = 1;
for (int currentNumOfSteps = MAX_NUM_OF_STEPS; currentNumOfSteps >= MIN_NUM_OF_STEPS; currentNumOfSteps--) {
currentScale = (int) Math.ceil(span / currentNumOfSteps);
currentDifference = Math.abs(span - currentScale * currentNumOfSteps);
if (currentDifference < minDifference) {
minDifference = currentDifference;
minDifferenceScale = currentScale;
}
}
return minDifferenceScale;
}

@Override
public void draw(GraphicsContext gc) {
// draw axes
gc.strokeLine(0, xAxisLocation, canvasWidth, xAxisLocation);
gc.strokeLine(yAxisLocation, 0, yAxisLocation, canvasHeight);

// draw x markings
for (int currentStep = xScale; currentStep <= maxX; currentStep += xScale) {
drawXMarking(gc, currentStep);
}
for (int currentX = -xScale; currentX >= minX; currentX -= xScale) {
drawXMarking(gc, currentX);
}

// drawY markings
for (int currentY = yScale; currentY <= maxY; currentY += yScale) {
drawYMarking(gc, currentY);
}
for (int currentY = -yScale; currentY >= minY; currentY -= yScale) {
drawYMarking(gc, currentY);
}
}

private void drawXMarking(GraphicsContext gc, int x) {
double xLocation = mapXCoordinate(x / xScale);
gc.strokeLine(xLocation, xAxisLocation + MARKING_SIZE_PX / 2, xLocation, xAxisLocation - MARKING_SIZE_PX / 2);
gc.strokeText(String.valueOf(x), xLocation, xAxisLocation + MARKING_SIZE_PX * 3 / 2);
}

private void drawYMarking(GraphicsContext gc, int y) {
double yLocation = mapYCoordinate(y / yScale);
gc.strokeLine(yAxisLocation + MARKING_SIZE_PX / 2, yLocation, yAxisLocation - MARKING_SIZE_PX / 2, yLocation);
gc.strokeText(String.valueOf(y), yAxisLocation - MARKING_SIZE_PX * 2, yLocation);
}

public double mapXCoordinate(double x) {
double pxPerUnit = canvasWidth / (maxX - minX);
return yAxisLocation + x * xScale * pxPerUnit;
}

public double mapYCoordinate(double y) {
double pxPerUnit = canvasHeight / (maxY - minY);
return xAxisLocation - y * yScale * pxPerUnit;
}

}
16 changes: 16 additions & 0 deletions plot/Drawable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package plot;

import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Paint;

/**
* Represents something that can be drawn on a canvas
*/
public interface Drawable {
default void draw(GraphicsContext gc, Paint p) {
gc.setStroke(p);
draw(gc);
}

public void draw(GraphicsContext gc);
}

0 comments on commit 37bb269

Please sign in to comment.