Last active
March 16, 2017 02:53
-
-
Save benblack769/05eba38e9d512dee5801f6acd6a12974 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
import random | |
class Vector: | |
def __init__(self,x,y): | |
self.x = x | |
self.y = y | |
def __add__(self,vec2): | |
return Vector(self.x+vec2.x,self.y+vec2.y) | |
def __neg__(self): | |
return Vector(-self.x,-self.y) | |
def __sub__(self,vec2): | |
return self + (-vec2) | |
def __mul__(self,scalar): | |
return Vector(self.x*scalar,self.y*scalar) | |
def direction(self): | |
return math.atan2(self.y,self.x) | |
def length(self): | |
return math.sqrt(self.x*self.x + self.y*self.y) | |
def __str__(self): | |
return "("+str(self.x)+","+str(self.y)+")" | |
def to_xy(dir,length): | |
return Vector(x=math.cos(dir),y=math.sin(dir))*length | |
def normalize(vec): | |
return vec*(1.0/vec.length()) | |
def next_dir(curp,prevp): | |
''' | |
inputs are 2 dimentional vectors | |
to_xy converts (length, dir) to (x,y), or polar to cartisian coordinates | |
''' | |
away_weight = 1.0 | |
rand_weight = 0.5 | |
away_vec = normalize(curp-prevp) *away_weight | |
#to_xy converts (length, dir) to | |
rand_vec = to_xy(rand_weight,random.random()*2*math.pi) | |
result_vec = away_vec + rand_vec | |
return result_vec.direction() | |
def next_point(curp,prevp): | |
ndir = next_dir(curp,prevp) | |
return curp+to_xy(ndir,1) | |
prev_p = Vector(2,-1) | |
cur_p = Vector(1,1) | |
for x in range(10): | |
new_p = next_point(cur_p,prev_p) | |
prev_p = cur_p | |
cur_p = new_p | |
print(cur_p) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment