Added logarithms

This commit is contained in:
Andrea Cavalli 2018-05-05 23:06:36 +02:00
parent bc49dfe3f3
commit 3358028a71
9 changed files with 233 additions and 3 deletions

Binary file not shown.

View File

@ -8,5 +8,5 @@ public enum Key {
PLUS, MINUS, PLUS_MINUS, MULTIPLY, DIVIDE, EQUAL, DELETE, RESET, LEFT, RIGHT,
UP, DOWN, OK, debug1, debug2, debug3, debug4, debug5, SQRT, ROOT, POWER_OF_2,
POWER_OF_x, SINE, COSINE, TANGENT, ARCSINE, ARCCOSINE, ARCTANGENT, PI, SETTINGS,
F1, F2, F3, F4, BACK, ZOOM_MODE
F1, F2, F3, F4, BACK, ZOOM_MODE, LOGARITHM
}

View File

@ -186,6 +186,15 @@ public class Keyboard {
Keyboard.keyPressed(Key.ZOOM_MODE);
}
break;
case KeyEvent.VK_L:
if (Keyboard.shift) {
Keyboard.keyPressed(Key.LOGARITHM);
} else if (Keyboard.alpha) {
Keyboard.keyPressed(Key.NONE);
} else {
Keyboard.keyPressed(Key.LOGARITHM);
}
break;
case com.jogamp.newt.event.KeyEvent.VK_ENTER:
case KeyEvent.VK_ENTER:
if (Keyboard.shift) {

View File

@ -0,0 +1,139 @@
package org.warp.picalculator.gui.expression.blocks;
import org.warp.picalculator.Error;
import org.warp.picalculator.gui.expression.Caret;
import org.warp.picalculator.gui.graphicengine.GraphicEngine;
import org.warp.picalculator.gui.graphicengine.Renderer;
import org.warp.picalculator.math.Function;
import org.warp.picalculator.math.MathContext;
import org.warp.picalculator.math.parser.features.FeatureLogarithm;
import org.warp.picalculator.math.parser.features.interfaces.Feature;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
public class BlockLogarithm extends Block {
private final BlockContainer containerBase;
private final BlockContainer containerNumber;
private final String prefix = "LOG_TEST";
private int prw;
private int bw;
private int bh;
private int chw;
private int chh;
public BlockLogarithm() {
containerBase = new BlockContainer(true);
containerNumber = new BlockContainer(false);
recomputeDimensions();
}
public BlockLogarithm(ObjectArrayList<Block> blocks) {
containerBase = new BlockContainer(true);
containerNumber = new BlockContainer(false, blocks);
recomputeDimensions();
}
@Override
public void draw(GraphicEngine ge, Renderer r, int x, int y, Caret caret) {
BlockContainer.getDefaultFont(small).use(ge);
r.glColor(BlockContainer.getDefaultColor());
if (prefix != null) {
r.glDrawStringLeft(x + 1, y + line - chh / 2, prefix);
}
r.glDrawCharLeft(x + bw + prw, y, '╭');
r.glDrawCharLeft(x + bw + prw, y + height - chh, '╰');
if (small) {
r.glFillColor(x + bw + prw + 1, y + 5, 1, height - 4 * 2);
r.glFillColor(x + width - 3, y + 5, 1, height - 4 * 2);
} else {
r.glFillColor(x + bw + prw + 3, y + 6, 2, height - 6 * 2);
r.glFillColor(x + width - 5, y + 6, 2, height - 6 * 2);
}
r.glDrawCharLeft(x + width - chw, y, '╮');
r.glDrawCharLeft(x + width - chw, y + height - chh, '╯');
r.glColor(BlockContainer.getDefaultColor());
containerBase.draw(ge, r, x + prw, y + height - bh, caret);
r.glColor(BlockContainer.getDefaultColor());
containerNumber.draw(ge, r, x + bw + prw + chw, y, caret);
}
@Override
public boolean putBlock(Caret caret, Block newBlock) {
boolean added = false;
added = added | containerBase.putBlock(caret, newBlock);
added = added | containerNumber.putBlock(caret, newBlock);
if (added) {
recomputeDimensions();
}
return added;
}
@Override
public boolean delBlock(Caret caret) {
boolean removed = false;
removed = removed | containerBase.delBlock(caret);
removed = removed | containerNumber.delBlock(caret);
if (removed) {
recomputeDimensions();
}
return removed;
}
@Override
public BlockReference<?> getBlock(Caret caret) {
BlockReference<?> bl = null;
bl = containerBase.getBlock(caret);
if (bl != null) {
return bl;
}
bl = containerNumber.getBlock(caret);
return bl;
}
@Override
public void recomputeDimensions() {
if (prefix == null) {
prw = 0;
} else {
prw = 1 + BlockContainer.getDefaultCharWidth(small) * prefix.length() + 2;
}
bw = containerBase.getWidth();
bh = containerBase.getHeight();
chw = BlockContainer.getDefaultCharWidth(small);
chh = BlockContainer.getDefaultCharHeight(small);
width = prw + bw + chw + containerNumber.getWidth() + chw + 3;
height = containerNumber.getHeight();
line = containerNumber.getLine();
}
@Override
public void setSmall(boolean small) {
this.small = small;
containerBase.setSmall(small);
containerNumber.setSmall(small);
recomputeDimensions();
}
public BlockContainer getBaseContainer() {
return containerBase;
}
public BlockContainer getNumberContainer() {
return containerNumber;
}
@Override
public int computeCaretMaxBound() {
return containerBase.computeCaretMaxBound() + containerNumber.computeCaretMaxBound();
}
@Override
public Feature toFeature(MathContext context) throws Error {
final Function base = getBaseContainer().toFunction(context);
final Function number = getNumberContainer().toFunction(context);
return new FeatureLogarithm(base, number);
}
}

View File

@ -10,6 +10,7 @@ import org.warp.picalculator.gui.expression.blocks.BlockParenthesis;
import org.warp.picalculator.gui.expression.blocks.BlockPower;
import org.warp.picalculator.gui.expression.blocks.BlockReference;
import org.warp.picalculator.gui.expression.blocks.BlockSine;
import org.warp.picalculator.gui.expression.blocks.BlockLogarithm;
import org.warp.picalculator.gui.expression.blocks.BlockSquareRoot;
import org.warp.picalculator.gui.expression.blocks.BlockVariable;
import org.warp.picalculator.math.MathematicalSymbols;
@ -71,6 +72,8 @@ public class NormalInputContainer extends InputContainer {
return new BlockNumericChar(c);
case MathematicalSymbols.SINE:
return new BlockSine();
case MathematicalSymbols.LOGARITHM:
return new BlockLogarithm();
case MathematicalSymbols.PI:
return new BlockVariable(inputContext, c, true);
default:

View File

@ -361,6 +361,9 @@ public class MathInputScreen extends Screen {
case ARCTANGENT:
typeChar(MathematicalSymbols.ARC_TANGENT);
return true;
case LOGARITHM:
typeChar(MathematicalSymbols.LOGARITHM);
return true;
case DELETE:
userInput.del();
currentStep = 0;

View File

@ -16,7 +16,6 @@ public class MathematicalSymbols {
public static final char PARENTHESIS_OPEN = '(';
public static final char PARENTHESIS_CLOSE = ')';
public static final char POWER = 'Ⓑ';
public static final char POWER_OF_TWO = 'Ⓘ';
public static final char EQUATION = '=';
public static final char SYSTEM = '{';
public static final char SINE = 'Ⓒ';
@ -25,6 +24,8 @@ public class MathematicalSymbols {
public static final char ARC_SINE = 'Ⓕ';
public static final char ARC_COSINE = 'Ⓖ';
public static final char ARC_TANGENT = 'Ⓗ';
public static final char POWER_OF_TWO = 'Ⓘ';
public static final char LOGARITHM = 'Ⓙ';
public static final char UNDEFINED = '∅';
public static final char PI = 'π';
public static final char X = 'ⓧ';
@ -32,7 +33,7 @@ public class MathematicalSymbols {
public static final char[] functionsNSN = new char[] { NTH_ROOT, POWER };
public static final char[] functionsSN = new char[] { SQUARE_ROOT, POWER_OF_TWO, MINUS, SINE, COSINE, TANGENT, ARC_SINE, ARC_COSINE, ARC_TANGENT };
public static final char[] functionsSN = new char[] { SQUARE_ROOT, POWER_OF_TWO, MINUS, SINE, COSINE, TANGENT, ARC_SINE, ARC_COSINE, ARC_TANGENT, LOGARITHM };
public static final char[] functions = concat(functionsNSN, functionsSN);

View File

@ -0,0 +1,56 @@
package org.warp.picalculator.math.functions;
import org.warp.picalculator.Error;
import org.warp.picalculator.Errors;
import org.warp.picalculator.gui.expression.blocks.Block;
import org.warp.picalculator.gui.expression.blocks.BlockContainer;
import org.warp.picalculator.gui.expression.blocks.BlockDivision;
import org.warp.picalculator.gui.expression.blocks.BlockLogarithm;
import org.warp.picalculator.math.Function;
import org.warp.picalculator.math.FunctionOperator;
import org.warp.picalculator.math.MathContext;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
public class Logarithm extends FunctionOperator {
public Logarithm(MathContext root, Function value1, Function value2) {
super(root, value1, value2);
}
@Override
public boolean equals(Object o) {
if (o instanceof Logarithm) {
final FunctionOperator f = (FunctionOperator) o;
return parameter1.equals(f.getParameter1()) && parameter2.equals(f.getParameter2());
}
return false;
}
@Override
public Logarithm clone() {
return new Logarithm(mathContext, parameter1, parameter2);
}
@Override
public ObjectArrayList<Block> toBlock(MathContext context) throws Error {
ObjectArrayList<Block> result = new ObjectArrayList<>();
ObjectArrayList<Block> sub1 = getParameter1().toBlock(context);
ObjectArrayList<Block> sub2 = getParameter2().toBlock(context);
BlockLogarithm bd = new BlockLogarithm();
BlockContainer uc = bd.getBaseContainer();
BlockContainer lc = bd.getNumberContainer();
for (Block b : sub1) {
uc.appendBlockUnsafe(b);
}
for (Block b : sub2) {
lc.appendBlockUnsafe(b);
}
uc.recomputeDimensions();
lc.recomputeDimensions();
bd.recomputeDimensions();
result.add(bd);
return result;
}
}

View File

@ -0,0 +1,19 @@
package org.warp.picalculator.math.parser.features;
import org.warp.picalculator.Error;
import org.warp.picalculator.math.MathContext;
import org.warp.picalculator.math.functions.Logarithm;
import org.warp.picalculator.math.functions.Power;
public class FeatureLogarithm extends FeatureDoubleImpl {
public FeatureLogarithm(Object child1, Object child2) {
super(child1, child2);
}
@Override
public Logarithm toFunction(MathContext context) throws Error {
return new Logarithm(context, getFunction1(), getFunction2());
}
}