term.dart 811 Bytes
Newer Older
Chinmay Garde's avatar
Chinmay Garde committed
1 2 3 4 5 6
// Copyright (c) 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

part of cassowary;

7
class Term extends _EquationMember {
Chinmay Garde's avatar
Chinmay Garde committed
8 9
  final Variable variable;
  final double coefficient;
10 11 12

  bool get isConstant => false;

Chinmay Garde's avatar
Chinmay Garde committed
13 14 15 16
  double get value => coefficient * variable.value;

  Term(this.variable, this.coefficient);

17
  Expression asExpression() =>
18
      new Expression([new Term(this.variable, this.coefficient)], 0.0);
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

  String toString() {
    StringBuffer buffer = new StringBuffer();

    buffer.write(coefficient.sign > 0.0 ? "+" : "-");

    if (coefficient.abs() != 1.0) {
      buffer.write(coefficient.abs());
      buffer.write("*");
    }

    buffer.write(variable);

    return buffer.toString();
  }
Chinmay Garde's avatar
Chinmay Garde committed
34
}