• Stars
    star
    105
  • Rank 328,196 (Top 7 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 8 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

This repo contains some code which can graph equations in a 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

More Repositories

1

MBCalendarKit

An open source calendar framework for iOS, with support for customization, IBDesignable, Autolayout, and more.
Objective-C
560
star
2

Surfboard

Surfboard is a delightful onboarding framework for iOS.
Objective-C
441
star
3

PatronKit

A framework to add patronage to your apps.
Swift
363
star
4

MBTileParser

MBTileParser is a game engine written using pure UIKit in the days before SpriteKit.
Objective-C
291
star
5

MBMenuController

MBMenuController is similar to UIActionSheet.
Objective-C
72
star
6

ios-audio-remote-control

This repo demonstrates how to control the software based audio remote control in iOS.
Objective-C
52
star
7

FilterBar

A marriage of UINavigationBar and UISegmentedControl.
Swift
34
star
8

KosherCocoa-legacy

A port of the KosherJava project from Java to Objective-C Cocoa
Objective-C
22
star
9

ScrollViewSnapshotter

A demo project showing how to correctly snapshot the contents UIScrollView and its subclasses.
Swift
18
star
10

SpeedDialApp

A speed dial app for iOS.
Objective-C
10
star
11

Fahrii

A wrapper on Safari Mobile to support userscripts
Objective-C
7
star
12

KosherCocoa

My Objective-C port of KosherJava. KosherCocoa enables you to perform sunrise-based and sunset-based calculations for Jewish prayer and calendar.
Objective-C
7
star
13

GiphyKit

A Giphy Client + Framework in Swift
Swift
4
star
14

SpiffyKit

A framework for a drop-in view controller that allows users to contact you and share your app.
Objective-C
4
star
15

CustomScrollIndicator

A quick demo project demonstrating how to customize UIScrollView's scroll indicators.
Swift
3
star
16

MinimalOverflow

A userscript that invokes a minimalist theme on StackOverflow.com
JavaScript
3
star
17

Nippon

Nippon is a classic turn-based trading game. Reinvented for iOS.
Objective-C
3
star
18

ABookOnC

I'm using this repo to brush up on my C code, working with A Book On C.
C
2
star
19

HackerSim

Simulates hacking like in a movie
C++
2
star
20

MBScrollingLabel

A drop-in replacement for UILabel that adds the ability to scroll text.
Objective-C
2
star
21

Fence

Fence is an iOS app which assists in the creation of geofences for use location-sensitive applications.
Objective-C
2
star
22

MBRecyclingScrollView

This is a barebones implementation of view recycling. It includes a UIScrollView subclass, a barebones datasource protocol, and a barebones delegate protocol. There's no demo UI at the moment, but all the files you need are in a single folder.
Objective-C
2
star
23

LevenshteinMachine

A small project to calculate Levenshtein Distance between two strings, including unit tests and samples.
Objective-C
1
star
24

StudentsFirst

A user script to make CUNY First easier to use.
JavaScript
1
star
25

Pantheon

Pantheon is a project that allows the editing of Xcode projects on the iPad.
Objective-C
1
star
26

Nippon-Swift

A rewrite of my iOS game, Nippon, in Swift.
Swift
1
star
27

my-snippets

This is a collection of code snippets for Xcode that help with implementing Appledoc.
1
star
28

html-house

An interactive house made of HTML and CSS.
1
star
29

Introspector

An app to introspect the Objective-C runtime.
Objective-C
1
star
30

Polygon

An app using Core Graphics and math used to draw polygons.
1
star
31

MBGoDavenScraper

This is a utility app for OS X to scrape some data from GoDaven.com
Objective-C
1
star
32

Hipster

Find a new release before it was cool.
Swift
1
star
33

Stolz

Stolz is a library for handling Facebook login on OS X
Objective-C
1
star
34

MosheBerman-ios

This repo contains the source code for the Moshe Berman app.
Objective-C
1
star
35

wedding-site

This is the rep for mosheberman.com/wedding
JavaScript
1
star
36

Bitachon.org

The source code for the Bitachon.org website
JavaScript
1
star
37

PolygonJS

A demo app using math and the HTML 5 canvas element to draw polygons.
1
star
38

MosheBerman.com

My iOS themed website
CSS
1
star
39

Anchor

Anchor is a proof of concept of an idea I had to use one iOS device to help debug an app on another.
Swift
1
star