From ce34801fde29173f1d719d69737ac93bc6c94eaa Mon Sep 17 00:00:00 2001 From: Ines Zhou Date: Tue, 30 Apr 2024 18:01:12 +0800 Subject: [PATCH] Add Hilbert curve example --- .../12MoreProjects/HilbertCurve/.gitignore | 8 ++ .../12MoreProjects/HilbertCurve/Package.mmp | 17 +++ .../12MoreProjects/HilbertCurve/Package.swift | 30 +++++ .../HilbertCurve/Sources/Hilbert.swift | 59 +++++++++ .../HilbertCurve/Sources/HilbertCurve.swift | 116 ++++++++++++++++++ 5 files changed, 230 insertions(+) create mode 100644 Examples/SwiftIOPlayground/12MoreProjects/HilbertCurve/.gitignore create mode 100644 Examples/SwiftIOPlayground/12MoreProjects/HilbertCurve/Package.mmp create mode 100644 Examples/SwiftIOPlayground/12MoreProjects/HilbertCurve/Package.swift create mode 100644 Examples/SwiftIOPlayground/12MoreProjects/HilbertCurve/Sources/Hilbert.swift create mode 100644 Examples/SwiftIOPlayground/12MoreProjects/HilbertCurve/Sources/HilbertCurve.swift diff --git a/Examples/SwiftIOPlayground/12MoreProjects/HilbertCurve/.gitignore b/Examples/SwiftIOPlayground/12MoreProjects/HilbertCurve/.gitignore new file mode 100644 index 0000000..0023a53 --- /dev/null +++ b/Examples/SwiftIOPlayground/12MoreProjects/HilbertCurve/.gitignore @@ -0,0 +1,8 @@ +.DS_Store +/.build +/Packages +xcuserdata/ +DerivedData/ +.swiftpm/configuration/registries.json +.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata +.netrc diff --git a/Examples/SwiftIOPlayground/12MoreProjects/HilbertCurve/Package.mmp b/Examples/SwiftIOPlayground/12MoreProjects/HilbertCurve/Package.mmp new file mode 100644 index 0000000..102d0e2 --- /dev/null +++ b/Examples/SwiftIOPlayground/12MoreProjects/HilbertCurve/Package.mmp @@ -0,0 +1,17 @@ +# This is a MadMachine project file in TOML format +# This file holds those parameters that could not be managed by SwiftPM +# Edit this file would change the behavior of the building/downloading procedure +# Those project files in the dependent libraries would be IGNORED + +# Specify the board name below +# There are "SwiftIOBoard" and "SwiftIOMicro" now +board = "SwiftIOMicro" + +# Specifiy the target triple below +# There are "thumbv7em-unknown-none-eabi" and "thumbv7em-unknown-none-eabihf" now +# If your code use significant floating-point calculation, +# plz set it to "thumbv7em-unknown-none-eabihf" +triple = "thumbv7em-unknown-none-eabi" + +# Reserved for future use +version = 1 diff --git a/Examples/SwiftIOPlayground/12MoreProjects/HilbertCurve/Package.swift b/Examples/SwiftIOPlayground/12MoreProjects/HilbertCurve/Package.swift new file mode 100644 index 0000000..c572614 --- /dev/null +++ b/Examples/SwiftIOPlayground/12MoreProjects/HilbertCurve/Package.swift @@ -0,0 +1,30 @@ +// swift-tools-version: 5.9 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "HilbertCurve", + dependencies: [ + // Dependencies declare other packages that this package depends on. + .package(url: "https://github.com/madmachineio/SwiftIO.git", branch: "main"), + .package(url: "https://github.com/madmachineio/MadBoards.git", branch: "main"), + .package(url: "https://github.com/madmachineio/MadDrivers.git", branch: "main"), + .package(url: "https://github.com/madmachineio/MadGraphics.git", branch: "main"), + .package(url: "https://github.com/apple/swift-numerics", from: "1.0.0"), + ], + targets: [ + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. + .executableTarget( + name: "HilbertCurve", + dependencies: [ + "SwiftIO", + "MadBoards", + // Use specific library name rather than "MadDrivers" would speed up the build procedure. + .product(name: "MadDrivers", package: "MadDrivers"), + "MadGraphics", + .product(name: "RealModule", package: "swift-numerics"), + ]), + ] +) diff --git a/Examples/SwiftIOPlayground/12MoreProjects/HilbertCurve/Sources/Hilbert.swift b/Examples/SwiftIOPlayground/12MoreProjects/HilbertCurve/Sources/Hilbert.swift new file mode 100644 index 0000000..091771c --- /dev/null +++ b/Examples/SwiftIOPlayground/12MoreProjects/HilbertCurve/Sources/Hilbert.swift @@ -0,0 +1,59 @@ +import MadGraphics + +struct Hilbert { + // The order of the Hilbert curve. + let order: Int + // The points of the Hilbert curve. + var points = [Point]() + + let size: Int + let total: Int + + // The four corners of a unit square. + let unitSquarePoints = [Point(0, 0), Point(0, 1), Point(1, 1), Point(1, 0)] + + // Generate a Hilbert curve with a specified order. + init(order: Int, canvas: Canvas) { + self.order = order + + // Total number of points in the curve. + size = Int(Float.pow(2, order)) + total = size * size + for i in 0.. Point { + var i = i + // One of the four corners of the unit square, which serves as the initial point. + var point = unitSquarePoints[i % 4] + + // Update the point iteratively. + for j in 1..