Search in sources :

Example 11 with Node

use of expression.Node in project OpenNotebook by jaltekruse.

the class ExpressionGenerator method generateProblem.

public GeneratedProblem generateProblem(int difficulty) {
    GeneratedProblem newProblem = new GeneratedProblem();
    ExpressionObject expressionObj = new ExpressionObject();
    try {
        newProblem.setDifficulty(difficulty);
        newProblem.getListWithName(GeneratedProblem.GEN_LIST).getValues().clear();
        ((ListAttribute<UUIDAttribute>) newProblem.getListWithName(GeneratedProblem.GEN_LIST)).addValueWithString(getProblemID().toString());
        Node[] n = generateExpression(difficulty);
        expressionObj.setExpression(n[0].toStringRepresentation());
        expressionObj.addCorrectAnswer(n[1].toStringRepresentation());
    } catch (AttributeException e) {
        e.printStackTrace();
    } catch (NodeException e) {
        e.printStackTrace();
    }
    getProblemHoldingDocument().getDocViewerPanel().drawObjectInBackground(expressionObj);
    expressionObj.setParentContainer(newProblem.getParentContainer());
    newProblem.addObjectFromPage(expressionObj);
    return newProblem;
}
Also used : ExpressionObject(doc.mathobjects.ExpressionObject) Node(expression.Node) GeneratedProblem(doc.mathobjects.GeneratedProblem) NodeException(expression.NodeException) AttributeException(doc.attributes.AttributeException) ListAttribute(doc.attributes.ListAttribute)

Example 12 with Node

use of expression.Node in project OpenNotebook by jaltekruse.

the class AdditionPropertyOfEquality method generateExpression.

@Override
protected Node[] generateExpression(int difficulty) throws NodeException {
    Node[] n = new Node[2];
    if (difficulty == ProblemGenerator.EASY) {
        double[] numPair = ExUtil.pairOfCleanAddingNumbers(100);
        if (ExUtil.randomBoolean()) {
            numPair[1] = -1 * numPair[1];
        }
        n[1] = new Expression(new Operator.Equals(), ExUtil.randomVar(), new Number(numPair[0]));
        n[0] = ((Expression) n[1]).applyOpToBothSides(new Operator.Addition(), new Number(numPair[1]), true);
        n[0] = n[0].smartNumericSimplify();
    } else if (difficulty == ProblemGenerator.MEDIUM) {
        n[1] = new Expression(new Operator.Equals(), ExUtil.randomVar(), new Number(ExUtil.randomInt(5, 30, true)));
        n[0] = ((Expression) n[1]).applyOpToBothSides(new Operator.Addition(), new Number(ExUtil.randomInt(5, 30, true)), true);
        n[0] = n[0].smartNumericSimplify();
    } else if (difficulty == ProblemGenerator.HARD) {
        n[1] = new Expression(new Operator.Equals(), ExUtil.randomVar(), new Number(ExUtil.randomInt(5, 30, true)));
        Expression sumNode = ExUtil.randomAdditionOrSubtraction(5, 30);
        Node sum = sumNode.numericSimplify();
        n[0] = n[1].cloneNode();
        Node newLeft = null, newRight = null;
        if (ExUtil.randomBoolean()) {
            newLeft = ((Expression) n[0]).getChild(0).addNodeToExpression(sumNode);
            newRight = ((Expression) n[0]).getChild(1).addNodeToExpression(sum);
        } else {
            newLeft = ((Expression) n[0]).getChild(0).addNodeToExpression(sum);
            newRight = ((Expression) n[0]).getChild(1).addNodeToExpression(sumNode);
        }
        n[0] = new Expression(new Operator.Equals(), newLeft, newRight);
    }
    n[0] = ExUtil.flipSides((Expression) n[0]);
    return n;
}
Also used : Operator(expression.Operator) Number(expression.Number) Expression(expression.Expression) Node(expression.Node)

Example 13 with Node

use of expression.Node in project OpenNotebook by jaltekruse.

