我正在制作具有无限滚动和常规分页功能的RESTful API。所以我想提供[ElementsCount,TotalPages,Skip(或Offset),Limit,hasMoreElements?,“ ActualPage”)。
我需要计算“页面数,限制数和跳过数”的“实际页面”
我尝试了这个: 举个例子。我没有对数字进行硬编码...
import numpy as np
def raytrace(v0, v1):
# The equation of the ray is v = v0 + t*d
d = v1 - v0
inc = np.sign(d) # Gives the quadrant in which the ray progress
# Rounding coordinates give us the current tile
tile = np.array(np.round(v0), dtype=int)
tiles = [tile]
v = v0
endtile = np.array(np.round(v1), dtype=int)
# Continue as long as we are not in the last tile
while np.max(np.abs(endtile - v)) > 0.5:
# L = (Lx, Ly) where Lx is the x coordinate of the next vertical
# line and Ly the y coordinate of the next horizontal line
L = tile + 0.5*inc
# Solve the equation v + d*t == L for t, simultaneously for the next
# horizontal line and vertical line
t = (L - v)/d
if t[0] < t[1]: # The vertical line is intersected first
tile[0] += inc[0]
v += t[0]*d
else: # The horizontal line is intersected first
tile[1] += inc[1]
v += t[1]*d
tiles.append(tile)
return tiles
但是当我更改totalElements时,此公式不起作用
ElemetsCount = 17
Limit = 4
Pages = 5
Skip = 8
actualPage = skip + limit / pages