aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakobS1n <jakob@Jakobs-MacBook-Pro.local>2016-03-23 15:44:16 +0100
committerJakobS1n <jakob@Jakobs-MacBook-Pro.local>2016-03-23 15:45:17 +0100
commit3c5a62897cec1d612314d2943dd56f4e42526333 (patch)
tree9133719671c535bd9fe73bb73108a4ba409f6147
parent833957f25ea48401fcee38d3289712d4ac9ff1dc (diff)
downloadmath-draw-3c5a62897cec1d612314d2943dd56f4e42526333.tar.gz
math-draw-3c5a62897cec1d612314d2943dd56f4e42526333.zip
Code cleanup
cleanup + added function for command line parameters
-rw-r--r--math-draw.py165
1 files changed, 94 insertions, 71 deletions
diff --git a/math-draw.py b/math-draw.py
index 9ea0d3d..aedf7ed 100644
--- a/math-draw.py
+++ b/math-draw.py
@@ -7,6 +7,8 @@
import turtle
import math
import sys
+import getopt
+import random
def setup():
global startPosition # Et referansepunkt for sentrum + radius
@@ -22,6 +24,9 @@ def setup():
global animation # Boolean for animajon paa/av
global dots # Boolean for prikker paa sirkelbuen paa/av
global tracer # Boolean for linjetegning paa/av
+ global GlobalColor # Farge paa tegning
+ global debug # Boolean for debug
+ global rotation # Default rotation
center = (0, 0)
radiusInt = 375
@@ -31,6 +36,9 @@ def setup():
animation = True
dots = False
tracer = False
+ GlobalColor = "black"
+ debug = False
+ rotation = 90
wn = turtle.Screen()
wheel = turtle.Turtle()
@@ -46,12 +54,12 @@ def drawCircle(anyTurtle, radius):
sideLength = circumference / 360
drawPolygon(anyTurtle, sideLength, 360)
-def calculatePoints(center, radius, intPoints, t, dots, update):
+def calculatePoints(center, radius, intPoints, t, dots, update, angle):
global differAngle
fullCircle = 360
differAngle = float(fullCircle) / float(pointsInt)
- currDegree = 90 # sett til 90 for aa starte fra toppen
+ currDegree = angle # sett til 90 for aa starte fra toppen
for i in xrange(pointsInt):
t.setx(0)
@@ -69,11 +77,12 @@ def calculatePoints(center, radius, intPoints, t, dots, update):
if update:
turtle.update()
- print t.pos()
+ if debug:
+ print t.pos()
pointsArray[i][0] = t.pos()[0]
pointsArray[i][1] = t.pos()[1]
-def drawChord(t, update, animation):
+def drawChord(t, update, animation, color):
global currentHeading
currentPoint = 1
@@ -82,8 +91,18 @@ def drawChord(t, update, animation):
if animation:
t.tracer(True)
+ else:
+ t.tracer(False)
for i in pointsArray:
+
+ if color == "random":
+ r = lambda: random.randint(0,255)
+ randColor = ('#%02X%02X%02X' % (r(),r(),r()))
+ wheel.color(randColor)
+ else:
+ wheel.color(color)
+
nextPoint = currentPoint*multiplier
while True:
@@ -148,10 +167,17 @@ def drawStatusText():
wheel.write("Tracer: " + str(tracer))
wheel.penup()
-def drawThingy():
+def clearScreen():
wheel.clear() # Clear out the drawing (if any)
wheel.reset()
+def drawThingy(radius, multiplier, points, animation, drawAnimation, rotation, color, clear):
+ # Skru av tegneAnimasjon
+ wheel.tracer(False)
+
+ if clear:
+ clearScreen()
+
# Faa sirkelen til aa starte aa tegnes radius fra sentrum
wheel.penup()
wheel.sety(radiusInt)
@@ -166,101 +192,56 @@ def drawThingy():
drawCircle(wheel, radiusInt)
wheel.penup()
- # Calculate points
+ # Kalkuler punkter
if dots:
- calculatePoints(center, radiusInt, pointsInt, wheel, True, False)
+ calculatePoints(center, radius, points, wheel, True, False, rotation)
else:
- calculatePoints(center, radiusInt, pointsInt, wheel, False, False)
+ calculatePoints(center, radius, points, wheel, False, False, rotation)
# tegn korder
if animation:
- if tracer:
- drawChord(wheel, True, True)
+ if drawAnimation:
+ drawChord(wheel, True, True, color)
else:
- drawChord(wheel, True, False)
+ drawChord(wheel, True, False, color)
else:
- if tracer:
- drawChord(wheel, False, True)
+ if drawAnimation:
+ drawChord(wheel, False, True, color)
else:
- drawChord(wheel, False, False)
+ drawChord(wheel, False, False, color)
- # Skjul skilpadden
wheel.hideturtle()
-
- # Refresh screen
turtle.update()
- #wn.exitonclick()
turtle.done()
def main():
+ drawThingy(radiusInt, multiplier, pointsInt, animation, tracer, rotation, GlobalColor, True)
- # Skru av tegne-animasjon
- wheel.tracer(False)
-
- # Faa sirkelen til aa starte aa tegnes radius fra sentrum
- wheel.penup()
- wheel.sety(radiusInt)
- startPosition = wheel.pos()
-
- # Tegn teksten
- drawStatusText()
-
- # Tegn sirkelen
- wheel.goto(startPosition)
- wheel.pendown()
- drawCircle(wheel, radiusInt)
- wheel.penup()
-
- # Calculate points
- if dots:
- calculatePoints(center, radiusInt, pointsInt, wheel, True, False)
- else:
- calculatePoints(center, radiusInt, pointsInt, wheel, False, False)
-
- # tegn korder
- if animation:
- if tracer:
- drawChord(wheel, True, True)
- else:
- drawChord(wheel, True, False)
- else:
- if tracer:
- drawChord(wheel, True, True)
- else:
- drawChord(wheel, True, False)
-
- # Skjul skilpadden
- wheel.hideturtle()
-
- # Refresh screen
- turtle.update()
- #wn.exitonclick()
- turtle.done()
def arrowUpKeyPress():
global multiplier
multiplier = multiplier + 1 # Steps paa 1
- drawThingy()
+ drawThingy(radiusInt, multiplier, pointsInt, animation, tracer, rotation, GlobalColor, True)
def arrowDownKeyPress():
global multiplier
multiplier = multiplier - 1 # Steps paa 1
- drawThingy()
+ drawThingy(radiusInt, multiplier, pointsInt, animation, tracer, rotation, GlobalColor, True)
def arrowRightKeyPress():
global pointsInt
if pointsInt <= 650:
pointsInt = pointsInt +50 # Steps paa 50
- drawThingy()
+ drawThingy(radiusInt, multiplier, pointsInt, animation, tracer, rotation, GlobalColor, True)
def arrowLeftKeyPress():
global pointsInt
if pointsInt >= 100:
pointsInt = pointsInt - 50 # Steps paa 50
- drawThingy()
+ drawThingy(radiusInt, multiplier, pointsInt, animation, tracer, rotation, GlobalColor, True)
def keyExit():
sys.exit()
@@ -269,31 +250,73 @@ def toggleDots():
global dots
if dots:
dots = False
- drawThingy()
+ drawThingy(radiusInt, multiplier, pointsInt, animation, tracer, rotation, GlobalColor, True)
else:
dots = True
- drawThingy()
+ drawThingy(radiusInt, multiplier, pointsInt, animation, tracer, rotation, GlobalColor, True)
def toggleAnimation():
global animation
if animation:
animation = False
- drawThingy()
+ drawThingy(radiusInt, multiplier, pointsInt, animation, tracer, rotation, GlobalColor, True)
else:
animation = True
- drawThingy()
+ drawThingy(radiusInt, multiplier, pointsInt, animation, tracer, rotation, GlobalColor, True)
def toggleTracer():
global tracer
if tracer:
tracer = False
- drawThingy()
+ drawThingy(radiusInt, multiplier, pointsInt, animation, tracer, rotation, GlobalColor, True)
else:
tracer = True
- drawThingy()
+ drawThingy(radiusInt, multiplier, pointsInt, animation, tracer, rotation, GlobalColor, True)
+def readCommanlineParameters():
+
+ options, remainder = getopt.getopt(sys.argv[1:], 'r:m:p:a:t:h:c', ['radius=', 'multiplier=', 'points=', 'animation=', 'tracer=', 'heading=', 'color=', 'random', 'debug', ])
+
+ for opt, arg in options:
+ if opt in ('-r', '--radius'):
+ global radiusInt
+ radiusInt = int(arg)
+ elif opt in ('-m', '--multiplier'):
+ global multiplier
+ multiplier = int(arg)
+ elif opt in ('-p', '--points'):
+ global points
+ points = arg
+ elif opt in ('-a', '--animation'):
+ global animation
+ if arg == "yes":
+ animation = True
+ else:
+ animation = False
+ elif opt in ('-t', '--tracer'):
+ global tracer
+ if arg == "yes":
+ tracer = True
+ else:
+ tracer = False
+ elif opt in ('-h', '--heading'):
+ global rotation
+ rotation = int(arg)
+ elif opt in ('-c', '--color'):
+ global GlobalColor
+ GlobalColor = str(arg)
+ elif opt == '--random':
+ global GlobalColor
+ GlobalColor = "random"
+ elif opt == '--debug':
+ global debug
+ debug = True
+
+
+# Definer alle variabler
setup()
+# Sett opp listnere paa alle tastene
wn.onkey(arrowUpKeyPress, "Up") # increase multiplier
wn.onkey(arrowDownKeyPress, "Down") # decrease multiplier
wn.onkey(arrowRightKeyPress, "Right")
@@ -302,8 +325,8 @@ wn.onkey(wn.bye, "q")
wn.onkey(toggleDots, "d")
wn.onkey(toggleAnimation, "a")
wn.onkey(toggleTracer, "t")
-
wn.listen()
+readCommanlineParameters()
main()