2016-11-02 21:56:40 +01:00
|
|
|
package org.warp.picalculator.math.functions;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
import org.warp.picalculator.Error;
|
|
|
|
import org.warp.picalculator.Errors;
|
|
|
|
import org.warp.picalculator.Utils;
|
|
|
|
import org.warp.picalculator.device.graphicengine.Display;
|
|
|
|
import org.warp.picalculator.math.MathematicalSymbols;
|
|
|
|
import org.warp.picalculator.math.rules.ExpandRule1;
|
|
|
|
import org.warp.picalculator.math.rules.ExpandRule5;
|
|
|
|
|
|
|
|
public class Negative extends AnteriorFunction {
|
|
|
|
|
|
|
|
public Negative(Function parent, Function value) {
|
|
|
|
super(parent, value);
|
|
|
|
}
|
2016-12-04 22:22:16 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public Function NewInstance(Function parent, Function value) {
|
|
|
|
return new Negative(parent, value);
|
|
|
|
}
|
2016-11-02 21:56:40 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getSymbol() {
|
|
|
|
return MathematicalSymbols.MINUS;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void generateGraphics() {
|
|
|
|
variable.setSmall(small);
|
|
|
|
variable.generateGraphics();
|
|
|
|
|
|
|
|
height = getVariable().getHeight();
|
2016-11-25 22:40:43 +01:00
|
|
|
width = Display.Render.glGetStringWidth(Utils.getFont(small), "-") + getVariable().getWidth();
|
2016-11-02 21:56:40 +01:00
|
|
|
line = getVariable().getLine();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-11-25 22:40:43 +01:00
|
|
|
protected boolean isSolvable() {
|
2016-11-02 21:56:40 +01:00
|
|
|
if (variable instanceof Number) return true;
|
|
|
|
if (ExpandRule1.compare(this)) return true;
|
|
|
|
if (ExpandRule5.compare(this)) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-04 22:22:16 +01:00
|
|
|
public ArrayList<Function> solve() throws Error {
|
2016-11-02 21:56:40 +01:00
|
|
|
if (variable == null) {
|
|
|
|
throw new Error(Errors.SYNTAX_ERROR);
|
|
|
|
}
|
|
|
|
ArrayList<Function> result = new ArrayList<>();
|
|
|
|
if (ExpandRule1.compare(this)) {
|
|
|
|
result = ExpandRule1.execute(this);
|
|
|
|
} else if (ExpandRule5.compare(this)) {
|
|
|
|
result = ExpandRule5.execute(this);
|
|
|
|
} else if (variable.isSolved()) {
|
|
|
|
try {
|
|
|
|
Number var = (Number) getVariable();
|
|
|
|
result.add(var.multiply(new Number(null, "-1")));
|
|
|
|
} catch(NullPointerException ex) {
|
|
|
|
throw new Error(Errors.ERROR);
|
|
|
|
} catch(NumberFormatException ex) {
|
|
|
|
throw new Error(Errors.SYNTAX_ERROR);
|
|
|
|
} catch(ArithmeticException ex) {
|
|
|
|
throw new Error(Errors.NUMBER_TOO_SMALL);
|
|
|
|
}
|
|
|
|
} else {
|
2016-12-04 22:22:16 +01:00
|
|
|
ArrayList<Function> l1 = new ArrayList<Function>();
|
2016-11-02 21:56:40 +01:00
|
|
|
if (variable.isSolved()) {
|
|
|
|
l1.add(variable);
|
|
|
|
} else {
|
|
|
|
l1.addAll(variable.solveOneStep());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Function f : l1) {
|
|
|
|
result.add(new Negative(this.parent, (Function)f));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getWidth() {
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getHeight() {
|
|
|
|
return height;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getLine() {
|
|
|
|
return line;
|
|
|
|
}
|
2016-11-25 22:40:43 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
if (o instanceof Negative) {
|
|
|
|
return ((Negative)o).variable.equals(variable);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-02 21:56:40 +01:00
|
|
|
}
|