# ui/autolayout/layoutexpression (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/ui/autolayout/layoutexpression/

선형 식. (원본 ui/autolayout/layoutexpression.js 의 LayoutExpression, 항 목록 + 상수항)
- 산술 함수는 모두 새 식을 돌려준다. (바뀌지 않는 값)
- equal_to, less_than_or_equal_to, greater_than_or_equal_to 는 LayoutConstraint 를 돌려준다. (required 강도)
- 인자는 숫자(f64, i32), LayoutVariable, LayoutTerm, LayoutExpression 을 받는다. (impl `Into<LayoutExpression>`)
- 원본과 다른 것:
  - 원본 toExpression 의 실행 중 타입 검사는 From 구현으로 옮겼다. 받지 않는 타입은 컴파일이 막는다.
  - multiply, divide 는 원본도 숫자만 받으므로 f64 만 받는다. (원본의 '스칼라만 지원' 예외는 생기지 않는다)
  - 0 으로 나누기는 프로그램 실수라 원본과 같은 문구로 panic 한다.
  - 원본은 LayoutConstraint 와 서로 import 하는 것을 피하려고 늦게 읽는 꾀를 쓴다. 러스트 판은 그냥 use 한다.

## LayoutExpression (struct) (Clone, Debug, From<f64>, From<i32>, From<&LayoutVariable>, From<LayoutVariable>, From<LayoutTerm>, From<&LayoutTerm>, From<&LayoutExpression>)

선형 식.
- 항(계수 * 변수) 목록과 상수항의 합이다. 같은 변수의 항도 합치지 않고 넣은 차례대로 둔다.

예제: 왼쪽과 너비로 가운데 식 만들기

```rust
use vanilla::{LayoutExpression, LayoutVariable};

let left = LayoutVariable::new("left");
let width = LayoutVariable::new("width");
left.set_value(20.0);
width.set_value(100.0);
let center_x = LayoutExpression::from_variable(&left).add(LayoutExpression::from_variable(&width).divide(2.0));
assert_eq!(center_x.get_terms().len(), 2);
assert_eq!(center_x.get_value(), 70.0);

let shifted = center_x.subtract(10).multiply(2.0);
assert_eq!(shifted.get_constant(), -20.0);
assert_eq!(shifted.get_value(), 120.0);
```

- `pub fn new(terms: Vec<LayoutTerm>, constant: f64) -> LayoutExpression` (constructor, since 0.4.0): 생성. (항 목록과 상수항)
  - `terms` Vec<LayoutTerm>: 항 목록.
  - `constant` f64: 상수항.
  - returns LayoutExpression: 새 식.
- `pub fn from_variable(variable: &LayoutVariable) -> LayoutExpression` (static, since 0.4.0): 변수 하나로 식 만들기. (1 * variable + 0)
  - `variable` &LayoutVariable: 식에 넣을 변수. (핸들을 복제해 쥔다)
  - returns LayoutExpression: 항 하나(1 * variable)와 상수항 0 의 식.
- `pub fn from_constant(value: f64) -> LayoutExpression` (static, since 0.4.0): 상수만 가진 식 만들기.
  - `value` f64: 상수항.
  - returns LayoutExpression: 항이 없고 상수항이 value 인 식.
- `pub fn to_expression(input: impl Into<LayoutExpression>) -> LayoutExpression` (static, since 0.4.0): 받을 수 있는 입력을 식으로 바꾼다.
  - `input` impl Into<LayoutExpression>: 바꿀 것. (숫자 f64, i32, LayoutVariable, LayoutTerm, LayoutExpression 과 그 참조)
  - returns LayoutExpression: 바꾼 식. (숫자는 상수 식, 변수는 1 * variable, 항은 그 항 하나, 식은 사본)

  예제: 여러 꼴의 입력을 식으로

  ```rust
  use vanilla::{LayoutExpression, LayoutTerm, LayoutVariable};
  
  let x = LayoutVariable::new("x");
  assert!(LayoutExpression::to_expression(12).is_constant());
  assert_eq!(LayoutExpression::to_expression(2.5f64).get_constant(), 2.5);
  assert_eq!(LayoutExpression::to_expression(&x).get_terms()[0].get_coefficient(), 1.0);
  assert_eq!(LayoutExpression::to_expression(LayoutTerm::new(x.clone(), 3.0)).get_terms()[0].get_coefficient(), 3.0);
  ```

