2016-09-19 18:48:27 +02:00
|
|
|
package org.warp.picalculator.math.functions;
|
2016-09-18 14:33:25 +02:00
|
|
|
|
2016-11-02 21:56:40 +01:00
|
|
|
import static org.warp.picalculator.device.graphicengine.Display.Render.glGetStringWidth;
|
2016-09-19 18:48:27 +02:00
|
|
|
import static org.warp.picalculator.device.graphicengine.Display.Render.glDrawStringLeft;
|
2016-09-18 14:33:25 +02:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2016-09-19 18:48:27 +02:00
|
|
|
import org.warp.picalculator.Error;
|
|
|
|
import org.warp.picalculator.Errors;
|
|
|
|
import org.warp.picalculator.Utils;
|
|
|
|
import org.warp.picalculator.device.PIDisplay;
|
|
|
|
import org.warp.picalculator.device.graphicengine.Display;
|
|
|
|
import org.warp.picalculator.math.MathematicalSymbols;
|
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
|
|
|
|
2016-11-02 21:56:40 +01:00
|
|
|
public SumSubtraction(Function parent, Function value1, Function value2) {
|
|
|
|
super(parent, value1, value2);
|
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
|
|
|
|
protected boolean isSolvable() throws Error {
|
|
|
|
if (variable1 instanceof Number & variable2 instanceof Number) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-18 14:33:25 +02:00
|
|
|
@Override
|
|
|
|
public List<Function> solveOneStep() throws Error {
|
|
|
|
if (variable1 == null || variable2 == null) {
|
|
|
|
throw new Error(Errors.SYNTAX_ERROR);
|
|
|
|
}
|
|
|
|
ArrayList<Function> result = new ArrayList<>();
|
2016-10-02 16:01:41 +02:00
|
|
|
if (variable1.isSolved() & variable2.isSolved()) {
|
2016-09-18 14:33:25 +02:00
|
|
|
result.add(((Number)variable1).add((Number)variable2));
|
2016-11-02 21:56:40 +01:00
|
|
|
result.add(((Number)variable1).add(((Number)variable2).multiply(new Number(this.parent, "-1"))));
|
2016-09-18 14:33:25 +02:00
|
|
|
} else {
|
|
|
|
List<Function> l1 = new ArrayList<Function>();
|
|
|
|
List<Function> l2 = new ArrayList<Function>();
|
2016-10-02 16:01:41 +02:00
|
|
|
if (variable1.isSolved()) {
|
2016-09-18 14:33:25 +02:00
|
|
|
l1.add(variable1);
|
|
|
|
} else {
|
2016-10-02 16:01:41 +02:00
|
|
|
l1.addAll(variable1.solveOneStep());
|
|
|
|
}
|
|
|
|
if (variable2.isSolved()) {
|
2016-09-18 14:33:25 +02:00
|
|
|
l2.add(variable2);
|
2016-10-02 16:01:41 +02:00
|
|
|
} else {
|
|
|
|
l2.addAll(variable2.solveOneStep());
|
2016-09-18 14:33:25 +02:00
|
|
|
}
|
|
|
|
|
2016-10-02 16:01:41 +02:00
|
|
|
Function[][] results = Utils.joinFunctionsResults(l1, l2);
|
|
|
|
|
2016-09-18 14:33:25 +02:00
|
|
|
for (Function[] f : results) {
|
2016-11-02 21:56:40 +01:00
|
|
|
result.add(new SumSubtraction(this.parent, f[0], f[1]));
|
2016-09-18 14:33:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void generateGraphics() {
|
|
|
|
variable1.setSmall(small);
|
|
|
|
variable1.generateGraphics();
|
|
|
|
|
|
|
|
variable2.setSmall(small);
|
|
|
|
variable2.generateGraphics();
|
|
|
|
|
|
|
|
width = calcWidth();
|
|
|
|
height = calcHeight();
|
|
|
|
line = calcLine();
|
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
|
|
|
|
|
|
|
int ln = getLine();
|
|
|
|
int dx = 0;
|
|
|
|
variable1.draw(dx + x, ln - variable1.getLine() + y);
|
|
|
|
dx += variable1.getWidth();
|
2016-11-02 21:56:40 +01:00
|
|
|
Display.Render.glSetFont(Utils.getFont(small));
|
2016-09-18 14:33:25 +02:00
|
|
|
dx += 1;
|
|
|
|
glDrawStringLeft(dx + x, ln - Utils.getFontHeight(small) / 2 + y, getSymbol());
|
2016-11-02 21:56:40 +01:00
|
|
|
dx += glGetStringWidth(getSymbol());
|
2016-09-18 14:33:25 +02:00
|
|
|
variable2.draw(dx + x, ln - variable2.getLine() + y);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getWidth() {
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected int calcWidth() {
|
|
|
|
int dx = 0;
|
|
|
|
dx += variable1.getWidth();
|
|
|
|
dx += 1;
|
2016-11-02 21:56:40 +01:00
|
|
|
dx += glGetStringWidth(getSymbol());
|
2016-09-18 14:33:25 +02:00
|
|
|
return dx += variable2.getWidth();
|
|
|
|
}
|
|
|
|
}
|