{"id":16685947,"url":"https://github.com/mosheberman/precalc","last_synced_at":"2025-03-17T00:32:55.643Z","repository":{"id":139430993,"uuid":"64894469","full_name":"MosheBerman/Precalc","owner":"MosheBerman","description":"This repo contains some code which can graph equations in a UIView.","archived":false,"fork":false,"pushed_at":"2017-05-25T23:18:20.000Z","size":858,"stargazers_count":105,"open_issues_count":5,"forks_count":7,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-02-27T15:30:22.786Z","etag":null,"topics":["calculus","graph-equations","graphview","uiview"],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MosheBerman.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-08-04T02:16:29.000Z","updated_at":"2023-08-09T09:37:08.000Z","dependencies_parsed_at":null,"dependency_job_id":"eeab030c-8cd5-4b35-bdb9-6f6c48842220","html_url":"https://github.com/MosheBerman/Precalc","commit_stats":{"total_commits":29,"total_committers":3,"mean_commits":9.666666666666666,"dds":0.06896551724137934,"last_synced_commit":"6c57ecaa1777f08cfaa5f111ac21c8a769620de5"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MosheBerman%2FPrecalc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MosheBerman%2FPrecalc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MosheBerman%2FPrecalc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MosheBerman%2FPrecalc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MosheBerman","download_url":"https://codeload.github.com/MosheBerman/Precalc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243835942,"owners_count":20355611,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["calculus","graph-equations","graphview","uiview"],"created_at":"2024-10-12T15:03:57.198Z","updated_at":"2025-03-17T00:32:55.630Z","avatar_url":"https://github.com/MosheBerman.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Precalc\nThis repo contains some code which can graph equations in a UIView.\n\n[![A quick demo](./demo.png)](./demo.png)\n\nSummary:\n---\n\nBasically, you define an equation, tell the graph what color to draw in, and CoreGraphics does the rest.\n\nI've been meaning to make something like this during precalculus and then during calculus 1, so before I take calc 2, I'm finally making this thing.\n\nTechnical:\n---\n\nWritten during bus and subway commutes using Xcode 7.3.1 and Swift 2.2.\n\nUsing It:\n---\n\nYou can draw one of the predefined graphs by instantiating a `GraphView`, an instance of a `GraphableEquation`, and then adding it to the `GraphView`:\n\n```swift\nlet graph = GraphView(withSmallerXBound: -15.0, largerXBound: 15.0, andInterval: 0.5)\nlet sine = Sine()\ngraph.addEquation(sine)\n```\n\nHere's the output:\n\n![A graph of a sine wave](./demosin.png)\n\nYou can add multiple equations to a single GraphView, like so:\n\n```swift\nlet graph = GraphView(withSmallerXBound: -15.0, largerXBound: 15.0, andInterval: 0.5)\n\nlet sine = Sine()\nlet line = Line(slope: 1.0, offset: 4.0)\nlet exponential = Exponential(exponent: 2.0)\n\ngraph.addEquation(sine)\ngraph.addEquation(line)\ngraph.addEquation(exponential)\n```    \n    \nCheck it out:\n\n![Multiple Equations on a Single Graph](./multiple.png)\n\nAbout the Graph View:\n---\n\nThe initializer of the `GraphView` sets up how the graph should be drawn, mimicing how you might do it in real life:\n\n```swift\nlet graph = GraphView(withSmallerXBound: -15.0, largerXBound: 15.0, andInterval: 0.5)\n```\n\nThe \"smaller x bound\" is the negative x value on the left edge, and the \"larger\" one is the positive x value off to the right.\n\nGraphs are always square, and scale to fit inside the frame of the GraphView. (The frame is currently hard coded to some value I liked during testing. There's a TODO to make this customizable.)\n\nIf you make the bounds farther apart from each other, the graph will have smaller boxes, more points, and take longer to draw. If you make the X values closer to each other, you get... bigger boxes, fewer points, and maybe a quicker draw. \n\nThe interval is how often along the X axis we want equations to calculate a Y value. Think of this as how many points we want to draw on each square on our graph paper.\n\nImplementing Your Own Equations:\n---\n\nTo add your own equation, conform to the `Equation` protocol:\n\n```swift\nprotocol Equation\n{\n    func compute(at x: CGFloat) -\u003e CGFloat   \n}\n```\n\n---\nNote: Previous versions of this playground used a different version of the Equation protocol, which pre-cached coordinates. That approach made it difficult to batch equation computations together, and prevented function composition, so it was removed.\n\n---\n    \nThe graph view can draw your equation if you implement the compute function and also adopt `GraphableEquation`, which defines the color of your drawing on the graph.\n\n```swift\nprotocol GraphableEquation : Equation {\n    var drawingColor : UIColor { get set }\n    var drawingDomain: Range\u003cCGFloat\u003e? { get set }\n}\n```\n\nHere's an example `GraphableEquation` implementation for the sine formula we used earlier:\n\n```swift\n//: Sine\n\nclass Sine : GraphableEquation\n{\n    var period: CGFloat\n    var amplitude: CGFloat\n    var phaseShift: CGFloat\n    var verticalShift: CGFloat\n    \n    // MARK: - Initializer\n    \n    init(period: CGFloat, amplitude: CGFloat, phaseShift: CGFloat, verticalShift: CGFloat)\n    {\n        self.period = period\n        self.amplitude = amplitude\n        self.phaseShift = phaseShift\n        self.verticalShift = verticalShift\n    }\n    \n    convenience init()\n    {\n        self.init(period: 1.0, amplitude: 1.0, phaseShift: 0.0, verticalShift: 0.0)\n    }\n    \n    // MARK: - GraphableEquation\n    \n    var drawingColor: UIColor = UIColor.black\n    var drawingDomain: Range\u003cCGFloat\u003e?\n    \n    // MARK: - Equation\n    \n    func compute(at x: CGFloat) -\u003e CGFloat\n    {\n        return amplitude * cos((self.period * x) - (self.phaseShift/self.period)) + self.verticalShift\n    }\n}\n\n```\n\nWe just implement the formula for a sine wave, taking into account the possible transformations built into the equation.\n\nThe cool thing about this protocol based system is that we can implement convenience initializers specific to our function, and as long as we can supply the graph with coordinates, it will do the right thing. \n\nFor example, our sine equation has an amplitude parameter. A line equation might have a slope and an offset instead. For example:\n\n```swift\nlet line = Line(slope: 1.0, offset: 3.0)\n```\n\nThere's more information in the playground, so take a look! (If you're feeling ambitious, maybe take a stab at one of these TODO items.)\n\nTODO:\n---\n\nSee [Issues](https://github.com/MosheBerman/Precalc/issues).\n\nLicense:\n---\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmosheberman%2Fprecalc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmosheberman%2Fprecalc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmosheberman%2Fprecalc/lists"}