Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import matplotlib.pyplot as plt
- from matplotlib import rc
- import numpy as np
- from sys import argv
- def hsv2rgb(h, s, v):
- c = v*s
- x = c * (1-abs((h/60)%2-1))
- m = v - c
- res = None
- if h >= 0 and h < 60:
- res = (c, x, 0)
- elif 60 <= h and h < 120:
- res = (x, c, 0)
- elif 120 <= h and h < 180:
- res = (0, c, x)
- elif 180 <= h and h < 240:
- res = (0, x, c)
- elif 240 <= h and h < 300:
- res = (x, 0, c)
- elif 300 <= h and h <= 360:
- res = (c, 0, x)
- return (res[0] + m, res[1] + m, res[2] + m)
- def normalize(vector):
- magnitude = np.linalg.norm(vector)
- return [x / magnitude for x in vector]
- if __name__ == '__main__':
- f = open('big.dem')
- data = f.readlines()
- width, height, distance = [int(x) for x in data[0].rstrip().split(' ')]
- map = [[float(cell) for cell in line.rstrip().split(' ')] for line in data[1:]]
- max = np.amax(map)
- #calculate normals
- normals = []
- for z in range(0, height):
- normals.append([])
- for x in range(0, width):
- try:
- hl = map[z][x-1]
- hr = map[z][x+1]
- hd = map[z-1][x]
- hu = map[z+1][x]
- except IndexError:
- #edge pixels, i don't care
- hl = 0
- hr = 0
- hd = 0
- hu = 0
- normals[z].append(normalize([hl - hr, 2.0, hd - hu]))
- sun = normalize([0.5, 1.0, 0.0])
- base_brightness = 0.8
- strength = 0.2
- hues = np.linspace(135, 0, 1024)
- for z in range(0, len(map)):
- for x in range(0, len(map[z])):
- idx = int(((map[z][x] - 13) / max) * 1024) - 1
- #get the dot product
- cosine = np.dot(normals[z][x], sun)
- cosine = cosine if cosine >= 0 else 0
- map[z][x] = list(hsv2rgb(hues[idx], 1, base_brightness + strength * cosine))
- plt.imshow(map)
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement