Skip to content

Instantly share code, notes, and snippets.

@petfactory
Created July 31, 2015 13:10
Show Gist options
  • Save petfactory/8e00c525e773ffac9431 to your computer and use it in GitHub Desktop.
Save petfactory/8e00c525e773ffac9431 to your computer and use it in GitHub Desktop.
import math
def fit_center_rect(total_length, item_length):
ret_list = []
float_fit = float(total_length)/item_length
int_fit = int(math.ceil(float_fit))
# add one to the count if we have an even number
if int_fit % 2 == 0:
int_fit += 1
# calculate the protrusion beyong the edges
protrusion = 0
if int_fit > 1:
protrusion = ((int_fit * item_length) - total_length)*.5
for i in range(int_fit):
p1 = max(0, (i * item_length - protrusion))
p2 = min(total_length, (p1 + item_length))
if i == 0:
p2 -= protrusion
ret_list.append((p1, p2))
return ret_list
w = 400
h = 300
rw = 100
rh = 210
px_list = fit_center_rect(w, rw)
print(px_list)
py_list = fit_center_rect(h, rh)
print(py_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment