Here you can find the source of clamp(int x, int min, int max)
Parameter | Description |
---|---|
x | the value to clamp |
min | the minimum value of the range |
max | the maximum value of the range |
public static int clamp(int x, int min, int max)
//package com.java2s; /*/*from w w w .j a va 2 s . c om*/ * Copyright (c) 2016 Martin Davis. * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * and Eclipse Distribution License v. 1.0 which accompanies this distribution. * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html * and the Eclipse Distribution License is available at * * http://www.eclipse.org/org/documents/edl-v10.php. */ public class Main { /** * Clamps a <tt>double</tt> value to a given range. * @param x the value to clamp * @param min the minimum value of the range * @param max the maximum value of the range * @return the clamped value */ public static double clamp(double x, double min, double max) { if (x < min) return min; if (x > max) return max; return x; } /** * Clamps an <tt>int</tt> value to a given range. * @param x the value to clamp * @param min the minimum value of the range * @param max the maximum value of the range * @return the clamped value */ public static int clamp(int x, int min, int max) { if (x < min) return min; if (x > max) return max; return x; } }