Search in sources :

Example 1 with Identifier

use of expression.Identifier in project OpenNotebook by jaltekruse.

the class VariableValueInsertionProblem method generateProblem.

public GeneratedProblem generateProblem(int difficulty) {
    Grouping newProblem = new Grouping(getParentContainer(), getxPos(), getyPos() + getHeight() + bufferSpace, getWidth(), getHeight());
    String s;
    Node n = null;
    Vector<String> varNames = new Vector<>();
    Vector<Number> varVals = new Vector<>();
    for (StringAttribute strAtt : (Vector<StringAttribute>) getScripts().getValues()) {
        s = strAtt.getValue();
        if (s == null || s.equals("")) {
            continue;
        }
        try {
            n = Node.parseNode(s);
            //sub in variables already assigned in previous scripts
            for (int i = 0; i < varNames.size(); i++) {
                n = n.replace(varNames.get(i), varVals.get(i));
            }
            n = n.numericSimplify();
            if (n instanceof Expression) {
                Expression ex = (Expression) n;
                if (ex.getOperator() instanceof Operator.Equals) {
                    if (ex.getChild(0) instanceof Identifier) {
                        Identifier var = (Identifier) ex.getChild(0);
                        if (ex.getChild(1) instanceof Number) {
                            varNames.add(var.getIdentifier());
                            // this causes a lot of unneeded parenthesis
                            // but without it, you cannot sub in a value
                            // where there is an implied parenthesis
                            // ex.getChild(1).setDisplayParentheses(true);
                            varVals.add((Number) ex.getChild(1));
                        }
                    }
                }
            }
        } catch (NodeException e) {
            JOptionPane.showMessageDialog(null, "Error generating a problem, check scripts.", ERROR, JOptionPane.ERROR_MESSAGE);
        }
    }
    MathObject newObj = null;
    for (MathObject mObj : getObjects()) {
        newObj = mObj.clone();
        for (int i = 0; i < varNames.size(); i++) {
            newObj = subInVal(varNames.get(i), varVals.get(i), newObj);
        }
        //shift object down so it doesn't overlap the current problem
        newObj.setyPos(newObj.getyPos() + getHeight() + bufferSpace);
        //this line sets the bounds to the actual space it takes to render them
        if (getParentContainer() != null) {
            // if this problem formula is in the background storage for a document
            getParentDoc().getDocViewerPanel().drawObjectInBackground(newObj);
        } else {
            // if this problem formula is actually on a document
            getProblemHoldingDocument().getDocViewerPanel().drawObjectInBackground(newObj);
        }
        newObj.setParentContainer(newProblem.getParentContainer());
        newProblem.addObjectFromPage(newObj);
    }
    return new GeneratedProblem(newProblem.getParentContainer(), this.getProblemID(), newProblem);
}
Also used : Node(expression.Node) StringAttribute(doc.attributes.StringAttribute) NodeException(expression.NodeException) Identifier(expression.Identifier) Number(expression.Number) Expression(expression.Expression) Vector(java.util.Vector)

Example 2 with Identifier

use of expression.Identifier in project OpenNotebook by jaltekruse.

the class ExUtil method randomTerm.

public static Node randomTerm(int degree, String var, int minCoefficient, int maxCoefficient) {
    Number num = new Number(randomInt(minCoefficient, maxCoefficient, true));
    if (degree == 0) {
        return num;
    }
    Expression ex = new Expression(new Operator.Multiplication());
    ex.getChildren().add(num);
    try {
        if (degree == 1) {
            ex.getChildren().add(new Identifier(var));
            return ex;
        } else {
            ex.getChildren().add(new Expression(new Operator.Exponent(), new Identifier(var), new Number(degree)));
            return ex;
        }
    } catch (NodeException e) {
        throw new RuntimeException(e);
    }
}
Also used : Operator(expression.Operator) Identifier(expression.Identifier) Number(expression.Number) Expression(expression.Expression) NodeException(expression.NodeException)

Example 3 with Identifier

use of expression.Identifier in project OpenNotebook by jaltekruse.

the class GraphedCartFunction method draw.

