# PixelConfigurationFinder

a guest
Jul 27th, 2020
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import math
2.
3. """variables"""
4. MAX_DISTANCE = 5.0
5. PRECISION = 0.01
6. results = [] #list of (int,int,float) ordered by delta
7.
8.
9. """insert in results"""
10. def insert(L, px, pz, delta):
11.     i=len(L)
12.     L.append((px,pz,delta))
13.     while i>0 and L[i-1][2] > delta:
14.         L[i-1],L[i] = L[i],L[i-1] #swap
15.         i = i-1
16.
17.
18. """calculations - efficient algorithm
19. Visually, this algorithm plots all points of integer coordinates within a circular ring"""
20.
21. inf = MAX_DISTANCE-PRECISION
22. sup = MAX_DISTANCE
23. step_inf = math.ceil(16*(math.sqrt(inf*inf/2)+0.6))
24. step_sup = math.floor(16*(math.sqrt(sup*sup/2)+0.6))
25.
26. #linear setups (px=0)
27. min_range_0 = math.ceil(16*(inf + 0.6))
28. max_range_0 = math.floor(16*(sup + 0.6))
29. for px in range(min_range_0, max_range_0+1):
30.     x = (px/16)-0.6
31.     delta = MAX_DISTANCE-x
32.     insert(results,px,0,delta)
33.
34. #px between minimum (10pixels) and step_inf-1.
35. for px in range(10, step_inf):
36.     x = (px/16)-0.6
37.     min_range = math.ceil(16*(math.sqrt(max(inf*inf - x*x,0.0))+0.6))
38.     max_range = math.floor(16*(math.sqrt(max(sup*sup - x*x,0.0))+0.6))
39.
40.     for pz in range(min_range, max_range+1):
41.         z = (pz/16)-0.6
42.         distance = math.sqrt(x*x + z*z)
43.         delta = MAX_DISTANCE-distance
44.         insert(results,pz,px,delta)
45.
46. #px between step_inf and step_sup. extra precaution is taken in case px>pz. otherwise identical to above code
47. for px in range(step_inf, step_sup + 1):
48.     x = (px/16)-0.6
49.     min_range = math.ceil(16*(math.sqrt(max(inf*inf - x*x,0.0))+0.6))
50.     max_range = math.floor(16*(math.sqrt(max(sup*sup - x*x,0.0))+0.6))
51.
52.     for pz in range(min_range, max_range+1):
53.         if px>pz: continue
54.         z = (pz/16)-0.6
55.         distance = math.sqrt(x*x + z*z)
56.         delta = MAX_DISTANCE-distance
57.         insert(results,pz,px,delta)
58.
59.
60. """results display"""
61. for X,Z,DELTA in results:
62.     print("X: %3d \t Z: %3d \t delta: %.6f" % (X,Z,DELTA))
63.
64. """alternative display, showing distance instead of delta"""
65. #for X,Z,DELTA in results[::-1]:
66. #    print("X: %3d \t Z: %3d \t distance: %.6f" % (X,Z,MAX_DISTANCE-DELTA))