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

5 6 7
import 'equation_member.dart';
import 'expression.dart';
import 'param.dart';
Chinmay Garde's avatar
Chinmay Garde committed
8

9
class Term extends EquationMember {
Ian Hickson's avatar
Ian Hickson committed
10 11
  Term(this.variable, this.coefficient);

Chinmay Garde's avatar
Chinmay Garde committed
12
  final Variable variable;
13

Chinmay Garde's avatar
Chinmay Garde committed
14
  final double coefficient;
15

16
  @override
17
  Expression asExpression() =>
18
      new Expression(<Term>[new Term(this.variable, this.coefficient)], 0.0);
19

20
  @override
21
  bool get isConstant => false;
Chinmay Garde's avatar
Chinmay Garde committed
22

23
  @override
24
  double get value => coefficient * variable.value;
25

26
  @override
27 28 29 30 31 32 33 34 35 36 37 38 39 40
  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
41
}