Advertisement
UnitBus

getAroundPixelValues

Jun 15th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.81 KB | None | 0 0
  1. def getCoordPixel(pixels, width, height, index, x=0, y=0):
  2.     bx = (index % width) + x
  3.     by = (index // height) + y
  4.     ex = -1 < bx < width and -1 < by < height
  5.     indexB = index + x + (y * width) if ex else None
  6.     pixelB = pixels[indexB] if indexB else None
  7.     return pixelB
  8.  
  9. index = 8
  10. width = 4
  11. height = 4
  12. pixels = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
  13.  
  14. print '[ \][x0][x1][x2][x3]'
  15. print '[y0]   0,  1,  2,  3'
  16. print '[y1]   4,  5,  6,  7'
  17. print '[y2]   8,  9, 10, 11'
  18. print '[y3]  12, 13, 14, 15'
  19.  
  20. print ''
  21. print 'index:', index
  22. print 'R=Right, B=Bottom, L=Left, T=Top'
  23. print '   R:', getCoordPixel(pixels, width, height, index, x= 1, y= 0) # 9
  24. print '   B:', getCoordPixel(pixels, width, height, index, x= 0, y= 1) # 12
  25. print '   L:', getCoordPixel(pixels, width, height, index, x=-1, y= 0) # None
  26. print '   T:', getCoordPixel(pixels, width, height, index, x= 0, y=-1) # 4
  27. print '  TR:', getCoordPixel(pixels, width, height, index, x= 1, y=-1) # 5
  28. print '  BR:', getCoordPixel(pixels, width, height, index, x= 1, y= 1) # 13
  29. print '  BL:', getCoordPixel(pixels, width, height, index, x=-1, y= 1) # None
  30. print '  TL:', getCoordPixel(pixels, width, height, index, x=-1, y=-1) # None
  31. print '  RR:', getCoordPixel(pixels, width, height, index, x= 2, y= 0) # 10
  32. print '  BB:', getCoordPixel(pixels, width, height, index, x= 0, y= 2) # None
  33. print '  LL:', getCoordPixel(pixels, width, height, index, x=-2, y= 0) # None
  34. print '  TT:', getCoordPixel(pixels, width, height, index, x= 0, y=-2) # 0
  35. print 'TTRR:', getCoordPixel(pixels, width, height, index, x= 2, y=-2) # 2
  36. print 'BBRR:', getCoordPixel(pixels, width, height, index, x= 2, y= 2) # None
  37. print 'BBLL:', getCoordPixel(pixels, width, height, index, x=-2, y= 2) # None
  38. print 'TTLL:', getCoordPixel(pixels, width, height, index, x=-2, y=-2) # None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement