#cas from math import pi, asin KEY_NONE, KEY_LEFT, KEY_UP, KEY_DOWN, KEY_RIGHT, KEY_ENTER, KEY_ESC, KEY_LEFT_PARENTHESIS, KEY_RIGHT_PARENTHESIS = 0, 1, 2, 3, 4, 53, 6, 34, 35 def fix_angle(a): return a * 2 * asin(1) / pi def col3_2_rgb(c, bits, bgr): return c[2*bgr]//2**(8 - bits[0]) + c[1]//2**(8 - bits[1])*2**bits[0] + c[2*(not(bgr))]//2**(8-bits[2])*2**(bits[0] + bits[1]) def hp_draw_line(x1, y1, x2, y2, c): line_p(x1, y1, x2, y2, col3_2_rgb(c, [8, 8, 8], 1)) def hp_fill_rect(x, y, w, h, c): rect_p(x, y, x + w - 1, y + h - 1, col3_2_rgb(c, [8, 8, 8], 1)) def hp_draw_circle(x, y, r, c): global L1 c = col3_2_rgb(c, [8, 8, 8], 1) arc_p(x, y, r, 0, fix_angle(2*pi), c) def hp_fill_circle(x, y, r, c): global L1, L2 c = col3_2_rgb(c, [8, 8, 8], 1) L2 = [c, c] arc_p(x, y, r, 0, fix_angle(2*pi), L2) def hp_draw_ellipse(x, y, rx, ry, c): global L1 c, L1 = col3_2_rgb(c, [8, 8, 8], 1), [rx, ry] arc_p(x, y, L1, 0, fix_angle(2*pi), c) def hp_fill_ellipse(x, y, rx, ry, c): global L1, L2 c, L1 = col3_2_rgb(c, [8, 8, 8], 1), [rx, ry] L2 = [c, c] arc_p(x, y, L1, 0, fix_angle(2*pi), L2) def hp_draw_string(s, x, y, cf, cb): textout_p(s, x, y, 1, col3_2_rgb(cf, [8, 8, 8], 1), len(s) * 9, col3_2_rgb(cb, [8, 8, 8], 1)) def hp_clear_screen(): rect_p(0, 0, 319, 239, 16777215) def hp_pause(): wait() def hp_sleep(d): wait(d) def hp_monotonic(d): return time() def hp_get_key(): key = get_key() d = {KEY_LEFT:7, KEY_UP:2, KEY_DOWN:12, KEY_RIGHT:8, KEY_ENTER:30, KEY_ESC:4, KEY_LEFT_PARENTHESIS:28 } for k in d: if key == d[k]: return k return 0 def kc1_draw_line(x1, y1, x2, y2, c): draw_line(float(x1), float(y1), float(x2), float(y2), not(col3_2_rgb(c, [1, 1, 1], 0))) def kc1_fill_rect(x, y, w, h, c): draw_rectangle(float(x), float(y), float(w), float(h), (not (col3_2_rgb(c, [1, 1, 1], 0))) + filled) def kc1_draw_circle(x, y, r, c): draw_arc(float(x), float(y), float(r), not (col3_2_rgb(c, [1, 1, 1], 0))) def kc1_fill_circle(x, y, r, c): draw_arc(float(x), float(y), float(r), (not (col3_2_rgb(c, [1, 1, 1], 0))) + filled) def kc1_draw_ellipse(x, y, rx, ry, c): draw_arc(float(x), float(y), float(rx), float(ry), 0, 2*pi, not (col3_2_rgb(c, [1, 1, 1], 0))) def kc1_fill_ellipse(x, y, rx, ry, c): draw_arc(float(x), float(y), float(rx), float(ry), 0, 2*pi, (not (col3_2_rgb(c, [1, 1, 1], 0))) + filled) def kc1_draw_string(s, x, y, cf, cb): draw_string(s, float(x), float(y), col3_2_rgb(cf, [1, 1, 1], 1), col3_2_rgb(cb, [1, 1, 1], 1)) tcanvas = None def get_infos(keys): global hp_draw_line, hp_fill_rect, hp_draw_circle, hp_fill_circle, hp_draw_ellipse, hp_fill_ellipse, hp_draw_string, kc1_draw_line, kc1_fill_rect, kc1_draw_ellipse, kc1_fill_ellipse, kc1_draw_string, hp_clear_screen, hp_pause, tcanvas fnop = lambda : None screen_w, screen_h, screen_y0, color_bits, font_w, font_num_w, font_h, poly_has_color, poly_get_pixel, poly_set_pixel, poly_show, poly_draw_line, poly_fill_rect, poly_draw_circle, poly_fill_circle, poly_draw_ellipse, poly_fill_ellipse, poly_monotonic, poly_sleep, poly_get_key, poly_draw_string, poly_clear_screen, poly_pause, poly_set_buffer, show_need_pause_on_exit, screen_need_clear_on_init, need_clear, need_line, need_rect, need_ellipse, need_string, need_monotonic, need_sleep = 0, 0, 0, [5, 6, 5], 0, 0, 0, 1, fnop, fnop, fnop, fnop, fnop, fnop, fnop, fnop, fnop, fnop, fnop, fnop, fnop, fnop, fnop, fnop, 0, 0, 0, 1, 1, 1, 1, 1, 1 try: if chr(256)==chr(0): # KhiCAS Python compat. need_line, need_rect, need_ellipse, need_string, need_sleep, need_monotonic = 0, 0, 0, 0, 0, 0 if "HP" in version(): kc1_draw_line, kc1_fill_rect, kc1_draw_circle, kc1_fill_circle, kc1_draw_ellipse, kc1_fill_ellipse, kc1_draw_string = None, None, None, None, None, None, None screen_w, screen_h, font_w, font_h, show_need_pause_on_exit = 320, 240, 9, 11, 1 font_num_w = font_w def poly_set_pixel(x, y, c): set_pixel(x, y, col3_2_rgb(c, [8, 8, 8], 1)) poly_draw_line, poly_fill_rect, poly_draw_circle, poly_fill_circle, poly_draw_ellipse, poly_fill_ellipse, poly_draw_string, poly_clear_screen, poly_pause, poly_sleep, poly_monotonic = hp_draw_line, hp_fill_rect, hp_draw_circle, hp_fill_hp_draw_circle, hp_draw_ellipse, hp_fill_ellipse, hp_draw_string, hp_clear_screen, hp_pause, hp_sleep, hp_monotonic else: # Graph 35+E II / NumWorks or Nspire / Graph 90+E hp_draw_line, hp_fill_rect, hp_draw_string, hp_clear_screen, hp_pause = None, None, None, None, None t_mono, t_ns, t_nw, need_clear = not (white), "Numworks" in version(), "Nspire" in version(), 1 screen_w, screen_h, font_w, font_h, poly_get_pixel, poly_pause, poly_sleep = 384 - (t_ns or t_nw)*64 - t_mono*256, 192 + (t_ns or t_nw)*30 - t_mono*128, get_pixel, 10 - 4*t_mono + 5*t_ns + t_nw, 10 - 5*t_mono + 8*(t_ns or t_nw), getKey, sleep font_num_w = font_w def poly_monotonic(): return monotonic / (1 + (not(t_nw or t_ns))*99) def poly_get_key(): key = get_key() d = {KEY_LEFT:0, KEY_UP:1, KEY_DOWN:2, KEY_RIGHT:3, KEY_ENTER:4, KEY_ESC:5, KEY_LEFT_PARENTHESIS:40, KEY_RIGHT_PARENTHESIS:41} for k in d: if key == d[k]: return k return 0 if t_mono: def poly_set_pixel(x, y, c): set_pixel(x, y, not(col3_2_rgb(c, [1,1,1], 0))) poly_draw_line, poly_fill_rect, poly_draw_circle, poly_fill_circle, poly_draw_ellipse, poly_fill_ellipse, poly_draw_string = kc1_draw_line, kc1_fill_rect, kc1_draw_circle, kc1_fill_circle, kc1_draw_ellipse, kc1_fill_ellipse, kc1_draw_string else: kc1_draw_line, kc1_fill_rect, kc1_draw_circle, kc1_fill_circle, kc1_draw_ellipse, kc1_fill_ellipse, kc1_draw_string = None, None, None, None, None, None, None def poly_set_pixel(x, y, c): set_pixel(x, y, col3_2_rgb(c, [5,6,5], 0)) def poly_draw_line(x1, y1, x2, y2, c): draw_line(float(x1), float(y1), float(x2), float(y2), col3_2_rgb(c, color_bits, 1)) def poly_fill_rect(x, y, w, h, c): draw_rectangle(float(x), float(y), float(w), float(h), col3_2_rgb(c, color_bits, 1) + filled) def poly_draw_circle(x, y, r, c): draw_arc(float(x), float(y), float(r), float(r), 0, 2*pi, col3_2_rgb(c, color_bits, 1)) def poly_fill_circle(x, y, r, c): draw_arc(float(x), float(y), float(r), float(r), 0, 2*pi, col3_2_rgb(c, color_bits, 1) + filled) def poly_draw_ellipse(x, y, rx, ry, c): draw_arc(float(x), float(y), float(rx), float(ry), 0, 2*pi, col3_2_rgb(c, color_bits, 1)) def poly_fill_ellipse(x, y, rx, ry, c): draw_arc(float(x), float(y), float(rx), float(ry), 0, 2*pi, col3_2_rgb(c, color_bits, 1) + filled) def poly_draw_string(s, x, y, cf, cb): draw_string(s, float(x), float(y), col3_2_rgb(cf, color_bits, 1), col3_2_rgb(cb, color_bits, 1)) except: pass if not (screen_w): hp_draw_line, hp_fill_rect, hp_draw_circle, hp_fill_circle, hp_draw_ellipse, hp_fill_ellipse, hp_draw_string, kc1_draw_line, kc1_fill_rect, kc1_draw_circle, kc1_fill_circle, kc1_draw_ellipse, kc1_fill_ellipse, kc1_draw_string, hp_clear_screen, hp_pause = None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None try: import sys try: t_nw = sys.platform == "numworks" if sys.platform == "nspire" or t_nw: try: # KhiCAS Micropython import graphic, nsp, cas, time def poly_draw_line(x1, y1, x2, y2, c): graphic.draw_line(int(x1), int(y1), int(x2), int(y2), col3_2_rgb(c, color_bits, 1)) def poly_fill_rect(x1, y1, x2, y2, c): graphic.draw_filled_rectangle(int(x1), int(y1), int(x2), int(y2), c) def poly_draw_circle(x, y, r, c): graphic.draw_circle(int(x), int(y), int(r), c) def poly_fill_circle(x, y, r, c): graphic.draw_filled_circle(int(x), int(y), int(r), c) def poly_draw_ellipse(x, y, rx, ry, c): graphic.draw_arc(int(x), int(y), int(rx), int(ry), 0, 360, c) def poly_fill_ellipse(x, y, rx, ry, c): graphic.draw_filled_arc(int(x), int(y), int(rx), int(ry), 0, 360, c) def poly_draw_string(s, x, y, cf, cb): cas.caseval('draw_string("' + s + '", ' + str(float(x)) + ', ' + str(float(y)) + ', ' + str(col3_2_rgb(cf, color_bits, 1)) + ', ' + str(col3_2_rgb(cb, color_bits, 1)) + ')') def poly_get_key(): key = int(cas.caseval("get_key()")) d = {KEY_LEFT:0, KEY_UP:1, KEY_DOWN:2, KEY_RIGHT:3, KEY_ENTER:4, KEY_ESC:5, KEY_LEFT_PARENTHESIS:40, KEY_RIGHT_PARENTHESIS:41} for k in d: if key == d[k]: return k return 0 def poly_monotonic(): return time.monotonic() / (1 + t_nw*999) screen_w, screen_h, font_w, font_h, poly_get_pixel, poly_set_pixel, poly_show, poly_pause, need_clear, need_line, need_rect, need_ellipse, need_string, need_monotonic = 320, 222, 15, 18, graphic.get_pixel, graphic.set_pixel, graphic.show_screen, nsp.waitKeypress, 1, 0, 0, 0, 0, 0 font_num_w = font_w except: # Nspire MicroPython import nsp screen_w, screen_h, need_clear, show_need_pause_on_exit = 320, 240, 1, 1 tcanvas = nsp.Texture(screen_w, screen_h, 0) poly_get_pixel = tcanvas.getPx def poly_set_pixel(x, y, c): if x>=0 and x<=screen_w-1 and y>=0 and y <=screen_h-1: tcanvas.setPx(x, y, col3_2_rgb(c, color_bits, 1)) poly_show, poly_pause = tcanvas.display, nsp.waitKeypress elif sys.platform == "TI-Nspire": # CX II import time, ti_system screen_w, screen_h, font_w, font_h, need_monotonic, need_sleep, poly_sleep = 318, 212, 12, 12, 0, 0, time.sleep font_num_w = font_w def test(d,f): t=f() time.sleep(d) return round((f()-t)/d,0) if test(0.1, time.clock) == 1: poly_monotonic = time.clock else: poly_monotonic = time.time key_d = {KEY_LEFT:'left', KEY_UP:'up', KEY_DOWN:'down', KEY_RIGHT:'right', KEY_ENTER:'enter', KEY_ESC:'esc', KEY_LEFT_PARENTHESIS:'(', KEY_RIGHT_PARENTHESIS:')'} def poly_get_key(): gk = ti_system.get_key() for k in key_d: if gk == key_d[k]: return k return 0 def poly_test_key(v): return ti_system.get_key() == key_d[v] if "gp" in keys: import ti_image need_line, need_rect, need_ellipse, need_string = 1, 1, 1, 1 tcanvas = ti_image.new_image(screen_w, screen_h, (255,255,255)) poly_get_pixel, poly_set_pixel = tcanvas.get_pixel, tcanvas.set_pixel def poly_show(): tcanvas.show_image(0,0) else: import ti_draw ti_draw.use_buffer() need_line, need_rect, need_ellipse, need_string, poly_show, set_buffer = 0, 0, 0, 0, ti_draw.paint_buffer, ti_draw.use_buffer def poly_set_pixel(x, y, c): ti_draw.set_color(tuple(c)) ti_draw.plot_xy(x, y, 7) def poly_draw_line(x1, y1, x2, y2, c): ti_draw.set_color(tuple(c)) ti_draw.draw_line(x1, y1, x2, y2) def poly_fill_rect(x, y, w, h, c): ti_draw.set_color(tuple(c)) ti_draw.fill_rect(x, y, w, h) def poly_draw_circle(x, y, r, c): ti_draw.set_color(tuple(c)) ti_draw.draw_circle(x, y, r) def poly_fill_circle(x, y, r, c): ti_draw.set_color(tuple(c)) ti_draw.fill_arc(x, y, r) def poly_draw_ellipse(x, y, rx, ry, c): ti_draw.set_color(tuple(c)) x0, y0 = x - rx, y - ry for dy in range(1 + (y0 > int(y0))): for dx in range(1 + (x0 > int(x0))): ti_draw.draw_arc(x0 + dx, y0 + dy, 2 * rx, 2 * ry, 0, 360) def poly_fill_ellipse(x, y, rx, ry, c): ti_draw.set_color(tuple(c)) ti_draw.fill_arc(x - rx, y - ry, 2 * rx, 2 * ry, 0, 360) def poly_draw_string(s, x, y, cf, cb): poly_fill_rect(x, y, font_w, font_h, cb) ti_draw.set_color(tuple(cf)) ti_draw.draw_text(x, y + font_h, s) elif sys.platform.startswith('TI-Python'): import ti_graphics, ti_system, time screen_w, screen_h, font_w, font_h, screen_y0, need_line, need_rect, need_ellipse, need_string, need_monotonic, need_sleep, poly_pause, poly_get_key, poly_sleep, poly_monotonic, need_clear, show_need_pause_on_exit, need_sleep, screen_need_clear_on_init = 320, 210, 10, 15, 30, 0, 0, 0, 0, 0, ti_system.disp_wait, ti_system.wait_key, time.sleep, time.monotonic, 1, 1, 0, 1 font_num_w = font_w def poly_draw_line(x1, y1, x2, y2, c): ti_graphics.setColor(c) ti_graphics.drawLine(x1, y1 + screen_y0, x2, y2 + screen_y0) def poly_fill_rect(x, y, w, h, c): ti_graphics.setColor(c) ti_graphics.fillRect(x, y + screen_y0, w, h) def poly_get_pixel(x, y): return ti_graphics.getPixel(x, y + screen_y0) def poly_set_pixel(x, y, c): ti_graphics.setPixel(x, y + screen_y0, c) def poly_draw_ellipse(x, y, rx, ry, c): ti_graphics.setColor(c) x0, y0 = x - rx, y - ry for dy in range(1 + (y0 > int(y0))): for dx in range(1 + (x0 > int(x0))): ti_graphics.drawArc(x0 + dx, y0 + dy + screen_y0, 2 * rx, 2 * ry, 0, 3600) def poly_fill_ellipse(x, y, rx, ry, c): ti_graphics.setColor(c) ti_graphics.fillArc(x - rx, y - ry + screen_y0, 2 * rx, 2 * ry, 0, 3600) def poly_draw_circle(x, y, r, c): poly_draw_ellipse(x, y, r, r, c) def poly_fill_circle(x, y, r, c): ti_graphics.setColor(c) ti_graphics.fillCircle(x, y + screen_y0, r) def poly_draw_string(s, x, y, cf, cb): poly_fill_rect(x, y, font_w, font_h, cb) ti_graphics.setColor(cf) ti_graphics.drawString(s, x, y + screen_y0) except: # Graph 35+E/USB / 75/85/95 pass except: pass if not (screen_w): # Casio Graph 90/35+E II try: import casioplot casioplot.set_pixel(0, 0, (0, 0, 255)) col = casioplot.get_pixel(0, 0) t = col[0] == col[2] screen_w, screen_h, poly_has_color, font_w, font_h, poly_get_pixel, poly_set_pixel, poly_show, poly_clear_screen, need_string = t and 128 or 384, t and 64 or 192, not (t), t and 6 or 10, t and 5 or 10, casioplot.get_pixel, casioplot.set_pixel, casioplot.show_screen, casioplot.clear_screen, 0 font_num_w = t and 4 or 8 def poly_draw_string(s, x, y, cf, cb): poly_fill_rect(x, y, font_w, font_h, cb) casioplot.draw_string(x, y, s, cf, "small") def poly_pause(): try: while(1): pass except KeyboardInterrupt: pass except: pass if not (screen_w): try: # NumWorks import kandinsky, ion, time screen_w, screen_h, font_w, font_h, poly_get_pixel, poly_set_pixel, poly_draw_string, poly_sleep, poly_monotonic, need_rect, need_string, need_monotonic, need_sleep = 320, 222, 11, 18, kandinsky.get_pixel, kandinsky.set_pixel, kandinsky.draw_string, time.sleep, time.monotonic, 0, 0, 0, 0 font_num_w = font_w def poly_fill_rect(x, y, w, h, c): kandinsky.fill_rect(int(x), int(y), int(w), int(h), c) def poly_pause(): def key_down(): for k in range(53): if ion.keydown(k): return 1 return 0 while key_down(): pass while not (key_down()): pass def poly_draw_string(s, x, y, cf, cb): kandinsky.draw_string(s, x, y, cf, cb) def poly_get_key(): d = {KEY_LEFT:0, KEY_UP:1, KEY_RIGHT:2, KEY_DOWN:3, KEY_ENTER:52, KEY_ESC:5, KEY_LEFT_PARENTHESIS:33, KEY_RIGHT_PARENTHESIS:34} for k in d: if ion.keydown(d[k]): return k return 0 except: try: # HP Prime import hpprime screen_w, screen_h, color_bits, need_clear, show_need_pause_on_exit, screen_need_clear_on_init = 320, 240, (8, 8, 8), 1, 1, 1 def poly_set_pixel(x, y, c): hpprime.pixon(0, x, y, col3_2_rgb(c, color_bits, 1)) def poly_pause(): while hpprime.keyboard(): pass while not (hpprime.keyboard()): pass def poly_get_key(): keys = hpprime.keyboard() d = {KEY_LEFT:9223372036854775808, KEY_UP:288230376151711744, KEY_DOWN:4503599627370496, KEY_RIGHT:281474976710656, KEY_ENTER:274877906944, KEY_ESC:1152921504606846976, KEY_LEFT_PARENTHESIS:68719476736} for k in d: if keys | d[k]: return k return 0 except: pass if screen_w: if need_line: def poly_draw_line(x1, y1, x2, y2, c): m, a1, b1, a2, b2 = 0, int(x1), int(y1), int(x2), int(y2) if (x2 - x1) ** 2 < (y2 - y1) ** 2: m, a1, a2, b1, b2 = 1, b1, b2, a1, a2 if min(a1, a2) != a1: a1, b1, a2, b2 = a2, b2, a1, b1 for k in range(a2 - a1 + 1): a, b = a1 + k, int(b1 + (b2 - b1) * k / ((a2 - a1) or 1)) poly_set_pixel((a, b)[m], (b, a)[m] + screen_y0, c) if need_rect: def poly_fill_rect(x, y, w, h, c): for k in range(h): poly_draw_line(x, y + k, x + w - 1, y + k, c) if need_ellipse: from math import sqrt def poly_draw_ellipse(x, y, rx, ry, c): for h in range(-int(ry), int(ry)+1): w = sqrt(max(0, rx*rx*(1-h*h/ry/ry))) x1, x2 = int(x - w), int(x + w) yc = int(y + h) poly_set_pixel(x1, yc, c) poly_set_pixel(x2, yc, c) for w in range(-int(rx), int(rx)+1): h = sqrt(max(0, ry*ry*(1-w*w/rx/rx))) y1, y2 = int(y - h), int(y + h) xc = int(x + w) poly_set_pixel(xc, y1, c) poly_set_pixel(xc, y2, c) def poly_fill_ellipse(x, y, rx, ry, c): for h in range(-int(ry), int(ry)+1): w = sqrt(max(0, rx*rx*(1-h*h/ry/ry))) x1, x2 = int(x - w), int(x + w) yc = int(y + h) poly_draw_line(x1, yc, x2, yc, c) def poly_draw_circle(x, y, r, c): poly_draw_ellipse(x, y, r, r, c) def poly_fill_circle(x, y, r, c): poly_fill_ellipse(x, y, r, r, c) if need_string: from polyfont import poly_font font_w, font_h = 11, 10 font_num_w = 9 def poly_draw_string(s, x, y, cf, cb): for c in s: c = ord(c) >= 33 and ord(c) <= 127 and ord(c) - 32 or 0 poly_fill_rect(x, y, font_w, font_h, cb) for v in (0,) + poly_font[c] + (0,): ty = y while v: if v & 1: poly_set_pixel(x, ty, cf) ty += 1 v //= 2 x += 1 if need_clear: def poly_clear_screen(): poly_fill_rect(0, 0, screen_w, screen_h, [255, 255, 255]) if screen_need_clear_on_init: poly_clear_screen_init = poly_clear_screen else: poly_clear_screen_init = fnop if need_sleep: def poly_sleep(d): if need_monotonic: for k in range(int(t*100000)): pass else: s = poly_monotonic() + d while poly_monotonic() < s: pass if show_need_pause_on_exit: def poly_show_exit(): poly_show() poly_pause() else: poly_show_exit = poly_show l = [] for k in keys: if k=="w": l.append(screen_w) if k=="h": l.append(screen_h) if k=="hc": l.append(poly_has_color) if k=="fw": l.append(font_w) if k=="fwn": l.append(font_num_w) if k=="fh": l.append(font_h) if k=="gp": l.append(poly_get_pixel) if k=="sp": l.append(poly_set_pixel) if k=="dl": l.append(poly_draw_line) if k=="fr": l.append(poly_fill_rect) if k=="dc": l.append(poly_draw_circle) if k=="fc": l.append(poly_fill_circle) if k=="de": l.append(poly_draw_ellipse) if k=="fe": l.append(poly_fill_ellipse) if k=="ds": l.append(poly_draw_string) if k=="cl": l.append(poly_clear_screen) if k=="cli": l.append(poly_clear_screen_init) if k=="sh": l.append(poly_show) if k=="she": l.append(poly_show_exit) if k=="p": l.append(poly_pause) if k=="sb": l.append(poly_set_buffer) if k=="m": l.append(poly_monotonic) if k=="s": l.append(poly_sleep) if k=="gk": l.append(poly_get_key) if k=="tk": l.append(poly_test_key) return l