- `pub fn get_terms(&self) -> &[LayoutTerm]` (method, since 0.4.0): 항 목록 반환.
  - returns &[LayoutTerm]: 항 목록 조각. (넣은 차례)
- `pub fn get_constant(&self) -> f64` (method, since 0.4.0): 상수항 반환.
  - returns f64: 상수항.
- `pub fn is_constant(&self) -> bool` (method, since 0.4.0): 항이 없는지. (상수항만 있는지)
  - returns bool: 항이 없으면 true.
- `pub fn get_value(&self) -> f64` (method, since 0.4.0): 평가 값 반환. (항 값의 합 + 상수항)
  - returns f64: 항마다 계수 * 변수의 지금 값을 더하고 상수항을 더한 값.
- `pub fn add(&self, input: impl Into<LayoutExpression>) -> LayoutExpression` (method, since 0.4.0): 덧셈.
  - `input` impl Into<LayoutExpression>: 더할 것. (숫자, 변수, 항, 식)
  - returns LayoutExpression: self + input 인 새 식. (self 의 항 뒤에 input 의 항을 붙이고 상수항을 더한다)
- `pub fn subtract(&self, input: impl Into<LayoutExpression>) -> LayoutExpression` (method, since 0.4.0): 뺄셈.
  - `input` impl Into<LayoutExpression>: 뺄 것. (숫자, 변수, 항, 식)
  - returns LayoutExpression: self - input 인 새 식. (input 에 -1 을 곱해 더한다)
- `pub fn multiply(&self, coefficient: f64) -> LayoutExpression` (method, since 0.4.0): 스칼라 곱셈. (변수끼리의 곱 같은 비선형 곱은 받지 않는다)
  - `coefficient` f64: 곱할 수.
  - returns LayoutExpression: 모든 계수와 상수항에 coefficient 를 곱한 새 식.
- `pub fn divide(&self, denominator: f64) -> LayoutExpression` (method, since 0.4.0): 스칼라 나눗셈. (0 으로 나누면 panic 한다)
  - `denominator` f64: 나눌 수. (0 이면 panic)
  - returns LayoutExpression: self * (1 / denominator) 인 새 식.
- `pub fn equal_to(&self, input: impl Into<LayoutExpression>) -> LayoutConstraint` (method, since 0.4.0): 같음 제약 만들기. (self == input)
  - `input` impl Into<LayoutExpression>: 맞출 것. (숫자, 변수, 항, 식)
  - returns LayoutConstraint: 식이 self - input 이고 관계가 Equal 인 required 제약. (솔버에는 걸지 않는다)

  예제: 오른쪽 == 왼쪽 + 너비를 걸어 풀기

  ```rust
  use vanilla::{LayoutExpression, LayoutSolver, LayoutVariable};
  
  let solver = LayoutSolver::new();
  let left = LayoutVariable::new("left");
  let right = LayoutVariable::new("right");
  solver.add_constraint(&LayoutExpression::from_variable(&left).equal_to(30)).unwrap();
  let right_constraint = LayoutExpression::from_variable(&right).equal_to(LayoutExpression::from_variable(&left).add(200));
  solver.add_constraint(&right_constraint).unwrap();
  solver.update_variables();
  assert!((right.get_value() - 230.0).abs() < 1e-9);
  ```

- `pub fn less_than_or_equal_to(&self, input: impl Into<LayoutExpression>) -> LayoutConstraint` (method, since 0.4.0): 작거나 같음 제약 만들기. (self <= input)
  - `input` impl Into<LayoutExpression>: 넘지 않을 것. (숫자, 변수, 항, 식)
  - returns LayoutConstraint: 식이 self - input 이고 관계가 LessThanOrEqual 인 required 제약.
- `pub fn greater_than_or_equal_to(&self, input: impl Into<LayoutExpression>) -> LayoutConstraint` (method, since 0.4.0): 크거나 같음 제약 만들기. (self >= input)
  - `input` impl Into<LayoutExpression>: 밑돌지 않을 것. (숫자, 변수, 항, 식)
  - returns LayoutConstraint: 식이 self - input 이고 관계가 GreaterThanOrEqual 인 required 제약.
