1
0
mirror of https://github.com/flutter/samples.git synced 2025-11-09 06:18:49 +00:00
Files
samples/web/github_dataviz/lib/catmull.dart
John Ryan 3f5ab56485 Add workflow to deploy the sample index (#791)
* Add workflow to build and deploy the sample index

* update gh-pages action

* fix yaml

* create web/ directory in build

* grammar

* add ignored directories

* revert pubspec.lock files

* add job to run _tool/verify_samples.dart

* Update filipino_cuisine for Flutter 2

* remove timeflow demo.

The unnamed List constructor is now deprecated, refactoring
this code to use add() requires more knowledge about the code for
this demo.
https://dart.dev/null-safety/understanding-null-safety#no-unnamed-list-constructor

* update slide_puzzle

* ensure stable channel is used to verify

* move verify web demos action into separate yaml file - avoid running
on each flutter version.

* add on: pull_request

* update slide_puzzle

* Update gh-pages.yml

* Add copyright header
2021-05-06 10:26:15 -07:00

80 lines
2.3 KiB
Dart

import 'package:github_dataviz/mathutils.dart';
class ControlPointAndValue {
int point;
double value;
ControlPointAndValue() {
value = 0;
point = 2;
}
}
class CatmullInterpolator implements Interpolator {
List<Point2D> controlPoints;
CatmullInterpolator(this.controlPoints);
@override
double get(double v) {
for (int i = 2; i < controlPoints.length - 1; i++) {
if (controlPoints[i].x >= v) {
double t = (v - controlPoints[i - 1].x) /
(controlPoints[i].x - controlPoints[i - 1].x);
double p0 = controlPoints[i - 2].y;
double p1 = controlPoints[i - 1].y;
double p2 = controlPoints[i].y;
double p3 = controlPoints[i + 1].y;
return 0.5 *
((2 * p1) +
(p2 - p0) * t +
(2 * p0 - 5 * p1 + 4 * p2 - p3) * t * t +
(3 * p1 - p0 - 3 * p2 + p3) * t * t * t);
}
}
// Will be unreachable if the control points were set up right
return 0;
}
ControlPointAndValue progressiveGet(ControlPointAndValue cpv) {
double v = cpv.value;
for (int i = cpv.point; i < controlPoints.length - 1; i++) {
if (controlPoints[i].x >= v) {
double t = (v - controlPoints[i - 1].x) /
(controlPoints[i].x - controlPoints[i - 1].x);
double p0 = controlPoints[i - 2].y;
double p1 = controlPoints[i - 1].y;
double p2 = controlPoints[i].y;
double p3 = controlPoints[i + 1].y;
cpv.value = 0.5 *
((2 * p1) +
(p2 - p0) * t +
(2 * p0 - 5 * p1 + 4 * p2 - p3) * t * t +
(3 * p1 - p0 - 3 * p2 + p3) * t * t * t);
cpv.point = i;
return cpv;
}
}
// Will be unreachable if the control points were set up right
return cpv;
}
static void test() {
List<Point2D> controlPoints = <Point2D>[];
controlPoints.add(Point2D(-1, 1));
controlPoints.add(Point2D(0, 1));
controlPoints.add(Point2D(1, -1));
controlPoints.add(Point2D(3, 4));
controlPoints.add(Point2D(10, -2));
controlPoints.add(Point2D(11, -2));
CatmullInterpolator catmull = CatmullInterpolator(controlPoints);
print(catmull.get(0));
print(catmull.get(1));
print(catmull.get(2));
print(catmull.get(5));
print(catmull.get(7));
print(catmull.get(8));
print(catmull.get(10));
}
}