summaryrefslogtreecommitdiff
path: root/vendor/modernc.org/mathutil/envelope.go
diff options
context:
space:
mode:
authorLibravatar kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com>2021-08-29 15:41:41 +0100
committerLibravatar GitHub <noreply@github.com>2021-08-29 16:41:41 +0200
commited462245730bd7832019bd43e0bc1c9d1c055e8e (patch)
tree1caad78ea6aabf5ea93c93a8ade97176b4889500 /vendor/modernc.org/mathutil/envelope.go
parentMention fixup (#167) (diff)
downloadgotosocial-ed462245730bd7832019bd43e0bc1c9d1c055e8e.tar.xz
Add SQLite support, fix un-thread-safe DB caches, small performance f… (#172)
* Add SQLite support, fix un-thread-safe DB caches, small performance fixes Signed-off-by: kim (grufwub) <grufwub@gmail.com> * add SQLite licenses to README Signed-off-by: kim (grufwub) <grufwub@gmail.com> * appease the linter, and fix my dumbass-ery Signed-off-by: kim (grufwub) <grufwub@gmail.com> * make requested changes Signed-off-by: kim (grufwub) <grufwub@gmail.com> * add back comment Signed-off-by: kim (grufwub) <grufwub@gmail.com>
Diffstat (limited to 'vendor/modernc.org/mathutil/envelope.go')
-rw-r--r--vendor/modernc.org/mathutil/envelope.go46
1 files changed, 46 insertions, 0 deletions
diff --git a/vendor/modernc.org/mathutil/envelope.go b/vendor/modernc.org/mathutil/envelope.go
new file mode 100644
index 000000000..71980db50
--- /dev/null
+++ b/vendor/modernc.org/mathutil/envelope.go
@@ -0,0 +1,46 @@
+// Copyright (c) 2014 The mathutil Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package mathutil // import "modernc.org/mathutil"
+
+import (
+ "math"
+)
+
+// Approximation type determines approximation methods used by e.g. Envelope.
+type Approximation int
+
+// Specific approximation method tags
+const (
+ _ Approximation = iota
+ Linear // As named
+ Sinusoidal // Smooth for all derivations
+)
+
+// Envelope is an utility for defining simple curves using a small (usually)
+// set of data points. Envelope returns a value defined by x, points and
+// approximation. The value of x must be in [0,1) otherwise the result is
+// undefined or the function may panic. Points are interpreted as dividing the
+// [0,1) interval in len(points)-1 sections, so len(points) must be > 1 or the
+// function may panic. According to the left and right points closing/adjacent
+// to the section the resulting value is interpolated using the chosen
+// approximation method. Unsupported values of approximation are silently
+// interpreted as 'Linear'.
+func Envelope(x float64, points []float64, approximation Approximation) float64 {
+ step := 1 / float64(len(points)-1)
+ fslot := math.Floor(x / step)
+ mod := x - fslot*step
+ slot := int(fslot)
+ l, r := points[slot], points[slot+1]
+ rmod := mod / step
+ switch approximation {
+ case Sinusoidal:
+ k := (math.Sin(math.Pi*(rmod-0.5)) + 1) / 2
+ return l + (r-l)*k
+ case Linear:
+ fallthrough
+ default:
+ return l + (r-l)*rmod
+ }
+}