Skip to content

Instantly share code, notes, and snippets.

View heestand-xyz's full-sized avatar

Anton Heestand heestand-xyz

View GitHub Profile
var filters: [String: [CGFloat]] = [:]
func filter(_ val: CGFloat, for count: Int, as id: String) -> CGFloat {
if var filter = filters[id] {
filter.insert(val, at: 0)
let lastVal: CGFloat
if filter.count > count {
lastVal = filter.popLast()!
} else {
lastVal = filter.last!
}
@heestand-xyz
heestand-xyz / range.swift
Last active October 24, 2018 12:40
Range
func range(_ val: CGFloat, low: CGFloat, high: CGFloat, clamp: Bool = true, toLow: CGFloat = 0.0, toHigh: CGFloat = 1.0) -> CGFloat {
var norm = (val - low) / (high - low)
if clamp { norm = min(max(norm, 0.0), 1.0) }
return toLow + norm * (toHigh - toLow)
}
@heestand-xyz
heestand-xyz / circle3.swift
Last active October 23, 2023 07:40
Circle Center from 3 Points
func circleCenter(_ a: CGPoint, _ b: CGPoint, _ c: CGPoint) -> CGPoint {
let dya = b.y - a.y
let dxa = b.x - a.x
let dyb = c.y - b.y
let dxb = c.x - b.x
let sa = dya / dxa
let sb = dyb / dxb
let x = (sa * sb * (a.y - c.y) + sb * (a.x + b.x) - sa * (b.x + c.x)) / (2 * (sb - sa))
let y = -1 * (x - (a.x + b.x) / 2) / sa + (a.y + b.y) / 2
return CGPoint(x: x, y: y)
var allTouches: [UITouch] = []
func add(_ touches: Set<UITouch>) -> [UITouch] {
for newTouch in touches {
var exist = false
for oldTouch in allTouches {
if oldTouch == newTouch {
exist = true
break
}
}
@heestand-xyz
heestand-xyz / animate.swift
Last active October 17, 2018 08:45
Animate
func animate(for duration: CGFloat, loop: @escaping (CGFloat) -> (), done: @escaping () -> ()) {
let startTime = Date()
RunLoop.current.add(Timer(timeInterval: 1.0 / Double(UIScreen.main.maximumFramesPerSecond), repeats: true, block: { t in
let elapsedTime = CGFloat(-startTime.timeIntervalSinceNow)
let fraction = min(elapsedTime / duration, 1.0)
loop(fraction)
if fraction == 1.0 {
done()
t.invalidate()
}
@heestand-xyz
heestand-xyz / GIF2MP4.swift
Last active October 23, 2018 19:42 — forked from powhu/GIF2MP4.swift
Swift 4.2 GIF to MP4
// GIF2MP4.swift
//
// Created by PowHu Yang on 2017/1/24.
// Copyright © 2017 PowHu Yang. All rights reserved.
//
import UIKit
import Foundation
import AVFoundation
@heestand-xyz
heestand-xyz / animate-with-ease.swift
Last active February 5, 2019 10:11
Animate with Ease
enum AnimationEase {
case linear
case easeIn
case easeInOut
case easeOut
}
func animate(for duration: CGFloat, ease: AnimationEase = .linear, loop: @escaping (CGFloat) -> (), done: @escaping () -> ()) {
let startTime = Date()
RunLoop.current.add(Timer(timeInterval: 1.0 / Double(UIScreen.main.maximumFramesPerSecond), repeats: true, block: { t in
let elapsedTime = CGFloat(-startTime.timeIntervalSinceNow)
@heestand-xyz
heestand-xyz / simple-filter.swift
Created February 18, 2019 09:43
Simple Filter
var filterCache: [CGFloat] = []
func filter(_ value: CGFloat, for seconds: CGFloat) -> CGFloat {
filterCache.append(value)
guard filterCache.count > 1 else { return value }
let frameCount = Int(seconds * CGFloat(UIScreen.main.maximumFramesPerSecond))
if filterCache.count > frameCount {
filterCache.remove(at: 0)
}
var filtered: CGFloat = 0.0
for val in filterCache {
@heestand-xyz
heestand-xyz / multi-filter.swift
Last active April 30, 2019 07:47
Multi Filter
var filterCaches: [String: [CGFloat]] = [:]
func filter(_ value: CGFloat, for seconds: CGFloat, smooth: Bool = true, id: String) -> CGFloat {
guard filterCaches[id] != nil else {
filterCaches[id] = [value]
return value
}
filterCaches[id]!.append(value)
let frameCount = Int(seconds * CGFloat(60))
if filterCaches[id]!.count > frameCount {
filterCaches[id]!.remove(at: 0)
@heestand-xyz
heestand-xyz / pixels-rays.swift
Created February 26, 2019 08:32
Pixels - Rays
let rampPix = GradientPIX(res: .fullscreen)
rampPix.style = .angle
rampPix.colorSteps[1].color = .black
rampPix.colorSteps.append(ColorStep(.liveWave(for: 24.0), .white))
let anglePix = rampPix._quantize(by: 1 / 8)._edge(100)
let flipXPix = anglePix + anglePix._flipX()
let flipYPix = flipXPix + flipXPix._flipY()
let rampExtraPix = GradientPIX(res: .fullscreen)