Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.direnv/
__pycache__/
.DS_Store
Binary file added color-3492041321645561828-0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added color-8529397126331828772-0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added color-8529397126331828772-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added color-8529397126331828772-10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added color-8529397126331828772-11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added color-8529397126331828772-12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added color-8529397126331828772-13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added color-8529397126331828772-14.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added color-8529397126331828772-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added color-8529397126331828772-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added color-8529397126331828772-4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added color-8529397126331828772-5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added color-8529397126331828772-6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added color-8529397126331828772-7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added color-8529397126331828772-8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added color-8529397126331828772-9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 49 additions & 0 deletions perlin_noise.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import math
import random
from math import pi


# returns the unit vector of angle
def unitVector(angle):
return (math.cos(angle), math.sin(angle))


# needs to be extended for any size vector
def dotProduct(point1, point2):
return point1[0] * point2[0] + point1[1] * point2[1]


# interp uses a cubic function to perform a weighted average of a0 and a1
def interp(a0, a1, w):
weight = 3*math.pow(w, 2) - 2*math.pow(w, 3)
return a0*(1-weight) + a1*weight


# angles provides the four gradient angles for the grid in a tuple
# starting from top left then going clockwise
# coords provides the (x, y) coordinates on the grid, assuming top left is (0, 0)
def perlinNoise(coords, gridSize, angles):
topLeftGradient = unitVector(angles[0])
topRightGradient = unitVector(angles[1])
bottomRightGradient = unitVector(angles[2])
bottomLeftGradient = unitVector(angles[3])
coords = (((coords[0] + 1) * 1000) % gridSize,
((coords[1] + 1) * 1000) % gridSize)
topLeftVector = coords
topRightVector = (gridSize - coords[0], coords[1])
bottomRightVector = (gridSize - coords[0], gridSize - coords[1])
bottomLeftVector = (coords[0], gridSize - coords[1])
topLeft = dotProduct(topLeftGradient, topLeftVector)
topRight = dotProduct(topRightGradient, topRightVector)
bottomRight = dotProduct(bottomRightGradient, bottomRightVector)
bottomLeft = dotProduct(bottomLeftGradient, bottomLeftVector)
topAverage = interp(topLeft, topRight, coords[0]/gridSize)
bottomAverage = interp(bottomLeft, bottomRight, coords[0]/gridSize)
average = interp(topAverage, bottomAverage, coords[1]/gridSize)
return average/gridSize


if __name__ == "__main__":
print(perlinNoise((.002, .235), 350, (random.uniform(0, 2*pi),
random.uniform(0, 2*pi), random.uniform(0, 2*pi),
random.uniform(0, 2*pi))))
Binary file removed random1.png
Binary file not shown.
49 changes: 47 additions & 2 deletions random_art.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import random

from math import pi, sin, cos, tan, atan, acos, asin
import perlin_noise as pn
# Your job is to create better version of create_expression and
# run_expression to create random art.
# Your expression should have a __str__() function defined for it.
Expand All @@ -8,7 +9,34 @@
def create_expression():
"""This function takes no arguments and returns an expression that
generates a number between -1.0 and 1.0, given x and y coordinates."""
expr = lambda x, y: (random.random() * 2) - 1
angles1 = generate_angles()
angles2 = generate_angles()
angles3 = generate_angles()
grid_size1 = generate_grid_size()
grid_size2 = generate_grid_size()
grid_size3 = generate_grid_size()
offset_1x = random.random()
offset_1y = random.random()
offset_2x = random.random()
offset_2y = random.random()
trig1 = trig_func()
trig2 = trig_func()
trig3 = trig_func()
func1 = f()
func2 = f()
func3 = f()
func4 = f()
func5 = f()

def expr(x, y):
value1 = trig2(pi * func5(trig1(func1(pn.perlinNoise((x + offset_1x,
y + offset_1y), grid_size1, angles1)) * pi)))
value2 = trig2(func3(pn.perlinNoise((x + offset_2x, y + offset_2y),
grid_size2, angles2) * pi))
value3 = trig1(func2(pn.perlinNoise((x + offset_2y, y + offset_1x),
grid_size3, angles3)) * pi)
return trig2(pi * func4(trig3(pi * func2(value1 *
value2 - (value3 + x * y)))))
return expr


Expand All @@ -17,3 +45,20 @@ def run_expression(expr, x, y):
an x and y value. It runs the expression, passing the x and y values
to it and returns a value between -1.0 and 1.0."""
return expr(x, y)


def generate_angles():
return (random.uniform(0, 2*pi), random.uniform(0, 2*pi),
random.uniform(0, 2*pi), random.uniform(0, 2*pi))


def generate_grid_size():
return random.randint(2500, 4500)


def trig_func():
return random.choice([sin, cos])


def f():
return random.choice([sin, tan, cos, atan])