Here you can find the source of clamp(int value, int minValue, int maxValue)
Parameter | Description |
---|---|
value | value to be limited |
minValue | minimum value. Should be <= maxValue. |
maxValue | maximum value. Should be >= minValue. |
public static int clamp(int value, int minValue, int maxValue)
//package com.java2s; /*************************************************************************** * (C) Copyright 2003-2010 - Stendhal * *************************************************************************** *************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ public class Main { /**//w ww .j ava 2s .c o m * Limit an integer value to a specific range. * * @param value value to be limited * @param minValue minimum value. Should be <= maxValue. * @param maxValue maximum value. Should be >= minValue. * @return if value is in range [minValue, maxValue], value is returned. * Otherwise if value > maxValue, maxValue is returned, if value < minValue, * minValue is returned. */ public static int clamp(int value, int minValue, int maxValue) { return Math.max(minValue, Math.min(value, maxValue)); } /** * Limit a float value to a specific range. * * @param value value to be limited * @param minValue minimum value. Should be <= maxValue. * @param maxValue maximum value. Should be >= minValue. * @return if value is in range [minValue, maxValue], value is returned. * Otherwise if value > maxValue, maxValue is returned, if value < minValue, * minValue is returned. */ public static float clamp(float value, float minValue, float maxValue) { return Math.max(minValue, Math.min(value, maxValue)); } }