Created
July 31, 2015 13:10
-
-
Save petfactory/8e00c525e773ffac9431 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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