File src/data/Vector.java changed (mode: 100644) (index c9845d2..ef90de1) |
1 |
1 |
package data; |
package data; |
2 |
2 |
|
|
3 |
|
import java.util.List; |
|
|
3 |
|
public class Vector { |
4 |
4 |
|
|
5 |
|
public class Vector{ |
|
|
5 |
|
private double[] vector; |
6 |
6 |
|
|
7 |
|
private int[] vector; |
|
8 |
|
|
|
9 |
|
public Vector(int[] vector){ |
|
10 |
|
this.vector = vector; |
|
|
7 |
|
public Vector(double... val){ |
|
8 |
|
vector=val; |
11 |
9 |
} |
} |
12 |
10 |
|
|
13 |
|
public void multi(int a){ |
|
14 |
|
for(int i=0; i<vector.length; i++){ |
|
15 |
|
vector[i]= vector[i]*a; |
|
|
11 |
|
public Vector multi(int a) { |
|
12 |
|
double[] newVal = new double[vector.length]; |
|
13 |
|
for (int i = 0; i < vector.length; i++) { |
|
14 |
|
newVal[i] = vector[i] * a; |
16 |
15 |
} |
} |
|
16 |
|
return new Vector(newVal); |
17 |
17 |
} |
} |
18 |
18 |
|
|
19 |
|
public void add(Vector point){ |
|
20 |
|
int[] pointVal=point.getValues(); |
|
21 |
|
for(int i=0; i<pointVal.length; i++){ |
|
22 |
|
vector[i]= vector[i]+pointVal[i]; |
|
|
19 |
|
public Vector add(Vector point) { |
|
20 |
|
double[] newVal = new double[vector.length]; |
|
21 |
|
double[] pointVal = point.getValues(); |
|
22 |
|
for (int i = 0; i < pointVal.length; i++) { |
|
23 |
|
newVal[i] = vector[i] + pointVal[i]; |
23 |
24 |
} |
} |
|
25 |
|
return new Vector(newVal); |
24 |
26 |
} |
} |
25 |
27 |
|
|
26 |
|
public void matrixMulti(float[][] matrix){ |
|
27 |
|
|
|
28 |
|
|
|
29 |
|
|
|
|
28 |
|
public Vector matrixMulti(double[][] matrix) { |
|
29 |
|
if (matrix[0].length == vector.length) { |
|
30 |
|
double[] newVal = new double[matrix[0].length]; |
|
31 |
|
for (int i = 0; i < matrix.length; i++) { |
|
32 |
|
for (int j = 0; j < vector.length; j++) { |
|
33 |
|
newVal[i] += vector[j] * matrix[i][j]; |
|
34 |
|
} |
|
35 |
|
} |
|
36 |
|
return new Vector(newVal); |
|
37 |
|
}else{ |
|
38 |
|
throw new IllegalArgumentException("Number of columns of the matrix must match the number of coordinates of the Vector."); |
|
39 |
|
} |
30 |
40 |
} |
} |
31 |
|
public int[] getValues(){ |
|
|
41 |
|
|
|
42 |
|
private double[] getValues() { |
32 |
43 |
return vector; |
return vector; |
33 |
44 |
} |
} |
34 |
45 |
|
|
35 |
|
private List<Vector> splitMatrix(float[][] matrix){ |
|
36 |
|
|
|
|
46 |
|
public double get(int index){ |
|
47 |
|
return vector[index]; |
37 |
48 |
} |
} |
38 |
49 |
} |
} |
File src/ui/Controller.java changed (mode: 100644) (index f1b0869..ff94bbe) |
1 |
1 |
package ui; |
package ui; |
2 |
2 |
|
|
|
3 |
|
import data.OwnMath; |
|
4 |
|
import data.Vector; |
3 |
5 |
import javafx.fxml.FXML; |
import javafx.fxml.FXML; |
4 |
6 |
import javafx.scene.canvas.Canvas; |
import javafx.scene.canvas.Canvas; |
5 |
7 |
import javafx.scene.canvas.GraphicsContext; |
import javafx.scene.canvas.GraphicsContext; |
6 |
|
|
|
7 |
|
import data.Vector; |
|
8 |
8 |
import javafx.scene.control.CheckBox; |
import javafx.scene.control.CheckBox; |
9 |
9 |
import javafx.scene.control.Slider; |
import javafx.scene.control.Slider; |
10 |
10 |
import javafx.scene.paint.Color; |
import javafx.scene.paint.Color; |
11 |
|
import javafx.scene.paint.Paint; |
|
|
11 |
|
|
|
12 |
|
import java.util.ArrayList; |
|
13 |
|
import java.util.List; |
12 |
14 |
|
|
13 |
15 |
public class Controller { |
public class Controller { |
14 |
|
private int WIDTH,HEIGHT; |
|
|
16 |
|
private int WIDTH,HEIGHT,POINTSIZE =10; |
|
17 |
|
|
|
18 |
|
private List<Vector> points=new ArrayList<>(); |
|
19 |
|
private Vector origin; |
|
20 |
|
|
15 |
21 |
private GraphicsContext gc; |
private GraphicsContext gc; |
|
22 |
|
private double angle=0; |
|
23 |
|
|
16 |
24 |
@FXML |
@FXML |
17 |
25 |
private Canvas canvasDraw; |
private Canvas canvasDraw; |
18 |
26 |
@FXML |
@FXML |
|
... |
... |
public class Controller { |
25 |
33 |
gc = canvasDraw.getGraphicsContext2D(); |
gc = canvasDraw.getGraphicsContext2D(); |
26 |
34 |
WIDTH=(int)Math.round(canvasDraw.getWidth()); |
WIDTH=(int)Math.round(canvasDraw.getWidth()); |
27 |
35 |
HEIGHT=(int)Math.round(canvasDraw.getHeight()); |
HEIGHT=(int)Math.round(canvasDraw.getHeight()); |
|
36 |
|
|
|
37 |
|
points.add(new Vector(-50,-50,-50)); |
|
38 |
|
points.add(new Vector(-50,50,-50)); |
|
39 |
|
points.add(new Vector(50,-50,-50)); |
|
40 |
|
points.add(new Vector(50,50,-50)); |
|
41 |
|
points.add(new Vector(-50,-50,50)); |
|
42 |
|
points.add(new Vector(-50,50,50)); |
|
43 |
|
points.add(new Vector(50,-50,50)); |
|
44 |
|
points.add(new Vector(50,50,50)); |
|
45 |
|
|
|
46 |
|
this.origin=new Vector(WIDTH/2,HEIGHT/2,0.0); |
28 |
47 |
} |
} |
29 |
48 |
|
|
30 |
49 |
public void simulate(){ |
public void simulate(){ |
|
50 |
|
//draw background |
31 |
51 |
gc.setFill(Color.GRAY); |
gc.setFill(Color.GRAY); |
32 |
|
gc.fillRect(0, 0, 600, 600); |
|
|
52 |
|
gc.fillRect(0, 0, WIDTH, HEIGHT); |
|
53 |
|
gc.strokeOval(WIDTH/2,HEIGHT/2,POINTSIZE,POINTSIZE); |
33 |
54 |
gc.setFill(Color.WHITE); |
gc.setFill(Color.WHITE); |
34 |
|
gc.fillOval(WIDTH / 2, HEIGHT / 2, 10, 10); |
|
35 |
|
|
|
|
55 |
|
//draw object |
|
56 |
|
angle+=0.01; |
|
57 |
|
if(angle>(2*Math.PI)) |
|
58 |
|
angle=0; |
|
59 |
|
if(!points.isEmpty()){ |
|
60 |
|
for (Vector v : |
|
61 |
|
points) { |
|
62 |
|
v = v.matrixMulti(OwnMath.getRotationMatrix(angle,1)); |
|
63 |
|
drawPoint(v.add(origin)); |
|
64 |
|
} |
|
65 |
|
} |
36 |
66 |
} |
} |
37 |
67 |
|
|
38 |
68 |
private void drawPoint( Vector v){ |
private void drawPoint( Vector v){ |
39 |
|
gc.fillOval(v.get(0),v.get(1),5,5); |
|
|
69 |
|
gc.fillOval(v.get(0),v.get(1),POINTSIZE,POINTSIZE); |
40 |
70 |
} |
} |
41 |
71 |
private void connect( Vector v1 , Vector v2){ |
private void connect( Vector v1 , Vector v2){ |
42 |
72 |
gc.strokeLine(v1.get(0),v1.get(1),v2.get(0),v2.get(1)); |
gc.strokeLine(v1.get(0),v1.get(1),v2.get(0),v2.get(1)); |