실제로 위의 답변이 중단되는 경우가 있습니다. 잘못 입력 된 값, 잘못 입력 된 범위, 음수 입/출력 범위 등.
def remap(x, oMin, oMax, nMin, nMax):
#range check
if oMin == oMax:
print "Warning: Zero input range"
return None
if nMin == nMax:
print "Warning: Zero output range"
return None
#check reversed input range
reverseInput = False
oldMin = min(oMin, oMax)
oldMax = max(oMin, oMax)
if not oldMin == oMin:
reverseInput = True
#check reversed output range
reverseOutput = False
newMin = min(nMin, nMax)
newMax = max(nMin, nMax)
if not newMin == nMin :
reverseOutput = True
portion = (x-oldMin)*(newMax-newMin)/(oldMax-oldMin)
if reverseInput:
portion = (oldMax-x)*(newMax-newMin)/(oldMax-oldMin)
result = portion + newMin
if reverseOutput:
result = newMax - portion
return result
#test cases
print remap(25.0, 0.0, 100.0, 1.0, -1.0), "==", 0.5
print remap(25.0, 100.0, -100.0, -1.0, 1.0), "==", -0.25
print remap(-125.0, -100.0, -200.0, 1.0, -1.0), "==", 0.5
print remap(-125.0, -200.0, -100.0, -1.0, 1.0), "==", 0.5
#even when value is out of bound
print remap(-20.0, 0.0, 100.0, 0.0, 1.0), "==", -0.2
는 모두 감사 , 나는 간다. g는 첫 번째로 들어 왔기 때문에 cletus에 대한 대답이고, 나의 followup에 대답하기 위해 +1로 제리에게 대답했다. – SpliFF
실제 미안해. 크레타스. 새롭고 점수가 필요하기 때문에 제리에게주고있다. – SpliFF
안녕하세요 노년교입니다! Heheh, j/k, 걱정 마라. :) – cletus