Skip to content

Instantly share code, notes, and snippets.

@iliakonnov
Last active April 12, 2020 18:53
Show Gist options
  • Save iliakonnov/5baa47f541b7bf2c673e64cb0a332aa7 to your computer and use it in GitHub Desktop.
Save iliakonnov/5baa47f541b7bf2c673e64cb0a332aa7 to your computer and use it in GitHub Desktop.
def getChar(x, y, accuracy, fns):
d = 1 / accuracy / 2
res = ' '
clr = 0
n = 0
for fn in fns:
ll = 1 if fn(x, y)[0] else 0
lr = 2 if fn(x + d, y)[0] else 0
ul = 4 if fn(x, y + d)[0] else 0
ur = 8 if fn(x + d, y + d)[0] else 0
char = ll + lr + ul + ur
if char == 0:
continue
clr += fn(x, y)[1]
n += 1
res = [
' ', # 0000
'▖', # 0001 (ll)
'▗', # 0010 (lr)
'▄', # 0011 (lr + ll = l_)
'▘', # 0100 (ul)
'▌', # 0101 (ul + ll + _l)
'▚', # 0110 (ul + lr)
'▙', # 0111 (ul + lr + ll)
'▝', # 1000 (ur)
'▞', # 1001 (ur + ll)
'▐', # 1010 (ur + lr = _r)
'▟', # 1011 (ur + lr + ll)
'▀', # 1100 (ur + ul = u_)
'▛', # 1101 (ur + ul + ll)
'▜', # 1110 (ur + ul + lr)
'█', # 1111 (ur + ul + lr + ll = __)
][char]
if n == 0:
return ' '
if n > 1:
clr = (clr % 7) + 1
return '\x1b[3{}m{}\x1b[0m'.format(clr, res)
def plot(*fns, accuracy=5, xs=(-5, 5), ys=(-5,5)):
accuracy = 5
iList = list(reversed(
range(xs[0]*accuracy, xs[1]*accuracy + 1)
))
jList = list(
range(ys[0]*accuracy, ys[1]*accuracy + 1)
)
names = '; '.join(fn.__name__ for fn in fns)
placeholder = '*' * len(names)
prenames = placeholder.center(len(jList), '~')
names = '; '.join(
'\x1b[3{}m{}\x1b[0m'.format(fn(0, 0)[1], fn.__name__)
for fn in fns
)
print('\n', prenames.replace(placeholder, names))
for i in iList:
for j in jList:
x = j / accuracy
y = i / accuracy
print(getChar(x, y, accuracy, fns), end='')
print('.', i / accuracy)
for j in jList:
if j % accuracy == 0:
print(abs(j // accuracy), end='')
else:
print('.', end='')
print('')
def thick(thickness=0.3):
def helper(f):
def res(x, y):
val = f(x)
color = None
if isinstance(val, tuple):
val, color = val
if val is None:
val = False
else:
val = abs(val - y) <= thickness
if color is not None:
return val, color
else:
return val
return res
return helper
def colored(color=1):
def helper(f):
def res(*args, **kwargs):
val = f(*args, **kwargs)
if isinstance(val, tuple):
val, _ = val
return val, color
return res
return helper
def named(name=None):
def helper(f):
def res(*args, **kwargs):
return f(*args, **kwargs)
res.__name__ = name
return res
return helper
def patch(name=None, color=None, thickness=None):
def res(func):
n = name if name is not None else func.__name__
if thickness is not None:
func = thick(thickness)(func)
if color is not None:
func = colored(color)(func)
func.__name__ = n
return func
return res
def invert(func):
def res(x, y):
val = func(x, y)
if isinstance(val, tuple):
return not val[0], val[1]
else:
return not val
res.__name__ = 'not {}'.format(func.__name__)
return res
def d(dx=0.001):
def helper(f):
def res(x):
f1 = f(x)
f2 = f(x+dx)
if f1 is None or f2 is None:
return None
df = f2 - f1
return df / dx
return res
return helper
def circle(r, thickness=None):
def circle(x, y):
t = x**2 + y**2
a = t < r**2
b = thickness is None or t > (r - thickness)**2
return a and b
return circle
import math
func = math.sin
d = d(0.0001)
dd = lambda f: d(d(f))
plot(
patch("f", 1, 0.05)( (func)),
patch("f'", 2, 0.05)( d(func)),
patch("f''", 3, 0.05)(dd(func)),
patch(color=4)(invert(circle(5))),
patch("x²", 6, 0.15)(lambda x: x**2 - 5),
patch(color=7)(circle(3, thickness=0.1)),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment