Switch式を使ったのでメモ
条件に応じて何かしら値が決まるときに便利.シンプルに書ける.
加えて,ある条件に対して複数の値がセットで決まるような場合にタプル(Tuple)を使うとさらにシンプル.
細かいところは置いておいて,変数hd
が3パターン,変数vd
も3パターンあって,それぞれの値に応じて3パターンの異なる結果を得る場合,if文やswitch文だとちょっと長くなるところを,以下のようにスッキリかける.
(float x0, float x1) = hd switch { GradientHorizontalDirection.LeftToRight => (0f, width), GradientHorizontalDirection.RightToLeft => (width, 0f), _ => (0f, 0f) }; (float y0, float y1) = vd switch { GradientVerticalDirection.TopToBottom => (0f, height), GradientVerticalDirection.BottomToTop => (height, 0f), _ => (0f, 0f) }; return (x0, x1, y0, y1);