2016-09-19 18:48:27 +02:00
|
|
|
package org.warp.picalculator.math.functions;
|
2016-09-18 14:33:25 +02:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2016-09-19 18:48:27 +02:00
|
|
|
import org.warp.picalculator.Error;
|
|
|
|
import org.warp.picalculator.Errors;
|
|
|
|
import org.warp.picalculator.Utils;
|
2017-01-31 22:29:49 +01:00
|
|
|
import org.warp.picalculator.gui.DisplayManager;
|
2017-02-02 10:30:45 +01:00
|
|
|
import org.warp.picalculator.gui.graphicengine.cpu.CPUEngine;
|
2017-01-17 22:32:40 +01:00
|
|
|
import org.warp.picalculator.math.Calculator;
|
2016-09-19 18:48:27 +02:00
|
|
|
import org.warp.picalculator.math.MathematicalSymbols;
|
2016-11-25 22:40:43 +01:00
|
|
|
import org.warp.picalculator.math.rules.ExpandRule1;
|
|
|
|
import org.warp.picalculator.math.rules.NumberRule3;
|
|
|
|
import org.warp.picalculator.math.rules.NumberRule4;
|
|
|
|
import org.warp.picalculator.math.rules.NumberRule5;
|
2016-09-18 14:33:25 +02:00
|
|
|
|
2016-09-19 18:48:27 +02:00
|
|
|
public class SumSubtraction extends FunctionTwoValues {
|
2016-09-18 14:33:25 +02:00
|
|
|
|
2017-01-17 22:32:40 +01:00
|
|
|
public SumSubtraction(Calculator root, Function value1, Function value2) {
|
|
|
|
super(root, value1, value2);
|
2016-09-18 14:33:25 +02:00
|
|
|
}
|
2017-01-31 22:29:49 +01:00
|
|
|
|
2016-12-04 22:22:16 +01:00
|
|
|
@Override
|
2017-01-17 22:32:40 +01:00
|
|
|
protected Function NewInstance(Calculator root, Function value1, Function value2) {
|
|
|
|
return new SumSubtraction(root, value1, value2);
|
2016-12-04 22:22:16 +01:00
|
|
|
}
|
2016-09-18 14:33:25 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getSymbol() {
|
|
|
|
return MathematicalSymbols.SUM_SUBTRACTION;
|
|
|
|
}
|
|
|
|
|
2016-10-02 16:01:41 +02:00
|
|
|
@Override
|
2016-11-25 22:40:43 +01:00
|
|
|
protected boolean isSolvable() {
|
2016-10-02 16:01:41 +02:00
|
|
|
if (variable1 instanceof Number & variable2 instanceof Number) {
|
|
|
|
return true;
|
|
|
|
}
|
2017-01-31 22:29:49 +01:00
|
|
|
if (NumberRule3.compare(this)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (ExpandRule1.compare(this)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (NumberRule4.compare(this)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (NumberRule5.compare(this)) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-10-02 16:01:41 +02:00
|
|
|
return false;
|
|
|
|
}
|
2017-01-31 22:29:49 +01:00
|
|
|
|
2016-09-18 14:33:25 +02:00
|
|
|
@Override
|
2016-12-04 22:22:16 +01:00
|
|
|
public ArrayList<Function> solve() throws Error {
|
2016-09-18 14:33:25 +02:00
|
|
|
if (variable1 == null || variable2 == null) {
|
|
|
|
throw new Error(Errors.SYNTAX_ERROR);
|
|
|
|
}
|
|
|
|
ArrayList<Function> result = new ArrayList<>();
|
2016-11-25 22:40:43 +01:00
|
|
|
if (NumberRule3.compare(this)) {
|
|
|
|
result = NumberRule3.execute(this);
|
|
|
|
} else if (ExpandRule1.compare(this)) {
|
|
|
|
result = ExpandRule1.execute(this);
|
|
|
|
} else if (NumberRule4.compare(this)) {
|
|
|
|
result = NumberRule4.execute(this);
|
|
|
|
} else if (NumberRule5.compare(this)) {
|
|
|
|
result = NumberRule5.execute(this);
|
|
|
|
} else if (variable1.isSolved() & variable2.isSolved()) {
|
2017-01-31 22:29:49 +01:00
|
|
|
result.add(((Number) variable1).add((Number) variable2));
|
|
|
|
result.add(((Number) variable1).add(((Number) variable2).multiply(new Number(root, "-1"))));
|
2016-09-18 14:33:25 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void generateGraphics() {
|
|
|
|
variable1.setSmall(small);
|
|
|
|
variable1.generateGraphics();
|
2017-01-31 22:29:49 +01:00
|
|
|
|
2016-09-18 14:33:25 +02:00
|
|
|
variable2.setSmall(small);
|
|
|
|
variable2.generateGraphics();
|
2017-01-31 22:29:49 +01:00
|
|
|
|
2016-09-18 14:33:25 +02:00
|
|
|
width = calcWidth();
|
|
|
|
height = calcHeight();
|
|
|
|
line = calcLine();
|
|
|
|
}
|
2017-01-31 22:29:49 +01:00
|
|
|
|
2016-09-18 14:33:25 +02:00
|
|
|
@Override
|
|
|
|
public void draw(int x, int y) {
|
|
|
|
// glColor3f(127, 127-50+new Random().nextInt(50), 255);
|
|
|
|
// glFillRect(x,y,width,height);
|
|
|
|
// glColor3f(0, 0, 0);
|
2017-01-31 22:29:49 +01:00
|
|
|
|
|
|
|
final int ln = getLine();
|
2016-09-18 14:33:25 +02:00
|
|
|
int dx = 0;
|
|
|
|
variable1.draw(dx + x, ln - variable1.getLine() + y);
|
|
|
|
dx += variable1.getWidth();
|
2017-02-02 10:30:45 +01:00
|
|
|
Utils.getFont(small).use(DisplayManager.engine);
|
2016-09-18 14:33:25 +02:00
|
|
|
dx += 1;
|
2017-01-31 22:29:49 +01:00
|
|
|
DisplayManager.renderer.glDrawStringLeft(dx + x, ln - Utils.getFontHeight(small) / 2 + y, getSymbol());
|
2017-02-02 10:30:45 +01:00
|
|
|
dx += Utils.getFont(small).getStringWidth(getSymbol());
|
2016-09-18 14:33:25 +02:00
|
|
|
variable2.draw(dx + x, ln - variable2.getLine() + y);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getWidth() {
|
|
|
|
return width;
|
|
|
|
}
|
2017-01-31 22:29:49 +01:00
|
|
|
|
2016-09-18 14:33:25 +02:00
|
|
|
@Override
|
|
|
|
protected int calcWidth() {
|
|
|
|
int dx = 0;
|
|
|
|
dx += variable1.getWidth();
|
|
|
|
dx += 1;
|
2017-02-02 10:30:45 +01:00
|
|
|
dx += Utils.getFont(small).getStringWidth(getSymbol());
|
2016-09-18 14:33:25 +02:00
|
|
|
return dx += variable2.getWidth();
|
|
|
|
}
|
2017-01-31 22:29:49 +01:00
|
|
|
|
2016-11-25 22:40:43 +01:00
|
|
|
@Override
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
if (o instanceof SumSubtraction) {
|
2017-01-31 22:29:49 +01:00
|
|
|
final FunctionTwoValues f = (FunctionTwoValues) o;
|
2016-11-25 22:40:43 +01:00
|
|
|
return variable1.equals(f.variable1) && variable2.equals(f.variable2);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-18 14:33:25 +02:00
|
|
|
}
|