Skip to content

Instantly share code, notes, and snippets.

View theoknock's full-sized avatar

James Alan Bush theoknock

View GitHub Profile
@theoknock
theoknock / ContentView.swift
Last active June 5, 2024 10:03
"Circular distributions can be used even when the variables concerned are not explicitly angles: the main consideration is that there is not usually any real distinction between events occurring at the opposite ends of the range, and the division of the range could notionally be made at any point." https://en.wikipedia.org/wiki/Circular_distribu…
import Foundation
import SwiftUI
import Combine
import Observation
struct ContentView: View {
@State private var randoms: LatticeCircularDistributor = LatticeCircularDistributor(boundLower: 0.3125, boundUpper: 0.3125, threshholdLeft: 0.25, threshholdRight: 0.25)
var body: some View {
Spacer()
VStack {
@theoknock
theoknock / CircularLatticeDistribution.swift
Created May 27, 2024 01:05
A circular distribution or polar distribution is a probability distribution of a random variable whose values are angles, usually taken to be in the range [0, 2π). A circular distribution is often a continuous probability distribution, and hence has a probability density, but such distributions can also be discrete, in which case they are called…
/*
A circular distribution or polar distribution is a probability distribution of a random variable whose values are angles, usually taken to be in the range [0, 2π). A circular distribution is often a continuous probability distribution, and hence has a probability density, but such distributions can also be discrete, in which case they are called circular lattice distributions. Circular distributions can be used even when the variables concerned are not explicitly angles: the main consideration is that there is not usually any real distinction between events occurring at the opposite ends of the range, and the division of the range could notionally be made at any point.
*/
// https://en.wikipedia.org/wiki/Circular_distribution
import SwiftUI
func scale(oldMin: Double, oldMax: Double, value: Double, newMin: Double, newMax: Double) -> Double {
return ((value - oldMin) / (oldMax - oldMin)) * (newMax - newMin) + newMin
@theoknock
theoknock / vDSP_LinearizeArray.swift
Created May 27, 2024 00:21
Uses vDSP to simplify and accelerate linearization of arrays
let n = vDSP_Length(88200)
let stride = vDSP_Stride(1)
var a: Float32 = 0.0
var b: Float32 = 1.0
var c = [Float32](repeating: 0,
count: Int(vDSP_Length(88200)))
vDSP_vgen(&a,
&b,
@theoknock
theoknock / ContentView.swift
Last active May 25, 2024 05:30
A start to a typography style guide layout tool that lets you experiment with various aspects of typography (fonts, sizes, spacing, alignment, and other typographic elements) for branding and product design, ensuring that all textual content remains consistent and aligns with the overall aesthetic and communication goals of your brand or organiz…
import SwiftUI
struct ContentView: View {
@State private var selectedFontName: String = "Times New Roman"
@State private var isPresented: Bool = true
var body: some View {
VStack {
FontSelectorView(selectedFontName: $selectedFontName)
@theoknock
theoknock / AngularRandomDistributor.swift
Last active May 27, 2024 00:50
Generates two random values within the same range, ensuring the second value lies outside the tolerance range of the first.
import SwiftUI
func scale(oldMin: Double, oldMax: Double, value: Double, newMin: Double, newMax: Double) -> Double {
return ((value - oldMin) / (oldMax - oldMin)) * (newMax - newMin) + newMin
}
struct ContentView: View {
@State private var firstAngles: [Double] = []
@State private var secondAngles: [Double] = []
@State private var minusThresholdAngles: [Double] = []
@theoknock
theoknock / ExecutingMetalComputeShadersView.swift
Last active May 19, 2024 21:04
Executing Metal Compute Shaders: A barebones SwiftUI app that executes Metal Compute shaders and returns the output to a SwiftUI view for display.
import MetalKit
class MetalSineWaveGenerator {
let device: MTLDevice
let commandQueue: MTLCommandQueue
let computePipelineState: MTLComputePipelineState
let arraySize: Int
let frequency: Float
let sampleRate: Float
let resultBuffer: MTLBuffer
@theoknock
theoknock / DispatchGroup.swift
Last active May 16, 2024 19:46
Using a serial and global dispatch queue to synchronize calls to functions that should be executed one after another, preventing overlap.
import Foundation
public func randomizeDurationSplits(group: DispatchGroup? = nil, completion: @escaping ([[Double]]) -> Void) {
group?.enter()
DispatchQueue.global().async {
let dyad0harmony0: Double = Double.random(in: durationLowerBound ... durationUpperBound)
var dyad1harmony0: Double = dyad0harmony0
repeat {
@theoknock
theoknock / ConcentricCircularArrayView.swift
Last active May 10, 2024 09:35
A SwiftUI view consisting of a specified number of Circle() views, each with a diameter matching the standard Slider() "thumb" image (30 points), arranged in a perfect circle with an adjustable radius calculated to space them equidistantly, incorporating additional spacing between each circle.
import SwiftUI
struct CircularArrayView: View {
private let diameter: CGFloat = 30
var numberOfCircles: Int = 12
var spacing: CGFloat = 1.0
// Ensures the number of circles is always even
private var evenNumberOfCircles: Int {
numberOfCircles % 2 == 0 ? numberOfCircles : numberOfCircles + 1
@theoknock
theoknock / SimulateColorOpacity.swift
Created May 6, 2024 03:00
Simulates a color at opacity < 1.0 when placed over a black or white background
func hueAccentColor(angle: CGFloat) -> Color {
return Color(hue: CGFloat(angle / 360.0), saturation: 1.0, brightness: 1.0, opacity: 1.0)
}
func whiteColor() -> Color {
return Color.init(uiColor: .white) // return Color(hue: CGFloat(hueAngle / 360.0), saturation: 0.0, brightness: 1.0, opacity: 1.0)
}
func blackColor() -> Color {
return Color.init(uiColor: .black) // return Color(hue: CGFloat(hueAngle / 360.0), saturation: 0.0, brightness: 0.0, opacity: 1.0)
@theoknock
theoknock / Hue Circular Slider Control
Created May 2, 2024 08:47
A circular slider that sets the hue
import SwiftUI
struct ContentView: View {
var body: some View {
HueControlView()
}
}
struct HueControlView: View {
@State var hueAngle: CGFloat = 0.0