Skip to content

Instantly share code, notes, and snippets.

@iliakonnov
Created August 5, 2020 16:33
Show Gist options
  • Save iliakonnov/326d47da34a3af1c2bc00e0c5c383d07 to your computer and use it in GitHub Desktop.
Save iliakonnov/326d47da34a3af1c2bc00e0c5c383d07 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 in_complex(func):
def wrapped(x, y):
return func(x + y*1j)
wrapped.__name__ = func.__name__
return wrapped
plot(
patch(color=3, name='Circle')(
in_complex(lambda z: abs(z) <= 4)
),
)
@basti39
Copy link

basti39 commented Jul 7, 2023

اريد هذا السكريبت من فضلكم لو سمحتم وشكرا

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment