Skip to content

Commit

Permalink
switch argument order to reflect x,y
Browse files Browse the repository at this point in the history
  • Loading branch information
Bersaelor committed Jul 9, 2020
1 parent 666d057 commit 341d4d3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ class DetailViewController: UIViewController {
let function: (Double) -> Double
if arguments.count > 1 {
let spline = Spline(
values: values,
arguments: arguments,
values: values,
boundaryCondition: chosenOption == 0
? .smooth : .fixedTangentials(dAtStart: 0, dAtEnd: 0)
)
Expand Down
31 changes: 28 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,31 @@ Import the package in your *.swift file:
```swift
import SwiftSplines
```
### Example A : Dampening a signal

Make sure your data values conforom to
If you want a function that dampens a signal in the range of [0,3], like
```
f(0.1) = 0.3,
f(0.4) = 0.6,
f(1) = 1,
f(2) = 1.6
f(2.5) = 2
f'(0.1) = 0
f'(2.5) = 0
```
you could define:
```swift
// private let dampingFunction: (Double) -> Double
private let dampingFunction = Spline(
arguments: [0.1, 0.4, 1, 2, 2.5],
values: [0.3, 0.6, 1, 1.6, 2],
boundaryCondition: .fixedTangentials(dAtStart: 0, dAtEnd: 0.0)
).f
```

### Example B: Connect custom vector data

Make sure your data values conform to
```swift
public protocol DataPoint {
associatedtype Scalar: FloatingPoint & DoubleConvertable
Expand All @@ -83,16 +106,18 @@ public protocol DataPoint {
static func * (left: Scalar, right: Self) -> Self
static func + (left: Self, right: Self) -> Self
}

extension MyVector: DataPoint { ... }
```
(`Float`, `CGFloat`, `Double`, `CGPoint` conform to DataPoint as part of the package)

Then you can create your spline functions by:
```swift
let values: [CGPoint] = ...
let values: [MyVector] = ...

let spline = Spline(values: values)

func calculate(t: Double) -> CGPoint {
func calculate(t: Double) -> MyVector {
return spline.f(t: t)
}
```
Expand Down
6 changes: 3 additions & 3 deletions Sources/SwiftSplines/Spline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public struct Spline<P: DataPoint> {
/// by default they are 0 ... n
/// - boundaryCondition: the chosen `BoundaryCondition`
public init(
values: [P],
arguments: [P.Scalar]? = nil,
values: [P],
boundaryCondition: BoundaryCondition = .smooth
) {
if let arguments = arguments, arguments.count != values.count {
Expand All @@ -50,8 +50,8 @@ public struct Spline<P: DataPoint> {

let args = arguments ?? values.enumerated().map({ P.Scalar($0.0) })
self.init(
values: values,
arguments: args,
values: values,
derivatives: Self.computeDerivatives(from: values, boundaryCondition: boundaryCondition),
boundaryCondition: boundaryCondition
)
Expand All @@ -62,8 +62,8 @@ public struct Spline<P: DataPoint> {
/// - Parameter points: the control points
/// - Parameter derivatives: f'(t) at the control points
public init(
values: [P],
arguments: [P.Scalar],
values: [P],
derivatives: [P],
boundaryCondition: BoundaryCondition = .smooth
) {
Expand Down

0 comments on commit 341d4d3

Please sign in to comment.