LJ/clitools/generators/drawingTests/angleInteractive.py

263 lines
5.0 KiB
Python
Raw Normal View History

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# -*- mode: Python -*-
'''
This generator print diferent one angle but you can modify it interactively.
The key are:
* '' => *somthing*
* ...
v0.1.0
LICENCE : CC
by lapin (aka nipal)
'''
from __future__ import print_function
import time
import argparse
import math
import sys
# import for non-bloking input reading
#import sys
import select
import tty
import termios
name="generator::endingPoint"
def debug(*args, **kwargs):
if( verbose == False ):
return
print(*args, file=sys.stderr, **kwargs)
def isData():
return select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], [])
def flush_input():
try:
import msvcrt
while msvcrt.kbhit():
msvcrt.getch()
except ImportError:
import sys, termios #for linux/unix
termios.tcflush(sys.stdin, termios.TCIOFLUSH)
old_settings = termios.tcgetattr(sys.stdin)
argsparser = argparse.ArgumentParser(description="dummy generator")
argsparser.add_argument("-f","--fps",help="Frame Per Second",default=30,type=int)
argsparser.add_argument("-s","--speed",help="point per frame progress",default=3,type=int)
argsparser.add_argument("-v","--verbose",action="store_true",help="Verbose output")
args = argsparser.parse_args()
fps=args.fps
verbose=args.verbose
optimal_looptime = 1 / fps
#debug(name+" optimal looptime "+str(optimal_looptime))
width = 800
height = 800
white = 0xFFFFFF
blank = 0x0
seg1_length = 100
seg2_length = 100
ang = 50
incrLengthLittle= 5
incrLengthLot= 20
incrAngleLittle= 1
incrAngleLot= 10
angle_min = 0
angle_max = 90
length_min = 1
length_max = 350
shape = []
# angle
def ang_add_1():
global ang
if ang + incrAngleLittle <= angle_max:
ang += incrAngleLittle
else:
ang = angle_max
def ang_add_2():
global ang
if ang + incrAngleLot <= angle_max:
ang += incrAngleLot
else:
ang = angle_max
def ang_sub_1():
global ang
if ang - incrAngleLittle >= angle_min:
ang -= incrAngleLittle
else:
ang = angle_min
def ang_sub_2():
global ang
if ang - incrAngleLot >= angle_min:
ang -= incrAngleLot
else:
ang = angle_min
# seg1
def seg_1_add_1():
global seg1_length
if seg1_length + incrLengthLittle <= length_max:
seg1_length += incrLengthLittle
else:
seg1_length = length_max
def seg_1_add_2():
global seg1_length
if seg1_length + incrLengthLot <= length_max:
seg1_length += incrLengthLot
else:
seg1_length = length_max
def seg_1_sub_1():
global seg1_length
if seg1_length - incrLengthLittle >= length_min:
seg1_length -= incrLengthLittle
else:
seg1_length = length_min
def seg_1_sub_2():
global seg1_length
if seg1_length - incrLengthLot >= length_min:
seg1_length -= incrLengthLot
else:
seg1_length = length_min
# seg2
def seg_2_add_1():
global seg2_length
if seg2_length + incrLengthLittle <= length_max:
seg2_length += incrLengthLittle
else:
seg2_length = length_max
def seg_2_add_2():
global seg2_length
if seg2_length + incrLengthLot <= length_max:
seg2_length += incrLengthLot
else:
seg2_length = length_max
def seg_2_sub_1():
global seg2_length
if seg2_length - incrLengthLittle >= length_min:
seg2_length -= incrLengthLittle
else:
seg2_length = length_min
def seg_2_sub_2():
global seg2_length
if seg2_length - incrLengthLot >= length_min:
seg2_length -= incrLengthLot
else:
seg2_length = length_min
action = {
# segment 1
'q': seg_1_sub_1,
'w': ssg_1_add_1,
'a': seg_1_sub_2,
's': seg_1_add_2,
# segment 2
'o': seg_2_sub_1,
'p': seg_2_add_1,
'l': seg_2_sub_2,
';': seg_2_add_2,
# angle
't': ang_sub_1,
'y': ang_add_1,
'g': ang_sub_2,
'h': ang_add_2,
}
def print_param():
debug("\n\n===")
debug("segment 1 length:", seg1_length)
debug("segment 2 length:", seg2_length)
debug("angle:", ang)
def set_shape():
global shape
shape.clear()
cx = width / 2
cy = height / 2
px1 = int(seg1_length * math.cos(math.radians(ang)))
py1 = int(seg1_length * math.sin(math.radians(ang)))
px2 = int(seg2_length * math.cos(math.radians(ang)))
py2 = int(seg2_length * math.sin(math.radians(ang)))
# line up
shape.append([-px1 + cx, -py1 + cy, blank])
shape.append([-px1 + cx, -py1 + cy, white])
shape.append([ cx, + cy, white])
shape.append([ px2 + cx, -py2 + cy, white])
shape.append([ px2 + cx, -py2 + cy, blank])
# line down
shape.append([ px1 + cx, py1 + cy, blank])
shape.append([ px1 + cx, py1 + cy, white])
shape.append([ cx, + cy, white])
shape.append([-px2 + cx, py2 + cy, white])
shape.append([-px2 + cx, py2 + cy, blank])
def update_param(c):
if c in action:
action[c]()
print_param()
set_shape()
try:
tty.setcbreak(sys.stdin.fileno())
set_shape()
print_param()
print(shape, flush=True);
while 1:
if isData():
c = sys.stdin.read(1)
update_param(c)
start = time.time()
print(shape, flush=True);
looptime = time.time() - start
if( looptime < optimal_looptime ):
time.sleep( optimal_looptime - looptime)
#debug(name+" micro sleep:"+str( optimal_looptime - looptime))
finally:
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)