Here you can find the source of clamp(float v, float min, float max)
Parameter | Description |
---|---|
v | is the value to clamp. |
min | is the min value of the range. |
max | is the max value of the range. |
public static float clamp(float v, float min, float max)
//package com.java2s; /* //from w w w .j av a 2 s. c o m * $Id$ * * Copyright (c) 2011-15 Stephane GALLAND <stephane.galland@utbm.fr>. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * This program is free software; you can redistribute it and/or modify */ public class Main { /** Clamp the given value to the given range. * <p> * If the value is outside the {@code [min;max]} * range, it is clamp to the nearest bounding value * <var>min</var> or <var>max</var>. * * @param v is the value to clamp. * @param min is the min value of the range. * @param max is the max value of the range. * @return the value in {@code [min;max]} range. */ public static float clamp(float v, float min, float max) { if (min < max) { if (v < min) return min; if (v > max) return max; } else { if (v > min) return min; if (v < max) return max; } return v; } }