1 package org.djutils.draw.curve;
2
3
4
5
6
7
8
9
10
11
12
13 public final class Bezier
14 {
15
16 static final double[] T =
17 new double[] {-0.0640568928626056260850430826247450385909, 0.0640568928626056260850430826247450385909,
18 -0.1911188674736163091586398207570696318404, 0.1911188674736163091586398207570696318404,
19 -0.3150426796961633743867932913198102407864, 0.3150426796961633743867932913198102407864,
20 -0.4337935076260451384870842319133497124524, 0.4337935076260451384870842319133497124524,
21 -0.5454214713888395356583756172183723700107, 0.5454214713888395356583756172183723700107,
22 -0.6480936519369755692524957869107476266696, 0.6480936519369755692524957869107476266696,
23 -0.7401241915785543642438281030999784255232, 0.7401241915785543642438281030999784255232,
24 -0.8200019859739029219539498726697452080761, 0.8200019859739029219539498726697452080761,
25 -0.8864155270044010342131543419821967550873, 0.8864155270044010342131543419821967550873,
26 -0.9382745520027327585236490017087214496548, 0.9382745520027327585236490017087214496548,
27 -0.9747285559713094981983919930081690617411, 0.9747285559713094981983919930081690617411,
28 -0.9951872199970213601799974097007368118745, 0.9951872199970213601799974097007368118745};
29
30
31 static final double[] C =
32 new double[] {0.1279381953467521569740561652246953718517, 0.1279381953467521569740561652246953718517,
33 0.1258374563468282961213753825111836887264, 0.1258374563468282961213753825111836887264,
34 0.121670472927803391204463153476262425607, 0.121670472927803391204463153476262425607,
35 0.1155056680537256013533444839067835598622, 0.1155056680537256013533444839067835598622,
36 0.1074442701159656347825773424466062227946, 0.1074442701159656347825773424466062227946,
37 0.0976186521041138882698806644642471544279, 0.0976186521041138882698806644642471544279,
38 0.086190161531953275917185202983742667185, 0.086190161531953275917185202983742667185,
39 0.0733464814110803057340336152531165181193, 0.0733464814110803057340336152531165181193,
40 0.0592985849154367807463677585001085845412, 0.0592985849154367807463677585001085845412,
41 0.0442774388174198061686027482113382288593, 0.0442774388174198061686027482113382288593,
42 0.0285313886289336631813078159518782864491, 0.0285313886289336631813078159518782864491,
43 0.0123412297999871995468056670700372915759, 0.0123412297999871995468056670700372915759};
44
45
46 public static final int DEFAULT_BEZIER_SIZE = 64;
47
48
49 private static long[] fact = new long[] {1L, 1L, 2L, 6L, 24L, 120L, 720L, 5040L, 40320L, 362880L, 3628800L, 39916800L,
50 479001600L, 6227020800L, 87178291200L, 1307674368000L, 20922789888000L, 355687428096000L, 6402373705728000L,
51 121645100408832000L, 2432902008176640000L};
52
53
54 private Bezier()
55 {
56
57 }
58
59
60
61
62
63
64
65
66
67
68 @SuppressWarnings("checkstyle:methodname")
69 static double Bn(final double t, final double... p)
70 {
71 if (p.length == 0)
72 {
73 return 0.0;
74 }
75 double b = 0.0;
76 double m = (1.0 - t);
77 int n = p.length - 1;
78 double fn = factorial(n);
79 for (int i = 0; i <= n; i++)
80 {
81 double c = fn / (factorial(i) * (factorial(n - i)));
82 b += c * Math.pow(m, n - i) * Math.pow(t, i) * p[i];
83 }
84 return b;
85 }
86
87
88
89
90
91
92 private static double factorial(final int k)
93 {
94 if (k < fact.length)
95 {
96 return fact[k];
97 }
98 double f = 1;
99 for (int i = 2; i <= k; i++)
100 {
101 f = f * i;
102 }
103 return f;
104 }
105
106
107
108
109
110
111 public static double[] derivative(final double[] in)
112 {
113 if (in.length == 0)
114 {
115 return in;
116 }
117 int n = in.length - 1;
118 double[] result = new double[n];
119 for (int i = 0; i < n; i++)
120 {
121 result[i] = n * (in[i + 1] - in[i]);
122 }
123 return result;
124 }
125
126 }