flip flop chain sketch binary counter

This commit is contained in:
Xevion
2019-10-15 13:24:42 -05:00
parent 59aff8e609
commit 3aa7f92c35
3 changed files with 64 additions and 1 deletions

View File

@@ -0,0 +1,59 @@
'''
:param int length: The number of objects to be simulated.
:param float objPad: Space between each object. Will constrict the diameter of nearby objects
:param float sidePad: The padding between where the objects are rendered and the leftmost and rightmost sides of the application
:param float objDelta: The space between the center-y of the application that a object takes when it flips from on to off states
'''
class FlipFlopChain:
def __init__(self, length, objPad, sidePad, objDelta):
self.length = length
self.objPad = objPad
self.sidePad = sidePad
self.objDelta = objDelta
self.bools = [False for _ in range(length)]
objSpace = (width - (2 * self.sidePad)) - (self.length - 1) * self.objPad
self.objDiameter = objSpace / float(self.length)
self.objRadius = self.objDiameter / 2.0
def flip(self, i):
if i >= len(self.bools) - 1:
self.bools = [False for _ in range(len(self.bools))]
print('Completed one rotation')
return True
else:
self.bools[i] ^= True
def get(self, i):
return self.bools[i]
def tick(self, i=1):
place = 0
self.flip(place)
while not self.get(place):
place += 1
if self.flip(place):
break
def render(self):
translate(self.sidePad, height / 2.0)
for boolValue in self.bools:
translate(self.objRadius, 0)
if boolValue:
ellipse(0, -1 * self.objDelta, self.objDiameter, self.objDiameter)
else:
ellipse(0, self.objDelta, self.objDiameter, self.objDiameter)
translate(self.objRadius + self.objPad, 0)
def setup():
size(1500, 300)
global flipflopchain
flipflopchain = FlipFlopChain(30, 10, 45, 100)
def draw():
background(204)
global flipflopchain
for _ in range(100):
flipflopchain.tick()
flipflopchain.render()

View File

@@ -0,0 +1,2 @@
mode=Python
mode.id=jycessing.mode.PythonMode

View File

@@ -13,3 +13,5 @@ Other sketches without any specific purpose. If enough fit into a specific categ
- **DVD Logo** - A fun little sketch displaying the iconic bouncing DVD Logo. May need a little more work to better visualize true-to-heart.
- **DynamicLines** - A very simple line drawing application.
- **FlipFlopChain** - A simple project displaying how one would count in binary.