Skip to content

Commit

Permalink
Add SwiftUI Example
Browse files Browse the repository at this point in the history
  • Loading branch information
li3zhen1 committed Nov 5, 2023
1 parent 45e96e6 commit 1bafb99
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 47 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<img src="https://github.com/li3zhen1/Grape/actions/workflows/swift.yml/badge.svg" alt="swift workflow">
<a href="https://swiftpackageindex.com/li3zhen1/Grape"><img src="https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fli3zhen1%2FGrape%2Fbadge%3Ftype%3Dswift-versions" alt="swift package index"></a>
<a href="https://swiftpackageindex.com/li3zhen1/Grape"><img src="https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fli3zhen1%2FGrape%2Fbadge%3Ftype%3Dplatforms" alt="swift package index"></a>

</p>

<p align="center">A Swift library for force simulation and graph visualization.
Expand Down Expand Up @@ -83,7 +82,6 @@ struct ForceDirectedGraphSwiftUIExample: View {
for i in 0..<2 {
LinkMark(from: i, to: i+1)
}

} forceField: {
// Declare forces like you would do in D3.js.
LinkForce()
Expand Down
89 changes: 44 additions & 45 deletions Sources/ForceSimulation/Simulation2D/Simulation2D.CenterForce.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,62 @@
// Created by li3zhen1 on 10/16/23.
//


#if canImport(simd)

import simd
import simd

extension Simulation2D {
/// A force that drives nodes towards the center.
///
/// Center force is relatively fast, the complexity is `O(n)`,
/// where `n` is the number of nodes.
/// See [Collide Force - D3](https://d3js.org/d3-force/collide).
final public class CenterForce: ForceLike
where NodeID: Hashable {
public typealias V = simd_double2
extension Simulation2D {
/// A force that drives nodes towards the center.
///
/// Center force is relatively fast, the complexity is `O(n)`,
/// where `n` is the number of nodes.
/// See [Collide Force - D3](https://d3js.org/d3-force/collide).
final public class CenterForce: ForceLike
where NodeID: Hashable {
public typealias V = simd_double2

public var center: V
public var strength: V.Scalar
@usableFromInline weak var simulation: Simulation2D<NodeID>?
public var center: V
public var strength: V.Scalar
@usableFromInline weak var simulation: Simulation2D<NodeID>?

@inlinable internal init(center: V, strength: V.Scalar) {
self.center = center
self.strength = strength
}
@inlinable internal init(center: V, strength: V.Scalar) {
self.center = center
self.strength = strength
}

public func apply() {
guard let sim = self.simulation else { return }
// let alpha = sim.alpha
public func apply() {
guard let sim = self.simulation else { return }
// let alpha = sim.alpha

var meanPosition = V.zero
for n in sim.nodePositions {
meanPosition += n //.position
}
let delta = meanPosition * (self.strength / V.Scalar(sim.nodePositions.count))
var meanPosition = V.zero
for n in sim.nodePositions {
meanPosition += n //.position
}
let delta = meanPosition * (self.strength / V.Scalar(sim.nodePositions.count))

for i in sim.nodePositions.indices {
sim.nodePositions[i] -= delta
for i in sim.nodePositions.indices {
sim.nodePositions[i] -= delta
}
}

}

}
/// Create a center force that drives nodes towards the center.
///
/// Center force is relatively fast, the complexity is `O(n)`,
/// where `n` is the number of nodes.
/// See [Collide Force - D3](https://d3js.org/d3-force/collide).
/// - Parameters:
/// - center: The center of the force.
/// - strength: The strength of the force.
@discardableResult
public func createCenterForce(center: V, strength: V.Scalar = 0.1) -> CenterForce {
let f = CenterForce(center: center, strength: strength)
f.simulation = self
self.forces.append(f)
return f
}

/// Create a center force that drives nodes towards the center.
///
/// Center force is relatively fast, the complexity is `O(n)`,
/// where `n` is the number of nodes.
/// See [Collide Force - D3](https://d3js.org/d3-force/collide).
/// - Parameters:
/// - center: The center of the force.
/// - strength: The strength of the force.
@discardableResult
public func createCenterForce(center: V, strength: V.Scalar = 0.1) -> CenterForce {
let f = CenterForce(center: center, strength: strength)
f.simulation = self
self.forces.append(f)
return f
}

}

#endif

0 comments on commit 1bafb99

Please sign in to comment.