Monday, August 13, 2012

Autosquiggle

I had little to do today, so I wrote come code to doodle for me:




I'm not sure why you would want to do this, but here's my code in case you want to try it out.  Play with the parameters to make different squiggles.

import matplotlib.pyplot as plt
import numpy as np

#######################################################################
def randangle(prevangle,variance):
    nextangle=np.random.normal(prevangle,variance)
    return(nextangle)
########################################################################
def thetagenerator(count):
    alltheangles=[]
    variance=3.0                #Can change this
    alltheangles.append(10.0)    #Can change this
    angcount=1
   
    while angcount<count:
        newangle=randangle(alltheangles[angcount-1],variance)
        if newangle>360:
            newangle=newangle%360
        alltheangles.append(newangle)
        angcount+=1

    return(alltheangles)
#######################################################################
       
totpoints=5000                    #Can change this
anglecount=0
x=[]
y=[]
x.append(0)        #First point
y.append(0)        #First point
radtodeg=360.0/(2*3.14159265359)
degtorad=1.0/radtodeg

segmentlength=0.01                #Can change this

angles=thetagenerator(totpoints)

points=1    #First point at (0,0)

while points<totpoints:
    currentangle=sum(angles[:points])%360
   
    x.append(x[points-1]+(segmentlength*np.cos(degtorad*currentangle)))
    y.append(y[points-1]+(segmentlength*np.sin(degtorad*currentangle)))
   
    points+=1
   
fig1=plt.figure(figsize=(10,10))    #Can change this
sub1=fig1.add_subplot(111,frameon=False,aspect="equal")
sub1.tick_params(axis='both',which='both',length=0, width=0, labelbottom=False,labeltop=False,labelleft=False,labelright=False)
sub1.plot(x,y)
plt.show()


No comments:

Post a Comment