Here you can find the source of clamp(int n, int min, int max)
Parameter | Description |
---|---|
n | the original number |
min | the minimum value of the interval |
max | the maximum value of the interval |
public static int clamp(int n, int min, int max)
//package com.java2s; //License from project: Open Source License public class Main { /**/*from w w w .ja v a 2s .c o m*/ * Restrains a number in a interval * @param n the original number * @param min the minimum value of the interval * @param max the maximum value of the interval * @return a valid number inside the said interval. If min > max, then the interval will be reversed */ public static int clamp(int n, int min, int max) { if (min > max) return clamp(n, max, min); if (n < min) n = min; if (n > max) n = max; return n; } /** * Restrains a number in a interval * @param n the original number * @param min the minimum value of the interval * @param max the maximum value of the interval * @return a valid number inside the said interval. If min > max, then the interval will be reversed */ public static float clamp(float n, float min, float max) { if (min > max) return clamp(n, max, min); if (n < min) n = min; if (n > max) n = max; return n; } }