├── .gitignore ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── Images └── sim_demo.gif ├── README.md ├── SwiftUIFourierSeries.xcodeproj ├── project.pbxproj └── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ └── IDEWorkspaceChecks.plist └── SwiftUIFourierSeries ├── App ├── AppDelegate.swift ├── Assets.xcassets │ ├── AppIcon.appiconset │ │ └── Contents.json │ └── Contents.json ├── Base.lproj │ └── LaunchScreen.storyboard ├── Info.plist └── SceneDelegate.swift ├── Model ├── AppState.swift ├── FourierSeries.swift ├── GraphModel.swift ├── Store.swift └── Timer.swift ├── Preview Content └── Preview Assets.xcassets │ └── Contents.json ├── SwiftUIFourierSeries.entitlements └── Views ├── ButtonsView.swift ├── ConfigView.swift ├── EpicircleView.swift ├── EpicirclesView.swift ├── GraphView.swift ├── PickerView.swift └── SeriesView.swift /.gitignore: -------------------------------------------------------------------------------- 1 | xcuserdata 2 | .DS_Store -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | ## Code of Conduct 2 | 3 | ### Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, gender identity and expression, level of experience, 9 | nationality, personal appearance, race, religion, or sexual identity and 10 | orientation. 11 | 12 | ### Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | When contributing to this repository, please first discuss the change you wish to make via issue or any other method with the owner of this repository before making a change. 4 | 5 | Please note we have a code of conduct, please follow it in all your interactions with the project. 6 | 7 | ## Pull Request Process 8 | 9 | 1. Ensure any install or build dependencies are removed before the end of the layer when doing a 10 | build. 11 | 2. Add details of changes in comments section of the Pull Request using this [template](https://github.com/embeddedartistry/templates/blob/master/oss_docs/PULL_REQUEST_TEMPLATE.md); this includes new environment 12 | variables, exposed ports, useful file locations and container parameters. 13 | 3. The Pull Request will be reviewed and approved by the owner of this repository 14 | -------------------------------------------------------------------------------- /Images/sim_demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/simonboots/SwiftUIFourierSeries/7ed44691599c6571ae64d7a9f134c944e3031499/Images/sim_demo.gif -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Fourier Series Visualisation in SwiftUI 2 | 3 | This little app visualises different [Fourier series](https://en.wikipedia.org/wiki/Fourier_series) using epicycles and a graph. 4 | 5 | ![Simulator demo](https://github.com/simonboots/SwiftUIFourierSeries/raw/master/Images/sim_demo.gif) 6 | 7 | The number of functions and the type of series can be changed. The following series are supported: 8 | 9 | * Square Wave 10 | * Saw Tooth 11 | * Triangle 12 | * Pulse 13 | 14 | It builds and runs on iOS and macOS using Catalyst. 15 | 16 | I started this little project to familiarise myself a bit more with SwiftUI and its drawing APIs. It is not meant to be perfect SwiftUI and I am sure there are probably better ways of doing things. Any feedback is appreciated! 17 | -------------------------------------------------------------------------------- /SwiftUIFourierSeries.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 50; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 0B2B5F9E23703300001DE7E0 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B2B5F9D23703300001DE7E0 /* AppDelegate.swift */; }; 11 | 0B2B5FA023703300001DE7E0 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B2B5F9F23703300001DE7E0 /* SceneDelegate.swift */; }; 12 | 0B2B5FA423703300001DE7E0 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 0B2B5FA323703300001DE7E0 /* Assets.xcassets */; }; 13 | 0B2B5FA723703300001DE7E0 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 0B2B5FA623703300001DE7E0 /* Preview Assets.xcassets */; }; 14 | 0B2B5FAA23703300001DE7E0 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 0B2B5FA823703300001DE7E0 /* LaunchScreen.storyboard */; }; 15 | 0B2B5FB823703475001DE7E0 /* ConfigView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B2B5FB323703475001DE7E0 /* ConfigView.swift */; }; 16 | 0B2B5FB923703475001DE7E0 /* EpicirclesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B2B5FB423703475001DE7E0 /* EpicirclesView.swift */; }; 17 | 0B2B5FBA23703475001DE7E0 /* EpicircleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B2B5FB523703475001DE7E0 /* EpicircleView.swift */; }; 18 | 0B2B5FBB23703475001DE7E0 /* GraphView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B2B5FB623703475001DE7E0 /* GraphView.swift */; }; 19 | 0B2B5FBC23703475001DE7E0 /* SeriesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B2B5FB723703475001DE7E0 /* SeriesView.swift */; }; 20 | 0B2B5FC323703490001DE7E0 /* AppState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B2B5FBE2370348F001DE7E0 /* AppState.swift */; }; 21 | 0B2B5FC423703490001DE7E0 /* Store.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B2B5FBF2370348F001DE7E0 /* Store.swift */; }; 22 | 0B2B5FC523703490001DE7E0 /* GraphModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B2B5FC023703490001DE7E0 /* GraphModel.swift */; }; 23 | 0B2B5FC623703490001DE7E0 /* Timer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B2B5FC123703490001DE7E0 /* Timer.swift */; }; 24 | 0B2B5FC723703490001DE7E0 /* FourierSeries.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B2B5FC223703490001DE7E0 /* FourierSeries.swift */; }; 25 | 733A092825DDBD1200CE61BF /* ButtonsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 733A092725DDBD1200CE61BF /* ButtonsView.swift */; }; 26 | 733A092C25DDBED500CE61BF /* PickerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 733A092B25DDBED500CE61BF /* PickerView.swift */; }; 27 | /* End PBXBuildFile section */ 28 | 29 | /* Begin PBXFileReference section */ 30 | 0B2B5F9A23703300001DE7E0 /* SwiftUIFourierSeries.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SwiftUIFourierSeries.app; sourceTree = BUILT_PRODUCTS_DIR; }; 31 | 0B2B5F9D23703300001DE7E0 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 32 | 0B2B5F9F23703300001DE7E0 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; }; 33 | 0B2B5FA323703300001DE7E0 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 34 | 0B2B5FA623703300001DE7E0 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = ""; }; 35 | 0B2B5FA923703300001DE7E0 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 36 | 0B2B5FAB23703300001DE7E0 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 37 | 0B2B5FB323703475001DE7E0 /* ConfigView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConfigView.swift; sourceTree = ""; }; 38 | 0B2B5FB423703475001DE7E0 /* EpicirclesView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EpicirclesView.swift; sourceTree = ""; }; 39 | 0B2B5FB523703475001DE7E0 /* EpicircleView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EpicircleView.swift; sourceTree = ""; }; 40 | 0B2B5FB623703475001DE7E0 /* GraphView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GraphView.swift; sourceTree = ""; }; 41 | 0B2B5FB723703475001DE7E0 /* SeriesView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SeriesView.swift; sourceTree = ""; }; 42 | 0B2B5FBE2370348F001DE7E0 /* AppState.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppState.swift; sourceTree = ""; }; 43 | 0B2B5FBF2370348F001DE7E0 /* Store.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Store.swift; sourceTree = ""; }; 44 | 0B2B5FC023703490001DE7E0 /* GraphModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GraphModel.swift; sourceTree = ""; }; 45 | 0B2B5FC123703490001DE7E0 /* Timer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Timer.swift; sourceTree = ""; }; 46 | 0B2B5FC223703490001DE7E0 /* FourierSeries.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FourierSeries.swift; sourceTree = ""; }; 47 | 0B2B5FC823703554001DE7E0 /* SwiftUIFourierSeries.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = SwiftUIFourierSeries.entitlements; sourceTree = ""; }; 48 | 733A092725DDBD1200CE61BF /* ButtonsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ButtonsView.swift; sourceTree = ""; }; 49 | 733A092B25DDBED500CE61BF /* PickerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PickerView.swift; sourceTree = ""; }; 50 | /* End PBXFileReference section */ 51 | 52 | /* Begin PBXFrameworksBuildPhase section */ 53 | 0B2B5F9723703300001DE7E0 /* Frameworks */ = { 54 | isa = PBXFrameworksBuildPhase; 55 | buildActionMask = 2147483647; 56 | files = ( 57 | ); 58 | runOnlyForDeploymentPostprocessing = 0; 59 | }; 60 | /* End PBXFrameworksBuildPhase section */ 61 | 62 | /* Begin PBXGroup section */ 63 | 0B2B5F9123703300001DE7E0 = { 64 | isa = PBXGroup; 65 | children = ( 66 | 0B2B5F9C23703300001DE7E0 /* SwiftUIFourierSeries */, 67 | 0B2B5F9B23703300001DE7E0 /* Products */, 68 | ); 69 | sourceTree = ""; 70 | }; 71 | 0B2B5F9B23703300001DE7E0 /* Products */ = { 72 | isa = PBXGroup; 73 | children = ( 74 | 0B2B5F9A23703300001DE7E0 /* SwiftUIFourierSeries.app */, 75 | ); 76 | name = Products; 77 | sourceTree = ""; 78 | }; 79 | 0B2B5F9C23703300001DE7E0 /* SwiftUIFourierSeries */ = { 80 | isa = PBXGroup; 81 | children = ( 82 | 0B2B5FC823703554001DE7E0 /* SwiftUIFourierSeries.entitlements */, 83 | 0B2B5FB12370341E001DE7E0 /* App */, 84 | 0B2B5FBD2370347B001DE7E0 /* Model */, 85 | 0B2B5FB22370345D001DE7E0 /* Views */, 86 | 0B2B5FA523703300001DE7E0 /* Preview Content */, 87 | ); 88 | path = SwiftUIFourierSeries; 89 | sourceTree = ""; 90 | }; 91 | 0B2B5FA523703300001DE7E0 /* Preview Content */ = { 92 | isa = PBXGroup; 93 | children = ( 94 | 0B2B5FA623703300001DE7E0 /* Preview Assets.xcassets */, 95 | ); 96 | path = "Preview Content"; 97 | sourceTree = ""; 98 | }; 99 | 0B2B5FB12370341E001DE7E0 /* App */ = { 100 | isa = PBXGroup; 101 | children = ( 102 | 0B2B5F9D23703300001DE7E0 /* AppDelegate.swift */, 103 | 0B2B5F9F23703300001DE7E0 /* SceneDelegate.swift */, 104 | 0B2B5FA323703300001DE7E0 /* Assets.xcassets */, 105 | 0B2B5FA823703300001DE7E0 /* LaunchScreen.storyboard */, 106 | 0B2B5FAB23703300001DE7E0 /* Info.plist */, 107 | ); 108 | path = App; 109 | sourceTree = ""; 110 | }; 111 | 0B2B5FB22370345D001DE7E0 /* Views */ = { 112 | isa = PBXGroup; 113 | children = ( 114 | 733A092725DDBD1200CE61BF /* ButtonsView.swift */, 115 | 733A092B25DDBED500CE61BF /* PickerView.swift */, 116 | 0B2B5FB323703475001DE7E0 /* ConfigView.swift */, 117 | 0B2B5FB423703475001DE7E0 /* EpicirclesView.swift */, 118 | 0B2B5FB523703475001DE7E0 /* EpicircleView.swift */, 119 | 0B2B5FB623703475001DE7E0 /* GraphView.swift */, 120 | 0B2B5FB723703475001DE7E0 /* SeriesView.swift */, 121 | ); 122 | path = Views; 123 | sourceTree = ""; 124 | }; 125 | 0B2B5FBD2370347B001DE7E0 /* Model */ = { 126 | isa = PBXGroup; 127 | children = ( 128 | 0B2B5FBE2370348F001DE7E0 /* AppState.swift */, 129 | 0B2B5FC223703490001DE7E0 /* FourierSeries.swift */, 130 | 0B2B5FC023703490001DE7E0 /* GraphModel.swift */, 131 | 0B2B5FBF2370348F001DE7E0 /* Store.swift */, 132 | 0B2B5FC123703490001DE7E0 /* Timer.swift */, 133 | ); 134 | path = Model; 135 | sourceTree = ""; 136 | }; 137 | /* End PBXGroup section */ 138 | 139 | /* Begin PBXNativeTarget section */ 140 | 0B2B5F9923703300001DE7E0 /* SwiftUIFourierSeries */ = { 141 | isa = PBXNativeTarget; 142 | buildConfigurationList = 0B2B5FAE23703300001DE7E0 /* Build configuration list for PBXNativeTarget "SwiftUIFourierSeries" */; 143 | buildPhases = ( 144 | 0B2B5F9623703300001DE7E0 /* Sources */, 145 | 0B2B5F9723703300001DE7E0 /* Frameworks */, 146 | 0B2B5F9823703300001DE7E0 /* Resources */, 147 | ); 148 | buildRules = ( 149 | ); 150 | dependencies = ( 151 | ); 152 | name = SwiftUIFourierSeries; 153 | productName = SwiftUIFourierSeries; 154 | productReference = 0B2B5F9A23703300001DE7E0 /* SwiftUIFourierSeries.app */; 155 | productType = "com.apple.product-type.application"; 156 | }; 157 | /* End PBXNativeTarget section */ 158 | 159 | /* Begin PBXProject section */ 160 | 0B2B5F9223703300001DE7E0 /* Project object */ = { 161 | isa = PBXProject; 162 | attributes = { 163 | LastSwiftUpdateCheck = 1120; 164 | LastUpgradeCheck = 1120; 165 | ORGANIZATIONNAME = "Simon Stiefel"; 166 | TargetAttributes = { 167 | 0B2B5F9923703300001DE7E0 = { 168 | CreatedOnToolsVersion = 11.2; 169 | }; 170 | }; 171 | }; 172 | buildConfigurationList = 0B2B5F9523703300001DE7E0 /* Build configuration list for PBXProject "SwiftUIFourierSeries" */; 173 | compatibilityVersion = "Xcode 9.3"; 174 | developmentRegion = en; 175 | hasScannedForEncodings = 0; 176 | knownRegions = ( 177 | en, 178 | Base, 179 | ); 180 | mainGroup = 0B2B5F9123703300001DE7E0; 181 | productRefGroup = 0B2B5F9B23703300001DE7E0 /* Products */; 182 | projectDirPath = ""; 183 | projectRoot = ""; 184 | targets = ( 185 | 0B2B5F9923703300001DE7E0 /* SwiftUIFourierSeries */, 186 | ); 187 | }; 188 | /* End PBXProject section */ 189 | 190 | /* Begin PBXResourcesBuildPhase section */ 191 | 0B2B5F9823703300001DE7E0 /* Resources */ = { 192 | isa = PBXResourcesBuildPhase; 193 | buildActionMask = 2147483647; 194 | files = ( 195 | 0B2B5FAA23703300001DE7E0 /* LaunchScreen.storyboard in Resources */, 196 | 0B2B5FA723703300001DE7E0 /* Preview Assets.xcassets in Resources */, 197 | 0B2B5FA423703300001DE7E0 /* Assets.xcassets in Resources */, 198 | ); 199 | runOnlyForDeploymentPostprocessing = 0; 200 | }; 201 | /* End PBXResourcesBuildPhase section */ 202 | 203 | /* Begin PBXSourcesBuildPhase section */ 204 | 0B2B5F9623703300001DE7E0 /* Sources */ = { 205 | isa = PBXSourcesBuildPhase; 206 | buildActionMask = 2147483647; 207 | files = ( 208 | 0B2B5FB823703475001DE7E0 /* ConfigView.swift in Sources */, 209 | 0B2B5FC723703490001DE7E0 /* FourierSeries.swift in Sources */, 210 | 0B2B5FB923703475001DE7E0 /* EpicirclesView.swift in Sources */, 211 | 0B2B5F9E23703300001DE7E0 /* AppDelegate.swift in Sources */, 212 | 0B2B5FC423703490001DE7E0 /* Store.swift in Sources */, 213 | 0B2B5FC523703490001DE7E0 /* GraphModel.swift in Sources */, 214 | 733A092825DDBD1200CE61BF /* ButtonsView.swift in Sources */, 215 | 0B2B5FC323703490001DE7E0 /* AppState.swift in Sources */, 216 | 0B2B5FBA23703475001DE7E0 /* EpicircleView.swift in Sources */, 217 | 0B2B5FA023703300001DE7E0 /* SceneDelegate.swift in Sources */, 218 | 0B2B5FBB23703475001DE7E0 /* GraphView.swift in Sources */, 219 | 0B2B5FC623703490001DE7E0 /* Timer.swift in Sources */, 220 | 733A092C25DDBED500CE61BF /* PickerView.swift in Sources */, 221 | 0B2B5FBC23703475001DE7E0 /* SeriesView.swift in Sources */, 222 | ); 223 | runOnlyForDeploymentPostprocessing = 0; 224 | }; 225 | /* End PBXSourcesBuildPhase section */ 226 | 227 | /* Begin PBXVariantGroup section */ 228 | 0B2B5FA823703300001DE7E0 /* LaunchScreen.storyboard */ = { 229 | isa = PBXVariantGroup; 230 | children = ( 231 | 0B2B5FA923703300001DE7E0 /* Base */, 232 | ); 233 | name = LaunchScreen.storyboard; 234 | sourceTree = ""; 235 | }; 236 | /* End PBXVariantGroup section */ 237 | 238 | /* Begin XCBuildConfiguration section */ 239 | 0B2B5FAC23703300001DE7E0 /* Debug */ = { 240 | isa = XCBuildConfiguration; 241 | buildSettings = { 242 | ALWAYS_SEARCH_USER_PATHS = NO; 243 | CLANG_ANALYZER_NONNULL = YES; 244 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 245 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 246 | CLANG_CXX_LIBRARY = "libc++"; 247 | CLANG_ENABLE_MODULES = YES; 248 | CLANG_ENABLE_OBJC_ARC = YES; 249 | CLANG_ENABLE_OBJC_WEAK = YES; 250 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 251 | CLANG_WARN_BOOL_CONVERSION = YES; 252 | CLANG_WARN_COMMA = YES; 253 | CLANG_WARN_CONSTANT_CONVERSION = YES; 254 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 255 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 256 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 257 | CLANG_WARN_EMPTY_BODY = YES; 258 | CLANG_WARN_ENUM_CONVERSION = YES; 259 | CLANG_WARN_INFINITE_RECURSION = YES; 260 | CLANG_WARN_INT_CONVERSION = YES; 261 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 262 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 263 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 264 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 265 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 266 | CLANG_WARN_STRICT_PROTOTYPES = YES; 267 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 268 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 269 | CLANG_WARN_UNREACHABLE_CODE = YES; 270 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 271 | COPY_PHASE_STRIP = NO; 272 | DEBUG_INFORMATION_FORMAT = dwarf; 273 | ENABLE_STRICT_OBJC_MSGSEND = YES; 274 | ENABLE_TESTABILITY = YES; 275 | GCC_C_LANGUAGE_STANDARD = gnu11; 276 | GCC_DYNAMIC_NO_PIC = NO; 277 | GCC_NO_COMMON_BLOCKS = YES; 278 | GCC_OPTIMIZATION_LEVEL = 0; 279 | GCC_PREPROCESSOR_DEFINITIONS = ( 280 | "DEBUG=1", 281 | "$(inherited)", 282 | ); 283 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 284 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 285 | GCC_WARN_UNDECLARED_SELECTOR = YES; 286 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 287 | GCC_WARN_UNUSED_FUNCTION = YES; 288 | GCC_WARN_UNUSED_VARIABLE = YES; 289 | IPHONEOS_DEPLOYMENT_TARGET = 13.2; 290 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 291 | MTL_FAST_MATH = YES; 292 | ONLY_ACTIVE_ARCH = YES; 293 | SDKROOT = iphoneos; 294 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 295 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 296 | }; 297 | name = Debug; 298 | }; 299 | 0B2B5FAD23703300001DE7E0 /* Release */ = { 300 | isa = XCBuildConfiguration; 301 | buildSettings = { 302 | ALWAYS_SEARCH_USER_PATHS = NO; 303 | CLANG_ANALYZER_NONNULL = YES; 304 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 305 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 306 | CLANG_CXX_LIBRARY = "libc++"; 307 | CLANG_ENABLE_MODULES = YES; 308 | CLANG_ENABLE_OBJC_ARC = YES; 309 | CLANG_ENABLE_OBJC_WEAK = YES; 310 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 311 | CLANG_WARN_BOOL_CONVERSION = YES; 312 | CLANG_WARN_COMMA = YES; 313 | CLANG_WARN_CONSTANT_CONVERSION = YES; 314 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 315 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 316 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 317 | CLANG_WARN_EMPTY_BODY = YES; 318 | CLANG_WARN_ENUM_CONVERSION = YES; 319 | CLANG_WARN_INFINITE_RECURSION = YES; 320 | CLANG_WARN_INT_CONVERSION = YES; 321 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 322 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 323 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 324 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 325 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 326 | CLANG_WARN_STRICT_PROTOTYPES = YES; 327 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 328 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 329 | CLANG_WARN_UNREACHABLE_CODE = YES; 330 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 331 | COPY_PHASE_STRIP = NO; 332 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 333 | ENABLE_NS_ASSERTIONS = NO; 334 | ENABLE_STRICT_OBJC_MSGSEND = YES; 335 | GCC_C_LANGUAGE_STANDARD = gnu11; 336 | GCC_NO_COMMON_BLOCKS = YES; 337 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 338 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 339 | GCC_WARN_UNDECLARED_SELECTOR = YES; 340 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 341 | GCC_WARN_UNUSED_FUNCTION = YES; 342 | GCC_WARN_UNUSED_VARIABLE = YES; 343 | IPHONEOS_DEPLOYMENT_TARGET = 13.2; 344 | MTL_ENABLE_DEBUG_INFO = NO; 345 | MTL_FAST_MATH = YES; 346 | SDKROOT = iphoneos; 347 | SWIFT_COMPILATION_MODE = wholemodule; 348 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 349 | VALIDATE_PRODUCT = YES; 350 | }; 351 | name = Release; 352 | }; 353 | 0B2B5FAF23703300001DE7E0 /* Debug */ = { 354 | isa = XCBuildConfiguration; 355 | buildSettings = { 356 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 357 | CODE_SIGN_ENTITLEMENTS = SwiftUIFourierSeries/SwiftUIFourierSeries.entitlements; 358 | "CODE_SIGN_IDENTITY[sdk=macosx*]" = "-"; 359 | CODE_SIGN_STYLE = Automatic; 360 | DERIVE_MACCATALYST_PRODUCT_BUNDLE_IDENTIFIER = YES; 361 | DEVELOPMENT_ASSET_PATHS = "\"SwiftUIFourierSeries/Preview Content\""; 362 | ENABLE_PREVIEWS = YES; 363 | INFOPLIST_FILE = SwiftUIFourierSeries/App/Info.plist; 364 | LD_RUNPATH_SEARCH_PATHS = ( 365 | "$(inherited)", 366 | "@executable_path/Frameworks", 367 | ); 368 | PRODUCT_BUNDLE_IDENTIFIER = com.simonboots.SwiftUIFourierSeries; 369 | PRODUCT_NAME = "$(TARGET_NAME)"; 370 | SUPPORTS_MACCATALYST = YES; 371 | SWIFT_VERSION = 5.0; 372 | TARGETED_DEVICE_FAMILY = "1,2"; 373 | }; 374 | name = Debug; 375 | }; 376 | 0B2B5FB023703300001DE7E0 /* Release */ = { 377 | isa = XCBuildConfiguration; 378 | buildSettings = { 379 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 380 | CODE_SIGN_ENTITLEMENTS = SwiftUIFourierSeries/SwiftUIFourierSeries.entitlements; 381 | "CODE_SIGN_IDENTITY[sdk=macosx*]" = "-"; 382 | CODE_SIGN_STYLE = Automatic; 383 | DERIVE_MACCATALYST_PRODUCT_BUNDLE_IDENTIFIER = YES; 384 | DEVELOPMENT_ASSET_PATHS = "\"SwiftUIFourierSeries/Preview Content\""; 385 | ENABLE_PREVIEWS = YES; 386 | INFOPLIST_FILE = SwiftUIFourierSeries/App/Info.plist; 387 | LD_RUNPATH_SEARCH_PATHS = ( 388 | "$(inherited)", 389 | "@executable_path/Frameworks", 390 | ); 391 | PRODUCT_BUNDLE_IDENTIFIER = com.simonboots.SwiftUIFourierSeries; 392 | PRODUCT_NAME = "$(TARGET_NAME)"; 393 | SUPPORTS_MACCATALYST = YES; 394 | SWIFT_VERSION = 5.0; 395 | TARGETED_DEVICE_FAMILY = "1,2"; 396 | }; 397 | name = Release; 398 | }; 399 | /* End XCBuildConfiguration section */ 400 | 401 | /* Begin XCConfigurationList section */ 402 | 0B2B5F9523703300001DE7E0 /* Build configuration list for PBXProject "SwiftUIFourierSeries" */ = { 403 | isa = XCConfigurationList; 404 | buildConfigurations = ( 405 | 0B2B5FAC23703300001DE7E0 /* Debug */, 406 | 0B2B5FAD23703300001DE7E0 /* Release */, 407 | ); 408 | defaultConfigurationIsVisible = 0; 409 | defaultConfigurationName = Release; 410 | }; 411 | 0B2B5FAE23703300001DE7E0 /* Build configuration list for PBXNativeTarget "SwiftUIFourierSeries" */ = { 412 | isa = XCConfigurationList; 413 | buildConfigurations = ( 414 | 0B2B5FAF23703300001DE7E0 /* Debug */, 415 | 0B2B5FB023703300001DE7E0 /* Release */, 416 | ); 417 | defaultConfigurationIsVisible = 0; 418 | defaultConfigurationName = Release; 419 | }; 420 | /* End XCConfigurationList section */ 421 | }; 422 | rootObject = 0B2B5F9223703300001DE7E0 /* Project object */; 423 | } 424 | -------------------------------------------------------------------------------- /SwiftUIFourierSeries.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /SwiftUIFourierSeries.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /SwiftUIFourierSeries/App/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // SwiftUIFourierSeries 4 | // 5 | // Created by Simon Stiefel on 4/11/19. 6 | // Copyright © 2019 Simon Stiefel. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | 15 | 16 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 17 | // Override point for customization after application launch. 18 | return true 19 | } 20 | 21 | // MARK: UISceneSession Lifecycle 22 | 23 | func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { 24 | // Called when a new scene session is being created. 25 | // Use this method to select a configuration to create the new scene with. 26 | return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) 27 | } 28 | 29 | func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set) { 30 | // Called when the user discards a scene session. 31 | // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions. 32 | // Use this method to release any resources that were specific to the discarded scenes, as they will not return. 33 | } 34 | 35 | 36 | } 37 | 38 | -------------------------------------------------------------------------------- /SwiftUIFourierSeries/App/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "size" : "1024x1024", 91 | "scale" : "1x" 92 | } 93 | ], 94 | "info" : { 95 | "version" : 1, 96 | "author" : "xcode" 97 | } 98 | } -------------------------------------------------------------------------------- /SwiftUIFourierSeries/App/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /SwiftUIFourierSeries/App/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /SwiftUIFourierSeries/App/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | $(PRODUCT_BUNDLE_PACKAGE_TYPE) 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UIApplicationSceneManifest 24 | 25 | UIApplicationSupportsMultipleScenes 26 | 27 | UISceneConfigurations 28 | 29 | UIWindowSceneSessionRoleApplication 30 | 31 | 32 | UISceneConfigurationName 33 | Default Configuration 34 | UISceneDelegateClassName 35 | $(PRODUCT_MODULE_NAME).SceneDelegate 36 | 37 | 38 | 39 | 40 | UILaunchStoryboardName 41 | LaunchScreen 42 | UIRequiredDeviceCapabilities 43 | 44 | armv7 45 | 46 | UISupportedInterfaceOrientations 47 | 48 | UIInterfaceOrientationPortrait 49 | UIInterfaceOrientationLandscapeLeft 50 | UIInterfaceOrientationLandscapeRight 51 | 52 | UISupportedInterfaceOrientations~ipad 53 | 54 | UIInterfaceOrientationPortrait 55 | UIInterfaceOrientationPortraitUpsideDown 56 | UIInterfaceOrientationLandscapeLeft 57 | UIInterfaceOrientationLandscapeRight 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /SwiftUIFourierSeries/App/SceneDelegate.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | import SwiftUI 3 | import Combine 4 | 5 | 6 | class SceneDelegate: UIResponder, UIWindowSceneDelegate { 7 | 8 | var window: UIWindow? 9 | 10 | func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { 11 | // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`. 12 | // If using a storyboard, the `window` property will automatically be initialized and attached to the scene. 13 | // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead). 14 | 15 | // Create the SwiftUI view that provides the window contents. 16 | let timer = Timer(frequency: 0.25) 17 | let store = Store(kind: .squareWave, numOfFunctions: 1, timer: timer) 18 | let contentView = SeriesView(store: store) 19 | 20 | // Use a UIHostingController as window root view controller. 21 | if let windowScene = scene as? UIWindowScene { 22 | let window = UIWindow(windowScene: windowScene) 23 | window.rootViewController = UIHostingController(rootView: contentView) 24 | self.window = window 25 | window.makeKeyAndVisible() 26 | } 27 | } 28 | 29 | func sceneDidDisconnect(_ scene: UIScene) { 30 | // Called as the scene is being released by the system. 31 | // This occurs shortly after the scene enters the background, or when its session is discarded. 32 | // Release any resources associated with this scene that can be re-created the next time the scene connects. 33 | // The scene may re-connect later, as its session was not neccessarily discarded (see `application:didDiscardSceneSessions` instead). 34 | } 35 | 36 | func sceneDidBecomeActive(_ scene: UIScene) { 37 | // Called when the scene has moved from an inactive state to an active state. 38 | // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive. 39 | } 40 | 41 | func sceneWillResignActive(_ scene: UIScene) { 42 | // Called when the scene will move from an active state to an inactive state. 43 | // This may occur due to temporary interruptions (ex. an incoming phone call). 44 | } 45 | 46 | func sceneWillEnterForeground(_ scene: UIScene) { 47 | // Called as the scene transitions from the background to the foreground. 48 | // Use this method to undo the changes made on entering the background. 49 | } 50 | 51 | func sceneDidEnterBackground(_ scene: UIScene) { 52 | // Called as the scene transitions from the foreground to the background. 53 | // Use this method to save data, release shared resources, and store enough scene-specific state information 54 | // to restore the scene back to its current state. 55 | } 56 | 57 | 58 | } 59 | 60 | -------------------------------------------------------------------------------- /SwiftUIFourierSeries/Model/AppState.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | struct AppState { 4 | var time: TimeInterval 5 | var timerIsRunning: Bool 6 | var numOfFunctions: Int 7 | var seriesKind: FourierSeries.Kind 8 | var series: FourierSeries 9 | var graph: GraphModel 10 | } 11 | -------------------------------------------------------------------------------- /SwiftUIFourierSeries/Model/FourierSeries.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | import CoreGraphics 3 | import Combine 4 | 5 | struct FourierSeries { 6 | 7 | // MARK: - Types 8 | 9 | struct Function { 10 | var amplitude: CGFloat 11 | var frequency: CGFloat 12 | var phase: CGFloat 13 | 14 | func point(at time: TimeInterval) -> CGPoint { 15 | let x = amplitude * cos(CGFloat(time) * frequency + phase) 16 | let y = amplitude * -sin(CGFloat(time) * frequency + phase) 17 | 18 | return CGPoint(x: x, y: y) 19 | } 20 | } 21 | 22 | // MARK: - Properties 23 | 24 | let functions: [Function] 25 | 26 | // MARK: - Life cycle 27 | 28 | init(functions: [Function]) { 29 | self.functions = functions 30 | } 31 | 32 | func absolutePoint(at time: TimeInterval, forFunctionAt index: Int) -> CGPoint { 33 | return functions.prefix(index).reduce(CGPoint(x: 0.0, y: 0.0)) { (result, function) -> CGPoint in 34 | return CGPoint(x: result.x + function.point(at: time).x, y: result.y + function.point(at: time).y) 35 | } 36 | } 37 | 38 | func absolutePointForSeries(at time: TimeInterval) -> CGPoint { 39 | return absolutePoint(at: time, forFunctionAt: functions.count) 40 | } 41 | } 42 | 43 | extension FourierSeries { 44 | 45 | enum Kind: String, CaseIterable { 46 | case squareWave = "Square Wave" 47 | case sawTooth = "Saw Tooth" 48 | case triangle = "Triangle" 49 | case pulse = "Pulse" 50 | } 51 | 52 | static func make(_ kind: Kind, numberOfFunctions: Int, scale: CGFloat) -> FourierSeries { 53 | let f: (Int, CGFloat) -> FourierSeries 54 | switch kind { 55 | case .squareWave: 56 | f = squareWave 57 | case .sawTooth: 58 | f = sawTooth 59 | case .triangle: 60 | f = triangle 61 | case .pulse: 62 | f = pulse 63 | } 64 | 65 | return f(numberOfFunctions, scale) 66 | } 67 | 68 | static func squareWave(numberOfFunctions: Int, scale: CGFloat = 1.0) -> FourierSeries { 69 | let functions = (0.. Function in 70 | let i = CGFloat(index+1) 71 | 72 | let amplitude = 1 / (2 * i - 1) 73 | let frequency = 2 * i - 1 74 | return Function(amplitude: scale * amplitude, frequency: frequency, phase: 0.0) 75 | } 76 | 77 | return FourierSeries(functions: functions) 78 | } 79 | 80 | static func sawTooth(numberOfFunctions: Int, scale: CGFloat = 1.0) -> FourierSeries { 81 | let functions = (0.. Function in 82 | let i = CGFloat(index+1) 83 | let sign = CGFloat((index % 2 == 0 ? -1 : 1)) 84 | 85 | let amplitude = sign / i 86 | let frequency = i 87 | return Function(amplitude: scale * amplitude, frequency: frequency, phase: 0.0) 88 | } 89 | 90 | return FourierSeries(functions: functions) 91 | } 92 | 93 | static func triangle(numberOfFunctions: Int, scale: CGFloat = 1.0) -> FourierSeries { 94 | let functions = (0.. Function in 95 | let i = CGFloat(index+1) 96 | let sign = CGFloat((index % 2 == 0 ? 1 : -1)) 97 | 98 | let amplitude = sign / ((2 * i - 1) * (2 * i - 1)) 99 | let frequency = 2 * i - 1 100 | return Function(amplitude: scale * amplitude, frequency: frequency, phase: 0.0) 101 | } 102 | 103 | return FourierSeries(functions: functions) 104 | } 105 | 106 | static func pulse(numberOfFunctions: Int, scale: CGFloat = 1.0) -> FourierSeries { 107 | let functions = (0.. Function in 108 | let i = CGFloat(index+1) 109 | 110 | let amplitude = CGFloat(0.1) 111 | let frequency = i 112 | return Function(amplitude: scale * amplitude, frequency: frequency, phase: 0.0) 113 | } 114 | 115 | return FourierSeries(functions: functions) 116 | } 117 | } 118 | 119 | extension CGPoint { 120 | var asCGSize: CGSize { 121 | CGSize(width: x, height: y) 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /SwiftUIFourierSeries/Model/GraphModel.swift: -------------------------------------------------------------------------------- 1 | final class GraphModel { 2 | 3 | // MARK: - Properties 4 | 5 | private let maxLength: Int 6 | private(set) var ys = [Double]() 7 | 8 | // MARK: - Life cycle 9 | 10 | init(maxLength: Int) { 11 | self.maxLength = maxLength 12 | } 13 | 14 | // MARK: - Methods 15 | 16 | func add(_ y: Double) { 17 | ys.append(y) 18 | if ys.count > maxLength { 19 | ys = ys.suffix(maxLength) 20 | } 21 | } 22 | 23 | func removeAll() { 24 | ys.removeAll() 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /SwiftUIFourierSeries/Model/Store.swift: -------------------------------------------------------------------------------- 1 | import Combine 2 | import CoreGraphics 3 | import Foundation 4 | 5 | final class Store: ObservableObject { 6 | 7 | // MARK: - Types 8 | 9 | enum Action { 10 | case updateTime(Double) 11 | case changeKind(FourierSeries.Kind) 12 | case incrementnumOfFunctions 13 | case decrementnumOfFunctions 14 | case pause 15 | case play 16 | case reset 17 | } 18 | 19 | private enum Constants { 20 | static let scale: CGFloat = 50.0 21 | } 22 | 23 | // MARK: - Properties 24 | 25 | @Published var state: AppState 26 | private let timer: Timer 27 | private var cancellable: Cancellable? 28 | 29 | // MARK: - Life cycle 30 | 31 | init(kind: FourierSeries.Kind, numOfFunctions: Int, timer: Timer) { 32 | self.timer = timer 33 | timer.start() 34 | self.state = AppState( 35 | time: timer.time, 36 | timerIsRunning: timer.isRunning, 37 | numOfFunctions: numOfFunctions, 38 | seriesKind: kind, 39 | series: FourierSeries.make(kind, numberOfFunctions: numOfFunctions, scale: Constants.scale), 40 | graph: GraphModel(maxLength: 1000) 41 | ) 42 | 43 | cancellable = timer.objectWillChange.sink(receiveValue: { (time) in 44 | self.send(.updateTime(time)) 45 | }) 46 | } 47 | 48 | func send(_ action: Action) { 49 | Store.reduce(state: &state, timer: timer, action: action) 50 | } 51 | 52 | static private func reduce(state: inout AppState, timer: Timer, action: Action) { 53 | switch action { 54 | case .updateTime(let time): 55 | state.graph.add(Double(state.series.absolutePointForSeries(at: time).y)) 56 | state.time = time 57 | case .changeKind(let kind): 58 | state.series = FourierSeries.make(kind, numberOfFunctions: state.numOfFunctions, scale: Constants.scale) 59 | state.seriesKind = kind 60 | case .incrementnumOfFunctions: 61 | let newNumberOfFunctions = state.numOfFunctions + 1 62 | state.series = FourierSeries.make(state.seriesKind, numberOfFunctions: newNumberOfFunctions, scale: Constants.scale) 63 | state.numOfFunctions = newNumberOfFunctions 64 | case .decrementnumOfFunctions: 65 | guard state.numOfFunctions > 1 else { return } 66 | let newNumberOfFunctions = state.numOfFunctions - 1 67 | state.series = FourierSeries.make(state.seriesKind, numberOfFunctions: newNumberOfFunctions, scale: Constants.scale) 68 | state.numOfFunctions = newNumberOfFunctions 69 | case .pause: 70 | timer.stop() 71 | state.timerIsRunning = false 72 | case .play: 73 | timer.start() 74 | state.timerIsRunning = true 75 | case .reset: 76 | timer.reset() 77 | state.time = 0.0 78 | state.graph.removeAll() 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /SwiftUIFourierSeries/Model/Timer.swift: -------------------------------------------------------------------------------- 1 | import Combine 2 | import UIKit 3 | 4 | final class Timer: ObservableObject { 5 | 6 | // MARK: - Properties 7 | 8 | let objectWillChange = PassthroughSubject() 9 | 10 | var isRunning: Bool { 11 | return !displayLink.isPaused 12 | } 13 | 14 | private let frequency: Double 15 | private let unit: Double 16 | private(set) var time: TimeInterval = 0.0 17 | 18 | private lazy var displayLink: CADisplayLink = { 19 | let displayLink = CADisplayLink(target: self, selector: #selector(updateTime)) 20 | displayLink.add(to: .current, forMode: .default) 21 | return displayLink 22 | }() 23 | 24 | private var dt: Double { 25 | return unit * frequency * displayLink.duration 26 | } 27 | 28 | // MARK: - Life cycle 29 | 30 | init(frequency: Double = 1.0, unit: Double = 2 * .pi) { 31 | self.frequency = frequency 32 | self.unit = unit 33 | } 34 | 35 | deinit { 36 | stop() 37 | } 38 | 39 | // MARK: - Methods 40 | 41 | func start() { 42 | displayLink.isPaused = false 43 | } 44 | 45 | func stop() { 46 | displayLink.isPaused = true 47 | } 48 | 49 | func reset() { 50 | self.time = 0.0 51 | } 52 | 53 | // MARK: - Private methods 54 | 55 | @objc private func updateTime() { 56 | time += dt 57 | objectWillChange.send(time) 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /SwiftUIFourierSeries/Preview Content/Preview Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /SwiftUIFourierSeries/SwiftUIFourierSeries.entitlements: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | com.apple.security.app-sandbox 6 | 7 | com.apple.security.network.client 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /SwiftUIFourierSeries/Views/ButtonsView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ButtonsView.swift 3 | // SwiftUIFourierSeries 4 | // 5 | // Created by Jessica Trinh on 2/17/21. 6 | // Copyright © 2021 Simon Stiefel. All rights reserved. 7 | // 8 | 9 | import SwiftUI 10 | 11 | struct ButtonsView: View { 12 | @ObservedObject var store: Store 13 | 14 | var body: some View { 15 | VStack { 16 | HStack { 17 | if self.store.state.timerIsRunning { 18 | Button("Pause") { self.store.send(.pause) } 19 | } else { 20 | Button("Play") { self.store.send(.play) } 21 | } 22 | Button("Reset") { self.store.send(.reset) } 23 | } 24 | .padding() 25 | 26 | HStack { 27 | Text("Number of functions:") 28 | Button("-") { self.store.send(.decrementnumOfFunctions) } 29 | .disabled(self.store.state.numOfFunctions <= 1) 30 | Text("\(self.store.state.numOfFunctions)") 31 | Button("+") { self.store.send(.incrementnumOfFunctions) } 32 | } 33 | 34 | } 35 | } 36 | } 37 | 38 | struct ButtonsView_Previews: PreviewProvider { 39 | static var previews: some View { 40 | let timer = Timer(frequency: 0.2) 41 | let store = Store(kind: .squareWave, numOfFunctions: 10, timer: timer) 42 | 43 | return ButtonsView(store: store) 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /SwiftUIFourierSeries/Views/ConfigView.swift: -------------------------------------------------------------------------------- 1 | import Combine 2 | import SwiftUI 3 | 4 | struct ConfigView: View { 5 | @ObservedObject var store: Store 6 | @Environment(\.verticalSizeClass) var verticalSizeClass: UserInterfaceSizeClass? 7 | @Environment(\.horizontalSizeClass) var horizontalSizeClass: UserInterfaceSizeClass? 8 | 9 | var body: some View { 10 | // https://developer.apple.com/design/human-interface-guidelines/ios/visual-design/adaptivity-and-layout/ 11 | if verticalSizeClass == .regular && horizontalSizeClass == .compact { 12 | // iPhone Portrait or iPad 1/3 split view for Multitasking for instance 13 | // A 14 | // B 15 | // C 16 | VStack { 17 | VStack { 18 | ButtonsView(store: store).padding() 19 | PickerView(store: store).padding() 20 | } 21 | } 22 | } else { 23 | // Landscape layout for all other size class combinations 24 | // A B C 25 | VStack { 26 | HStack { 27 | ButtonsView(store: store).padding() 28 | PickerView(store: store).padding() 29 | } 30 | } 31 | } 32 | } 33 | } 34 | 35 | struct ConfigView_Previews: PreviewProvider { 36 | static var previews: some View { 37 | let timer = Timer(frequency: 0.2) 38 | let store = Store(kind: .squareWave, numOfFunctions: 10, timer: timer) 39 | 40 | return ConfigView(store: store) 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /SwiftUIFourierSeries/Views/EpicircleView.swift: -------------------------------------------------------------------------------- 1 | import SwiftUI 2 | 3 | struct EpicircleView: View { 4 | @ObservedObject var store: Store 5 | let index: Int 6 | var function: FourierSeries.Function? { 7 | guard store.state.series.functions.count > index else { return nil } 8 | return store.state.series.functions[index] 9 | } 10 | 11 | var radius: CGFloat { 12 | return abs(function?.amplitude ?? 0.0) 13 | } 14 | 15 | var body: some View { 16 | let circle = Circle() 17 | .stroke(lineWidth: 1.0) 18 | .foregroundColor(.secondary) 19 | .frame( 20 | width: radius * 2.0, 21 | height: radius * 2.0 22 | ) 23 | 24 | var path = Path() 25 | 26 | if let function = function { 27 | // Path from center to point on circle 28 | path.move(to: CGPoint(x: radius, y: radius)) 29 | var point = function.point(at: store.state.time) 30 | point.x += radius 31 | point.y += radius 32 | path.addLine(to: point) 33 | } 34 | 35 | return circle 36 | .overlay( 37 | path 38 | .stroke(lineWidth: 1.0) 39 | .foregroundColor(.primary) 40 | ) 41 | } 42 | } 43 | 44 | struct EpicircleView_Previews: PreviewProvider { 45 | static var previews: some View { 46 | let timer = Timer(frequency: 1.0) 47 | let store = Store(kind: .squareWave, numOfFunctions: 1, timer: timer) 48 | 49 | return EpicircleView(store: store, index: 0) 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /SwiftUIFourierSeries/Views/EpicirclesView.swift: -------------------------------------------------------------------------------- 1 | import Combine 2 | import SwiftUI 3 | 4 | struct EpicirclesView: View { 5 | @ObservedObject var store: Store 6 | 7 | var body: some View { 8 | ZStack { 9 | ForEach(0.. some View { 13 | var path = Path() 14 | 15 | for (x,y) in store.state.graph.ys.reversed().enumerated() { 16 | // Center vertically 17 | let y = y + Double(reader.size.height / 2.0) 18 | if x == 0 { 19 | path.move(to: CGPoint(x: Double(x), y: y)) 20 | } else { 21 | path.addLine(to: CGPoint(x: Double(x), y: y)) 22 | } 23 | } 24 | 25 | return path 26 | .stroke(lineWidth: 1.0) 27 | .foregroundColor(.primary) 28 | } 29 | } 30 | 31 | struct GraphView_Previews: PreviewProvider { 32 | static var previews: some View { 33 | let timer = Timer(frequency: 0.2) 34 | let store = Store(kind: .squareWave, numOfFunctions: 5, timer: timer) 35 | 36 | return GraphView(store: store) 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /SwiftUIFourierSeries/Views/PickerView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // PickerView.swift 3 | // SwiftUIFourierSeries 4 | // 5 | // Created by Jessica Trinh on 2/17/21. 6 | // Copyright © 2021 Simon Stiefel. All rights reserved. 7 | // 8 | 9 | import SwiftUI 10 | 11 | struct PickerView: View { 12 | @ObservedObject var store: Store 13 | 14 | var body: some View { 15 | Picker(selection: 16 | Binding( 17 | get: { self.store.state.seriesKind }, 18 | set: { self.store.send(.changeKind($0)) } 19 | ), 20 | label: Text(""), 21 | content: { 22 | ForEach(FourierSeries.Kind.allCases, id: \.rawValue) { kind in 23 | return Text(kind.rawValue).tag(kind) 24 | } 25 | } 26 | ) 27 | } 28 | } 29 | 30 | struct PickerView_Previews: PreviewProvider { 31 | static var previews: some View { 32 | let timer = Timer(frequency: 0.2) 33 | let store = Store(kind: .squareWave, numOfFunctions: 10, timer: timer) 34 | 35 | return PickerView(store: store) 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /SwiftUIFourierSeries/Views/SeriesView.swift: -------------------------------------------------------------------------------- 1 | import SwiftUI 2 | 3 | struct SeriesView: View { 4 | @ObservedObject var store: Store 5 | @State var viewBounds = BoundsData() 6 | 7 | struct BoundsData: Identifiable, Equatable { 8 | let id = UUID() 9 | var cirlcesBounds: CGRect? 10 | var graphBounds: CGRect? 11 | } 12 | 13 | struct BoundsPreferenceKey: PreferenceKey { 14 | typealias Value = BoundsData 15 | 16 | static var defaultValue: SeriesView.BoundsData = BoundsData() 17 | 18 | static func reduce(value: inout SeriesView.BoundsData, nextValue: () -> SeriesView.BoundsData) { 19 | let next = nextValue() 20 | if let nextCircles = next.cirlcesBounds { 21 | value.cirlcesBounds = nextCircles 22 | } 23 | 24 | if let nextGraph = next.graphBounds { 25 | value.graphBounds = nextGraph 26 | } 27 | } 28 | } 29 | 30 | var body: some View { 31 | VStack { 32 | GeometryReader { geometry in 33 | HStack( 34 | alignment: VerticalAlignment.center, 35 | content: { 36 | Spacer() 37 | .frame(width: CGFloat(50.0)) 38 | 39 | EpicirclesView(store: self.store) 40 | .transformAnchorPreference(key: BoundsPreferenceKey.self, value: .bounds) { (data: inout BoundsData, anchor) in 41 | data.cirlcesBounds = geometry[anchor] 42 | } 43 | Spacer() 44 | .frame(width: CGFloat(80.0)) 45 | GraphView(store: self.store) 46 | .transformAnchorPreference(key: BoundsPreferenceKey.self, value: .bounds) { (data: inout BoundsData, anchor) in 47 | data.graphBounds = geometry[anchor] 48 | } 49 | } 50 | ) 51 | .overlay(self.pen) 52 | .onPreferenceChange(BoundsPreferenceKey.self) { self.viewBounds = $0 } 53 | } 54 | 55 | ConfigView(store: self.store) 56 | } 57 | } 58 | 59 | private var pen: some View { 60 | var path = Path() 61 | 62 | // Start from Epicircle absolute point 63 | var point = store.state.series.absolutePointForSeries(at: store.state.time) 64 | if let epicirclesBounds = viewBounds.cirlcesBounds { 65 | point.x += epicirclesBounds.midX 66 | point.y += epicirclesBounds.midY 67 | } 68 | path.move(to: point) 69 | point.x = viewBounds.graphBounds?.minX ?? 0 70 | path.addLine(to: point) 71 | 72 | return path 73 | .stroke(lineWidth: 1.0) 74 | .foregroundColor(.red) 75 | } 76 | } 77 | 78 | struct SeriesView_Previews: PreviewProvider { 79 | static var previews: some View { 80 | let timer = Timer(frequency: 0.2) 81 | let store = Store(kind: .squareWave, numOfFunctions: 10, timer: timer) 82 | 83 | return SeriesView(store: store) 84 | } 85 | } 86 | --------------------------------------------------------------------------------