the class NegationGraphic method requestSize.

@Override
public int[] requestSize(Graphics g, Font f, int x1, int y1) throws Exception {
    g.setFont(f);
    setFont(f);
    space = (int) (1 * super.getRootNodeGraphic().DOC_ZOOM_LEVEL);
    Node tempChild = ((Expression) getValue()).getChild(0);
    NodeGraphic childValGraphic = null;
    int[] childSize = { 0, 0 };
    int[] symbolSize = { 0, 0 };
    int[] totalSize = { 0, 0 };
    symbolSize[0] = (int) Math.round(super.getRootNodeGraphic().getStringWidth("-", f)) + 2 * space;
    symbolSize[1] = super.getRootNodeGraphic().getFontHeight(f);
    childValGraphic = makeNodeGraphic(tempChild);
    setChildGraphic(childValGraphic);
    super.getRootNodeGraphic().getComponents().add(childValGraphic);
    childSize = childValGraphic.requestSize(g, f, x1 + symbolSize[0], y1);
    // set the west and east fields for inside an outside of the expression
    setMostInnerWest(this);
    setEast(childValGraphic);
    childValGraphic.setWest(this);
    setMostInnerEast(childValGraphic);
    symbolY1 = y1 + childValGraphic.getUpperHeight() - (int) Math.round(symbolSize[1] / 2.0);
    symbolY2 = symbolY1 + symbolSize[1];
    symbolX1 = x1;
    symbolX2 = x1 + symbolSize[0];
    setUpperHeight(childValGraphic.getUpperHeight());
    setLowerHeight(childValGraphic.getLowerHeight());
    totalSize[0] = symbolSize[0] + childSize[0];
    totalSize[1] = childSize[1];
    super.setX1(x1);
    super.setY1(y1);
    super.setX2(x1 + totalSize[0]);
    super.setY2(y1 + totalSize[1]);
    return totalSize;
}
Also used : Expression(expression.Expression) Node(expression.Node)

Example 14 with Node

use of expression.Node in project OpenNotebook by jaltekruse.

the class RadicalGraphic method requestSize.

@Override
public int[] requestSize(Graphics g, Font f, int x1, int y1) throws Exception {
    g.setFont(f);
    setFont(f);
    space = (int) (4 * super.getRootNodeGraphic().DOC_ZOOM_LEVEL * getRootNodeGraphic().getFontSizeAdjustment());
    widthFront = (int) (8 * super.getRootNodeGraphic().DOC_ZOOM_LEVEL * getRootNodeGraphic().getFontSizeAdjustment());
    heightLeadingTail = (int) (6 * super.getRootNodeGraphic().DOC_ZOOM_LEVEL * getRootNodeGraphic().getFontSizeAdjustment());
    lengthLittleTail = (int) (3 * super.getRootNodeGraphic().DOC_ZOOM_LEVEL * getRootNodeGraphic().getFontSizeAdjustment());
    // The call to getChild() skips the first paren inside of the operator, the parens are needed to have
    // an expression inside of a UnaryOp, but they are not usually displayed
    // if a user wants to show parens, the can use  two pairs of parens: sqrt((5/6))
    Node tempChild = super.getValue().getChild(0);
    NodeGraphic childValGraphic = null;
    int[] childSize = { 0, 0 };
    int[] symbolSize = { 0, 0 };
    int[] totalSize = { 0, 0 };
    childValGraphic = makeNodeGraphic(tempChild);
    childSize = childValGraphic.requestSize(g, f, x1 + widthFront + space, y1 + space);
    // set the west and east fields for inside an outside of the expression
    setMostInnerWest(this);
    childValGraphic.getMostInnerEast().setEast(this);
    setMostInnerEast(this);
    childValGraphic.getMostInnerWest().setWest(this);
    setMostInnerNorth(this);
    setMostInnerSouth(this);
    setChildGraphic(childValGraphic);
    super.getRootNodeGraphic().getComponents().add(childValGraphic);
    widthFront += (int) Math.round(childSize[1] / 14.0);
    if (widthFront > 20) {
        widthFront = 20;
    }
    heightLeadingTail += (int) Math.round(childSize[1] / 5.0);
    if (heightLeadingTail > 40) {
        heightLeadingTail = 40;
    }
    childValGraphic.shiftToX1(x1 + widthFront + space);
    symbolSize[0] = childSize[0] + space * 2 + widthFront;
    symbolSize[1] = childSize[1] + space;
    symbolY1 = y1;
    symbolY2 = symbolY1 + symbolSize[1];
    symbolX1 = x1;
    symbolX2 = x1 + symbolSize[0];
    setUpperHeight(childValGraphic.getUpperHeight() + space);
    setLowerHeight(childValGraphic.getLowerHeight());
    totalSize[0] = symbolSize[0];
    totalSize[1] = symbolSize[1];
    super.setX1(x1);
    super.setY1(y1);
    super.setX2(x1 + totalSize[0]);
    super.setY2(y1 + totalSize[1]);
    return totalSize;
}
Also used : Node(expression.Node)

