#! /usr/bin/env python """ Garbagepatch.py is a program which simply simulates random floating of garbage in a currentless body of water. Paul Eugenio Florida State University Mar 23, 2017 """ from __future__ import division, print_function import numpy as np from random import randrange import matplotlib.pyplot as plt class Garbage(): """ Garbage is an object which simulates the random floating of trash in a currentless ocean """ def __init__(self, x=0, y=0): """ Each piece of garbage has an (x,y) position. """ self.x, self.y = x, y def move(self, x_increment=0, y_increment=0): """ Move the garbage according to the parameters given. Default behavior is to stay put """ self.x, self.y = self.x + x_increment, self.y + y_increment def get_distance(self, other): """ Calculates the distance from this piece to another piece, and returns that value. """ return np.sqrt( (self.x - other.x)**2 + (self.y - other.y )**2 ) def float(self): """ random floating movement: moves one unit East, North, West, South, or stays put """ direction = np.random.randint(5) dx,dy = 0,0 # default for direction == 0 if direction == 1: dx = 1 elif direction == 2: dy = 1 elif direction == 3: dx = -1 elif direction == 4: dy = -1 self.move(dx,dy) # # main # # Create some trash garbagePatch = [] for k in range(60): garbagePatch += [Garbage()] # time interval max_t = 1000 tRange = range(max_t) # plot the location of the trash as it randomly floats for trash in garbagePatch: x,y = [],[] for t in tRange: trash.float() x += [trash.x] y += [trash.y] plt.plot(x,y,"o") plt.title("Random movement of a 60 element garbage patch") plt.savefig("garbagePatch.jpg") plt.show()