-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFunctionCurve.java
More file actions
56 lines (49 loc) · 1.68 KB
/
FunctionCurve.java
File metadata and controls
56 lines (49 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* This class represents a curve in a mathematical function.
*/
import curve.Curve;
import curve.boundable.Boundable;
import function.Function;
import javafx.geometry.Point2D;
import javafx.scene.paint.Paint;
/**
* Represents evaluation that is done based on a rule
*/
public class FunctionCurve extends Curve implements Boundable {
public static final double EVALUATION_INTERVAL = 0.01;
public static final double JUMP_TRESHOLD = 2000; // the maximal amount of units that can be jumped in one x inteval
private Function function;
/**
* Constructs a new function path
*
* @param func the function to draw
* @param startX the start value of the function
* @param endX the end value of the function *
* @param paint the painting of the curve
*/
public FunctionCurve(Function func, double startX, double endX, Paint paint) {
super(paint);
this.function = func;
evaluate(startX, endX);
}
@Override
public void setBounds(double startX, double endX) {
clear();
evaluate(startX, endX);
}
private void evaluate(double startX, double endX) {
Function derivative = function.derive();
double y, dy;
for (double x = startX; x <= endX; x += EVALUATION_INTERVAL) {
try {
y = function.evaluate(x);
dy = derivative.evaluate(x) * EVALUATION_INTERVAL;
if (Math.abs(dy) >= JUMP_TRESHOLD)
throw new ArithmeticException("Jump is too big!");
points.add(new Point2D(x, y));
} catch (ArithmeticException e) {
points.add(null);
}
}
}
}