Example 15 with Node

use of expression.Node in project OpenNotebook by jaltekruse.

the class UnaryExpressionGraphic method requestSize.

@Override
public int[] requestSize(Graphics g, Font f, int x1, int y1) throws Exception {
    g.setFont(f);
    setFont(f);
    space = (int) (2 * super.getRootNodeGraphic().DOC_ZOOM_LEVEL);
    Node tempChild = super.getValue().getChild(0);
    NodeGraphic childValGraphic = null;
    int[] childSize = { 0, 0 };
    int[] symbolSize = { 0, 0 };
    int[] totalSize = { 0, 0 };
    symbolSize[0] = super.getRootNodeGraphic().getStringWidth(value.getOperator().getSymbol(), f) + space;
    symbolSize[1] = super.getRootNodeGraphic().getFontHeight(f);
    childValGraphic = makeNodeGraphic(tempChild);
    setChildGraphic(childValGraphic);
    super.getRootNodeGraphic().getComponents().add(childValGraphic);
    childSize = childValGraphic.requestSize(g, f, x1 + symbolSize[0], y1);
    // set the west and east fields for inside and outside of the expression
    setMostInnerWest(this);
    setEast(childValGraphic);
    childValGraphic.setWest(this);
    setMostInnerEast(childValGraphic);
    symbolY1 = y1 + childValGraphic.getUpperHeight() - (int) Math.round(symbolSize[1] / 2.0);
    symbolY2 = symbolY1 + symbolSize[1];
    symbolX1 = x1;
    symbolX2 = x1 + symbolSize[0];
    setUpperHeight(childValGraphic.getUpperHeight());
    setLowerHeight(childValGraphic.getLowerHeight());
    totalSize[0] = symbolSize[0] + childSize[0];
    totalSize[1] = childSize[1];
    super.setX1(x1);
    super.setY1(y1);
    super.setX2(x1 + totalSize[0]);
    super.setY2(y1 + totalSize[1]);
    return totalSize;
}
Also used : Node(expression.Node)

Aggregations

Node (expression.Node)29 Expression (expression.Expression)10 NodeException (expression.NodeException)10 Number (expression.Number)9 Operator (expression.Operator)7 Vector (java.util.Vector)6 StringAttribute (doc.attributes.StringAttribute)4 AttributeException (doc.attributes.AttributeException)3 Identifier (expression.Identifier)2 BasicStroke (java.awt.BasicStroke)2 Graphics2D (java.awt.Graphics2D)2 Point (java.awt.Point)2 RootNodeGraphic (math_rendering.RootNodeGraphic)2 ListAttribute (doc.attributes.ListAttribute)1 ExpressionObject (doc.mathobjects.ExpressionObject)1 GeneratedProblem (doc.mathobjects.GeneratedProblem)1 Multiplication (expression.Operator.Multiplication)1 Color (java.awt.Color)1 Cursor (math_rendering.Cursor)1