Skip to content

Instantly share code, notes, and snippets.

@jsbain
Created March 17, 2019 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsbain/a26fa4250a40c7a6da5c4a028fe7b5aa to your computer and use it in GitHub Desktop.
Save jsbain/a26fa4250a40c7a6da5c4a028fe7b5aa to your computer and use it in GitHub Desktop.
plotView.py
import ui
import numpy as np
W=1024
import time
from objc_util import *
CAShapeLayer=ObjCClass('CAShapeLayer')
t=np.linspace(0,1,1024)
f=25
class plotView(ui.View):
def update(self):
y=self.A*np.sin(2*np.pi*f*t)*np.exp(-t/self.tau)+self.height/2
p=ui.Path()
p.move_to(0,y[0])
for ti,yi in zip((t-t[0])/(t[-1]-t[0])*W,y):
p.line_to(ti,yi)
self.L.path=p.objc_instance.CGPath()
self.L.setNeedsDisplay()
self.fps.text='{}'.format(1/(time.perf_counter()-self.t))
self.t=time.perf_counter()
def __init__(self):
self.bg_color='white'
L=CAShapeLayer.alloc().init()
L.strokeColor=UIColor.redColor().CGColor()
L.fillColor=UIColor.clearColor().CGColor()
L.lineWidth=2
self.objc_instance.layer().addSublayer_(L)
L.setNeedsDisplay()
self.L=L
self.tau=1
self.A=200
self.fps=ui.Label(0,0,100,40)
self.fps.text='0.0'
self.t=time.perf_counter()
self.add_subview(self.fps)
def touch_moved(self,touch):
x,y=touch.location
#up/down bumps up amplitude
#l/r changes tau
self.tau=x/1024
self.A=y-self.center.y
self.update()
#self.L.setNeedsDisplay()
v=plotView()
#v.update_interval=1/15
v.present()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment