Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. from math import floor
  2.  
  3. def get_rgb_intensity(canvas, color, bg_color, intensity):
  4. grad = []
  5. (r1, g1, b1) = canvas.winfo_rgb(color)
  6. (r2, g2, b2) = canvas.winfo_rgb(bg_color)
  7. r_ratio = float(r2 - r1) / intensity
  8. g_ratio = float(g2 - g1) / intensity
  9. b_ratio = float(b2 - b1) / intensity
  10. for i in range(intensity):
  11. nr = int(r1 + (r_ratio * i))
  12. ng = int(g1 + (g_ratio * i))
  13. nb = int(b1 + (b_ratio * i))
  14. grad.append("#%4.4x%4.4x%4.4x" % (nr, ng, nb))
  15. grad.reverse()
  16. return grad
  17.  
  18. def draw_wu(canvas, ps, pf, fill, bg_color):
  19. x1 = ps[0]
  20. x2 = pf[0]
  21. y1 = ps[1]
  22. y2 = pf[1]
  23. I = 100
  24. stairs = []
  25. fills = get_rgb_intensity(canvas, fill, bg_color, I)
  26. if x1 == x2 and y1 == y2:
  27. canvas.create_line(x1, y1, x1 + 1, y1 + 1, fill = fills[100])
  28.  
  29. steep = abs(y2 - y1) > abs(x2 - x1)
  30.  
  31. if steep:
  32. x1, y1 = y1, x1
  33. x2, y2 = y2, x2
  34. if x1 > x2:
  35. x1, x2 = x2, x1
  36. y1, y2 = y2, y1
  37.  
  38. dx = x2 - x1
  39. dy = y2 - y1
  40.  
  41. if dx == 0:
  42. tg = 1
  43. else:
  44. tg = dy / dx
  45.  
  46. # first endpoint
  47. xend = round(x1)
  48. yend = y1 + tg * (xend - x1)
  49. xpx1 = xend
  50. y = yend + tg
  51.  
  52. # second endpoint
  53. xend = int(x2 + 0.5)
  54. xpx2 = xend
  55. st = 0
  56.  
  57. # main loop
  58. if steep:
  59. for x in range(xpx1, xpx2):
  60. canvas.create_line(int(y), x + 1, int(y) + 1, x + 2,
  61. fill = fills[int((I - 1) * (abs(1 - y + int(y))))])
  62. canvas.create_line(int(y) + 1, x + 1, int(y) + 2, x + 2,
  63. fill = fills[int((I - 1) * (abs(y - int(y))))])
  64.  
  65. if (abs(int(x) - int(x + 1)) >= 1 and tg > 1) or \
  66. (not 1 > abs(int(y) - int(y + tg)) >= tg):
  67. stairs.append(st)
  68. st = 0
  69. else:
  70. st += 1
  71. y += tg
  72. else:
  73. for x in range(xpx1, xpx2):
  74. #print((I - 1)*round(abs(1 - y + floor(y))))
  75. canvas.create_line(x + 1, int(y), x + 2, int(y) + 1,
  76. fill = fills[round((I - 1) * (abs(1 - y + floor(y))))])
  77. #print((I - 1)*round(abs(y - floor(y))))
  78. canvas.create_line(x + 1, int(y) + 1, x + 2, int(y) + 2,
  79. fill = fills[round((I - 1) * (abs(y - floor(y))))])
  80.  
  81. if (abs(int(x) - int(x + 1)) >= 1 and tg > 1) or \
  82. (not 1 > abs(int(y) - int(y + tg)) >= tg):
  83. stairs.append(st)
  84. st = 0
  85. else:
  86. st += 1
  87. y += tg
  88. return stairs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement