aboutsummaryrefslogtreecommitdiff
path: root/chess
diff options
context:
space:
mode:
Diffstat (limited to 'chess')
-rw-r--r--chess/bezier.scad85
-rw-r--r--chess/demo.scad32
2 files changed, 85 insertions, 32 deletions
diff --git a/chess/bezier.scad b/chess/bezier.scad
new file mode 100644
index 0000000..4a75cb6
--- /dev/null
+++ b/chess/bezier.scad
@@ -0,0 +1,85 @@
+// https://youtu.be/tOx5UI8GGns
+
+$fn = 8;
+
+function bezier_3(p0, p1, p2, t) =
+ (( 1 - t) ^ 2) * p0
+ + 2 * (1 - t) * t * p1
+ + (t ^ 2) * p2;
+
+function bezier_4(p0, p1, p2, p3, t) =
+ (( 1 - t) ^ 3) * p0
+ + 3 * ((1 - t) ^ 2) * t * p1
+ + 3 * (1 - t) * (t ^ 2) * p2
+ + (t ^ 3) * p3;
+
+function sum(array, index = 0) =
+ index < len(array) - 1 ?
+ array[index] + sum(array, index + 1) :
+ array[index] ;
+
+function factorial(x) =
+ x > 1 ?
+ x * factorial(x - 1) :
+ 1 ;
+
+function binomial_coefficient(degree, index) =
+ factorial(degree) / (factorial(index) * factorial(degree - index)) ;
+
+function bezier_p0(point, degree, index, t) =
+ binomial_coefficient(degree, index) * ((1 - t) ^ (degree - index)) * (t ^ index) * point;
+
+function bezier_p(points, t) =
+ [for (i=[1:len(points[0])]) sum(bezier_p0(points[i], len(points) - 1, i, t))];
+
+function bezier_p_debug(points, t) =
+ [for (i=[1:len(points[0])]) sum(bezier_p0(points[i], len(points) - 1, i, t))];
+
+p0 = [0, 0];
+p1 = [10, 0];
+pb = [1, 5];
+pn = [0, 10];
+
+
+points = $fn;
+
+points_list = [for (i=[0:points]) bezier_3(p0, p1, pn, i/points)];
+
+//rotate_extrude(angle = 360)
+ polygon(points_list);
+
+points_list_2 = [for (i=[0:points]) bezier_4(p0, p1, pb, pn, i/points)];
+
+translate([20, 0, 0])
+ //rotate_extrude(angle = 360)
+ polygon(points_list_2);
+
+p_set_1 = [p0, p1, pn];
+p_set_2 = [p0, p1, pb, pn];
+
+points_list_p1 = [for (i=[0:$fn]) bezier_p(p_set_1, i/$fn)];
+points_list_p2 = [for (i=[0:$fn]) bezier_p(p_set_2, i/$fn)];
+points_list_p2d = [for (i=[0:$fn]) bezier_p_debug(p_set_2, i/$fn)];
+
+for (i=[0:$fn]) {
+ echo("i", i, bezier_p(p_set_2, i/$fn));
+ echo("i", i, bezier_p_debug(p_set_2, i/$fn));
+ for (j=[1:len(p_set_2[0])]) {
+ echo("j", j, sum(bezier_p0(p_set_2[j], len(p_set_2[j]), j, i/$fn)));
+ echo("j", j, bezier_p0(p_set_2[j], len(p_set_2[j]), j, i/$fn));
+ }
+}
+
+//echo(points_list);
+//echo(points_list_p1);
+echo(points_list_2);
+echo(points_list_p2d);
+echo(points_list_p2);
+
+translate([0, 20, 0])
+ //rotate_extrude(angle = 360)
+ polygon(points_list_p1);
+
+translate([20, 20, 0])
+ //rotate_extrude(angle = 360)
+ polygon(points_list_p2d);
diff --git a/chess/demo.scad b/chess/demo.scad
deleted file mode 100644
index 341da35..0000000
--- a/chess/demo.scad
+++ /dev/null
@@ -1,32 +0,0 @@
-// https://youtu.be/tOx5UI8GGns
-
-$fn = 8;
-
-function bezier_3(p0, p1, p2, t) =
- (( 1 - t) ^ 2) * p0
- + 2 * (1 - t) * t * p1
- + (t ^ 2) * p2;
-
-function bezier_4(p0, p1, p2, p3, t) =
- (( 1 - t) ^ 3) * p0
- + 3 * ((1 - t) ^ 2) * t * p1
- + 3 * (1 - t) * (t ^ 2) * p2
- + (t ^ 3) * p3;
-
-p0 = [0, 0];
-p1 = [10, 0];
-p2 = [0, 10];
-p3 = [0, 5];
-
-points = $fn;
-
-points_list = [for (i=[0:points]) bezier_3(p0, p1, p2, i/points)];
-
-rotate_extrude(angle = 360)
- polygon(points_list);
-
-points_list_2 = [for (i=[0:points]) bezier_4(p0, p1, p3, p2, i/points)];
-
-translate([20, 0, 0])
- rotate_extrude(angle = 360)
- polygon(points_list_2);