All Projects → MosheBerman → Precalc

MosheBerman / Precalc

Licence: mit
This repo contains some code which can graph equations in a UIView.

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Precalc

Ppbadgeview
iOS Custom Badge, Support UIView, UITabBarItem, UIBarButtonItem ,Support Objective-C/Swift; iOS自定义Badge组件, 支持UIView, UITabBarItem, UIBarButtonItem, 支持Objective-C/Swift
Stars: ✭ 807 (+640.37%)
Mutual labels:  uiview
Capsuleview
An easy to use drop-in view to create a beautiful card style effect with a title placeholder and body content.
Stars: ✭ 68 (-37.61%)
Mutual labels:  uiview
Ios Hierarchy Viewer
iOS Hierarchy viewer - View and Coredata debugging made easy
Stars: ✭ 1,345 (+1133.94%)
Mutual labels:  uiview
Corerender
Moved to https://github.com/alexdrone/Render
Stars: ✭ 25 (-77.06%)
Mutual labels:  uiview
Camerabackground
Show camera layer as a background to any UIView
Stars: ✭ 57 (-47.71%)
Mutual labels:  uiview
Fractional Differentiation Time Series
As described in Advances of Machine Learning by Marcos Prado.
Stars: ✭ 78 (-28.44%)
Mutual labels:  calculus
Mathparser.org Mxparser
Math Parser Java Android C# .NET/MONO (.NET Framework, .NET Core, .NET Standard, .NET PCL, Xamarin.Android, Xamarin.iOS) CLS Library - a super easy, rich and flexible mathematical expression parser (expression evaluator, expression provided as plain text / strings) for JAVA and C#. Main features: rich built-in library of operators, constants, math functions, user defined: arguments, functions, recursive functions and general recursion (direct / indirect). Additionally parser provides grammar and internal syntax checking.
Stars: ✭ 624 (+472.48%)
Mutual labels:  calculus
Ihequalizerview
An Custom UIView which draws the output of an audio asset in real time.
Stars: ✭ 106 (-2.75%)
Mutual labels:  uiview
Tweenkit
Animation library for iOS in Swift
Stars: ✭ 1,146 (+951.38%)
Mutual labels:  uiview
Panslip
Use PanGesture to dismiss view on UIViewController and UIView
Stars: ✭ 93 (-14.68%)
Mutual labels:  uiview
Leego
Declarative, configurable & highly reusable UI development as making Lego bricks.
Stars: ✭ 967 (+787.16%)
Mutual labels:  uiview
Hhcustomcorner
Awesome library to customize corners of UIView and UIButton. Now you can customize each corner differently
Stars: ✭ 36 (-66.97%)
Mutual labels:  uiview
Afviewshaker
Stars: ✭ 1,214 (+1013.76%)
Mutual labels:  uiview
Carouselview
Carousel View
Stars: ✭ 22 (-79.82%)
Mutual labels:  uiview
Hackermath
Introduction to Statistics and Basics of Mathematics for Data Science - The Hacker's Way
Stars: ✭ 1,380 (+1166.06%)
Mutual labels:  calculus
Viewanimator
ViewAnimator brings your UI to life with just one line
Stars: ✭ 6,592 (+5947.71%)
Mutual labels:  uiview
Phfcomposebarview
Compose bar from iOS 7 Messages.app
Stars: ✭ 1,197 (+998.17%)
Mutual labels:  uiview
Stanford Cme 102 Ordinary Differential Equations
VIP cheatsheets for Stanford's CME 102 Ordinary Differential Equations for Engineers
Stars: ✭ 109 (+0%)
Mutual labels:  calculus
Lazysets.jl
A Julia package for calculus with convex sets
Stars: ✭ 107 (-1.83%)
Mutual labels:  calculus
Shapeview
A customized shape view with shadow and transparent background supported.
Stars: ✭ 90 (-17.43%)
Mutual labels:  uiview

Precalc

This repo contains some code which can graph equations in a UIView.

A quick demo

Summary:

Basically, you define an equation, tell the graph what color to draw in, and CoreGraphics does the rest.

I'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.

Technical:

Written during bus and subway commutes using Xcode 7.3.1 and Swift 2.2.

Using It:

You can draw one of the predefined graphs by instantiating a GraphView, an instance of a GraphableEquation, and then adding it to the GraphView:

let graph = GraphView(withSmallerXBound: -15.0, largerXBound: 15.0, andInterval: 0.5)
let sine = Sine()
graph.addEquation(sine)

Here's the output:

A graph of a sine wave

You can add multiple equations to a single GraphView, like so:

let graph = GraphView(withSmallerXBound: -15.0, largerXBound: 15.0, andInterval: 0.5)

let sine = Sine()
let line = Line(slope: 1.0, offset: 4.0)
let exponential = Exponential(exponent: 2.0)

graph.addEquation(sine)
graph.addEquation(line)
graph.addEquation(exponential)

Check it out:

Multiple Equations on a Single Graph

About the Graph View:

The initializer of the GraphView sets up how the graph should be drawn, mimicing how you might do it in real life:

let graph = GraphView(withSmallerXBound: -15.0, largerXBound: 15.0, andInterval: 0.5)

The "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.

Graphs 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.)

If 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.

The 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.

Implementing Your Own Equations:

To add your own equation, conform to the Equation protocol:

protocol Equation
{
    func compute(at x: CGFloat) -> CGFloat   
}

Note: 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.


The 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.

protocol GraphableEquation : Equation {
    var drawingColor : UIColor { get set }
    var drawingDomain: Range<CGFloat>? { get set }
}

Here's an example GraphableEquation implementation for the sine formula we used earlier:

//: Sine

class Sine : GraphableEquation
{
    var period: CGFloat
    var amplitude: CGFloat
    var phaseShift: CGFloat
    var verticalShift: CGFloat
    
    // MARK: - Initializer
    
    init(period: CGFloat, amplitude: CGFloat, phaseShift: CGFloat, verticalShift: CGFloat)
    {
        self.period = period
        self.amplitude = amplitude
        self.phaseShift = phaseShift
        self.verticalShift = verticalShift
    }
    
    convenience init()
    {
        self.init(period: 1.0, amplitude: 1.0, phaseShift: 0.0, verticalShift: 0.0)
    }
    
    // MARK: - GraphableEquation
    
    var drawingColor: UIColor = UIColor.black
    var drawingDomain: Range<CGFloat>?
    
    // MARK: - Equation
    
    func compute(at x: CGFloat) -> CGFloat
    {
        return amplitude * cos((self.period * x) - (self.phaseShift/self.period)) + self.verticalShift
    }
}

We just implement the formula for a sine wave, taking into account the possible transformations built into the equation.

The 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.

For example, our sine equation has an amplitude parameter. A line equation might have a slope and an offset instead. For example:

let line = Line(slope: 1.0, offset: 3.0)

There'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.)

TODO:

See Issues.

License:

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].