This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Data+Hexadecimal.swift | |
// | |
// The MIT License (MIT) | |
// | |
// Copyright (c) 2023 Electricwoods LLC, Kaz Yoshikawa. | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
extension UIImage { | |
convenience init(color: UIColor) { | |
let renderer = UIGraphicsImageRenderer(size: CGSize(width: 1, height: 1)) | |
let image = renderer.image { context in | |
context.cgContext.setFillColor(UIColor.blue.cgColor) | |
context.cgContext.fill(CGRect(x: 0, y: 0, width: 1, height: 1)) | |
} | |
guard let cgImage = image.cgImage else { fatalError() } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ZError.swift | |
// ZKit | |
// | |
// Created by Kaz Yoshikawa on 7/18/22. | |
// | |
// Usage: | |
// General usage for throwing Error in Swift language. | |
// | |
// Example: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Array+RunLength.swift | |
// ZKit | |
// | |
// The MIT License (MIT) | |
// | |
// Copyright (c) 2020 Electricwoods LLC, Kaz Yoshikawa. | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
public extension Array { | |
func slice(count: Int) -> [some Collection] { | |
let n = self.count / count // quotient | |
let i = n * count // index | |
let r = self.count % count // remainder | |
let slices = (0..<n).map { $0 * count }.map { self[$0 ..< $0 + count] } | |
return (r > 0) ? slices + [self[i..<i + r]] : slices | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Note: | |
// When you found that you cannot cast/convert between CGFloat and String, you may add following piece of code | |
// to make your life a bit easier. | |
// | |
// if let number = CGFloat("1234.5") { | |
// } | |
// let string = String(CGFloat.pi) | |
// | |
extension CGFloat: LosslessStringConvertible { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This sample code shows the problem of archive and unarchive abstruct objects. Please see `TODO` keyword | |
and make this Contents class to save and load shape objects. | |
CASE: All target objects are based on NSObject with NSCoding to archive and unarchive Shape desendant class objects | |
file: archive-unarchive-1.swift | |
https://gist.github.com/codelynx/428b27b3cfd58b8c7382346f1a4bc415 | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This sample code shows the problem of archive and unarchive abstruct objects. Please see `TODO` keyword | |
and make this Contents class to save and load shape objects. | |
CASE: All target objects are based on NSObject with NSCoding to archive and unarchive Shape desendant class objects | |
file: archive-unarchive-1.swift | |
https://gist.github.com/codelynx/428b27b3cfd58b8c7382346f1a4bc415 | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This sample code shows the problem of archive and unarchive abstruct objects. Please see `TODO` keyword | |
and make this Contents class to save and load shape objects. | |
*/ | |
import Foundation | |
import CoreGraphics | |
protocol Shape { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func casteljau<T: FloatingPoint>(_ v0: T, _ v1: T, _ t: T) -> T { | |
return v0 + (v1 - v0) * t | |
} | |
func casteljau<T: FloatingPoint>(_ v0: T, _ v1: T, _ v2: T, _ t: T) -> T { | |
let q0 = v0 + (v1 - v0) * t | |
let q1 = v1 + (v2 - v1) * t | |
return q0 + (q1 - q0) * t | |
} |