@Override
public void draw(Graphics g) {
    //used to temporarily store the value stored in the independent and dependent vars,
    //this will allow it to be restored after graphing, so that if in the terminal a
    //value was assigned to x, it will not be overriden by the action of graphing
    //		Number xVal = getIndependentVar().getValue();
    //		Number yVal = getDependentVar().getValue();
    super.clearPts();
    Graphics2D g2d = ((Graphics2D) g);
    g.setColor(getColor());
    if (hasFocus()) {
        graph.LINE_SIZE = 3;
    } else {
        graph.LINE_SIZE = 2;
    }
    int tempLineSize = graph.LINE_SIZE;
    double lastX = 0, lastY = 0, currX = 0, currY = 0;
    Node origionalEx = null;
    Node ex;
    try {
        lastX = graph.X_MIN;
        origionalEx = Node.parseNode(getFuncEqtn());
        ex = origionalEx.cloneNode();
        ex = ex.replace(new Identifier(getIndependentVar()), new Number(lastX));
        ex = ex.replace("π", new Number(Math.PI));
        ex = ex.replace("e", new Number(Math.E));
        ex = ex.numericSimplify();
        if (ex instanceof Expression) {
            if (((Expression) ex).getOperator() instanceof Operator.Equals) {
                if (((Expression) ex).getChild(1) instanceof Number) {
                    lastY = ((Number) ((Expression) ex).getChild(1)).getValue();
                }
            }
        }
        if (gridxToScreen(lastX) <= graph.X_SIZE + graph.X_PIC_ORIGIN && gridxToScreen(lastX) >= +graph.X_PIC_ORIGIN && gridyToScreen(lastY) <= graph.Y_SIZE + graph.Y_PIC_ORIGIN && gridyToScreen(lastY) >= +graph.Y_PIC_ORIGIN) {
            //if the current point is on the screen, add it to the list of points
            addPt(gridxToScreen(lastX), gridyToScreen(lastY));
        }
    } catch (Exception e1) {
        currX = graph.X_MIN;
        lastY = graph.Y_MIN;
        ;
    }
    currX = graph.X_MIN;
    boolean validExpression;
    for (int i = 1; i < graph.X_SIZE; i += 1) {
        validExpression = false;
        try {
            currX = currX + graph.X_PIXEL;
            ex = origionalEx.cloneNode();
            ex = ex.replace(getIndependentVar(), new Number(currX));
            ex = ex.replace("π", new Number(Math.PI));
            ex = ex.replace("e", new Number(Math.E));
            ex = ex.numericSimplify();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            lastX = currX;
            lastY = graph.Y_MIN;
            continue;
        }
        if (ex instanceof Expression) {
            if (((Expression) ex).getOperator() instanceof Operator.Equals) {
                if (((Expression) ex).getChild(1) instanceof Number) {
                    currY = ((Number) ((Expression) ex).getChild(1)).getValue();
                    validExpression = true;
                }
            }
        } else if (!validExpression) {
            lastX = currX;
            lastY = graph.Y_MIN;
            continue;
        }
        if (gridxToScreen(currX) <= graph.X_SIZE + graph.X_PIC_ORIGIN && gridxToScreen(currX) >= graph.X_PIC_ORIGIN && gridyToScreen(currY) <= graph.Y_SIZE + graph.Y_PIC_ORIGIN && gridyToScreen(currY) >= graph.Y_PIC_ORIGIN) {
            //if the current point is on the screen, add it to the list of points
            if (lastY <= graph.Y_MIN) {
                addPt(gridxToScreen(lastX), graph.Y_SIZE + graph.Y_PIC_ORIGIN);
            }
            if (lastY >= graph.Y_MAX) {
                addPt(gridxToScreen(lastX), 0 + graph.Y_PIC_ORIGIN);
            }
            addPt(gridxToScreen(currX), gridyToScreen(currY));
        } else if (gridxToScreen(lastX) <= graph.X_SIZE + graph.X_PIC_ORIGIN && gridxToScreen(lastX) >= graph.X_PIC_ORIGIN && gridyToScreen(lastY) <= graph.Y_SIZE + graph.Y_PIC_ORIGIN && gridyToScreen(lastY) >= graph.Y_PIC_ORIGIN) {
            //if the last point is on the screen, add the correct boundary for this point to the list
            addPt(gridxToScreen(lastX), gridyToScreen(lastY));
            if (currY <= graph.Y_MIN) {
                addPt(gridxToScreen(currX), graph.Y_SIZE + graph.Y_PIC_ORIGIN);
            }
            if (currY >= graph.Y_MAX) {
                addPt(gridxToScreen(currX), 0 + graph.Y_PIC_ORIGIN);
            }
        } else if (lastY >= graph.Y_MAX && currY <= graph.Y_MIN) {
            //if the last point was off the the top of the screen, and this one is off
            //the bottom, add the two to the list of points
            addPt(gridxToScreen(lastX), graph.Y_SIZE + graph.Y_PIC_ORIGIN);
            addPt(gridxToScreen(currX), 0 + graph.Y_PIC_ORIGIN);
        } else if (currY >= graph.Y_MAX && lastY <= graph.Y_MIN) {
            //if the last point was off the the bottom of the screen, and this one is off
            //the top, add the two to the list of points
            addPt(gridxToScreen(lastX), 0 + graph.Y_PIC_ORIGIN);
            addPt(gridxToScreen(currX), graph.Y_SIZE + graph.Y_PIC_ORIGIN);
        }
        if (isConnected()) {
            drawLineSeg(lastX, lastY, currX, currY, getColor(), g);
        }
        lastX = currX;
        lastY = currY;
    }
    //		g2d.setStroke(new BasicStroke(graph.LINE_SIZE * graph.DOC_ZOOM_LEVEL,
    //				BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
    //		if ( this.getxVals().size() > 0){
    //			GeneralPath polyline = 
    //					new GeneralPath(GeneralPath.WIND_EVEN_ODD, this.getxVals().size());
    //			polyline.moveTo (this.getxVals().get(0), this.getyVals().get(0));
    //			for (int i = 1; i < this.getxVals().size(); i++) {
    //				polyline.lineTo( getxVals().get(i), getyVals().get(i));
    //			};
    //			g2d.draw(polyline);
    //		}
    graph.LINE_SIZE = 2;
    g2d.setStroke(new BasicStroke(1));
}
Also used : BasicStroke(java.awt.BasicStroke) Identifier(expression.Identifier) Number(expression.Number) Expression(expression.Expression) Node(expression.Node) NodeException(expression.NodeException) Graphics2D(java.awt.Graphics2D)

Aggregations

Expression (expression.Expression)3 Identifier (expression.Identifier)3 NodeException (expression.NodeException)3 Number (expression.Number)3 Node (expression.Node)2 StringAttribute (doc.attributes.StringAttribute)1 Operator (expression.Operator)1 BasicStroke (java.awt.BasicStroke)1 Graphics2D (java.awt.Graphics2D)1 Vector (java.util.Vector)1