Skip to content

Instantly share code, notes, and snippets.

@edw
Created March 23, 2018 13:42
Show Gist options
  • Select an option

  • Save edw/835fa09e75e76ece08e7d32b54dff63b to your computer and use it in GitHub Desktop.

Select an option

Save edw/835fa09e75e76ece08e7d32b54dff63b to your computer and use it in GitHub Desktop.
Log scale SVG in Swift
import Foundation
func printLine(_ x: Double, with stroke: Stroke) {
let scaledX = log10(x) * 100.0
print("""
<line x1="\(scaledX)" x2="\(scaledX)" \(stroke)/>
""")
}
enum Stroke : CustomStringConvertible {
case thin, thick, constant
var description: String {
get {
switch self {
case .thin:
return """
y1="0" y2="5" stroke="black" stroke-width="0.125"
"""
case .thick:
return """
y1="0" y2="10" stroke="black" stroke-width="0.25"
"""
case .constant:
return """
y1="0" y2="15" stroke="red" stroke-width="0.125"
"""
}
}
}
}
print("""
<svg version="1.1" width="100" height="15">
""")
for i in 10...100 {
let stroke: Stroke = ((i % 10) == 0) ? .thick : .thin
let x = Double(i) * 0.1
printLine(x, with: stroke)
}
printLine(Double.pi, with: .constant)
printLine(exp(1.0), with: .constant)
print("</svg>")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment