Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.01 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. from matplotlib import rc
  3. import numpy as np
  4. from sys import argv
  5.  
  6. def hsv2rgb(h, s, v):
  7.     c = v*s
  8.     x = c * (1-abs((h/60)%2-1))
  9.     m = v - c
  10.     res = None
  11.     if h >= 0 and h < 60:
  12.         res = (c, x, 0)
  13.     elif 60 <= h and h < 120:
  14.         res = (x, c, 0)
  15.     elif 120 <= h and h < 180:
  16.         res = (0, c, x)
  17.     elif 180 <= h and h < 240:
  18.         res = (0, x, c)
  19.     elif 240 <= h and h < 300:
  20.         res = (x, 0, c)
  21.     elif 300 <= h and h <= 360:
  22.         res = (c, 0, x)
  23.     return (res[0] + m, res[1] + m, res[2] + m)
  24.  
  25. def normalize(vector):
  26.     magnitude = np.linalg.norm(vector)
  27.     return [x / magnitude for x in vector]
  28.  
  29.  
  30. if __name__ == '__main__':
  31.     f = open('big.dem')
  32.     data = f.readlines()
  33.  
  34.     width, height, distance = [int(x) for x in data[0].rstrip().split(' ')]
  35.    
  36.     map = [[float(cell) for cell in line.rstrip().split(' ')] for line in data[1:]]
  37.  
  38.     max = np.amax(map)
  39.  
  40.     #calculate normals
  41.     normals = []
  42.     for z in range(0, height):
  43.         normals.append([])
  44.         for x in range(0, width):
  45.             try:
  46.                 hl = map[z][x-1]
  47.                 hr = map[z][x+1]
  48.                 hd = map[z-1][x]
  49.                 hu = map[z+1][x]
  50.             except IndexError:
  51.                 #edge pixels, i don't care
  52.                 hl = 0
  53.                 hr = 0
  54.                 hd = 0
  55.                 hu = 0
  56.  
  57.             normals[z].append(normalize([hl - hr, 2.0, hd - hu]))
  58.        
  59.  
  60.     sun = normalize([0.5, 1.0, 0.0])
  61.    
  62.     base_brightness = 0.8
  63.     strength = 0.2
  64.  
  65.     hues = np.linspace(135, 0, 1024)
  66.     for z in range(0, len(map)):
  67.         for x in range(0, len(map[z])):
  68.             idx = int(((map[z][x] - 13) / max) * 1024) - 1
  69.             #get the dot product
  70.             cosine = np.dot(normals[z][x], sun)
  71.             cosine = cosine if cosine >= 0 else 0
  72.             map[z][x] = list(hsv2rgb(hues[idx], 1, base_brightness + strength * cosine))
  73.  
  74.     plt.imshow(map)
  75.     plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement