56
50

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

上限、下限を一行で書くテクニック

Last updated at Posted at 2012-02-10

よくゲームとかでは、自機の移動に0〜640までの制限などをつけることがあります。
昔はよくこういうコードを書いていました。

上限下限をつける
if(jiki.x < 0) jiki.x = 0;
if(jiki.x > 640) jiki.x = 640;

こう書くことで、0以下や640以上になることはありません。
しかし、これは1行で綺麗に書くことができます。

上限下限をつける
jiki.x = max(min(jiki.x,640),0);
56
50
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
56
50

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?