Search in sources :

Example 1 with Dimensions2D

use of org.matheclipse.core.graphics.Dimensions2D in project symja_android_library by axkr.

the class GraphicsFunctions method graphicsToSVG.

public static void graphicsToSVG(IAST ast, StringBuilder buf) {
    EvalEngine engine = EvalEngine.get();
    IAST numericAST = (IAST) engine.evalN(ast);
    Dimensions2D dim = new Dimensions2D(350, 350);
    // set a default value
    dim.color = RGBColor.BLUE;
    if (numericAST.size() > 2) {
        final OptionArgs options = new OptionArgs(numericAST.topHead(), numericAST, 2, engine);
        IExpr option = options.getOption(S.PlotRange);
        if (option.isListOfLists() && option.size() == 3) {
            IAST list = (IAST) option;
            dim.setPlotRange(list.getAST(1), list.getAST(2));
        }
        option = options.getOption(S.Axes);
        if (option.isTrue()) {
            dim.setAxes(true);
        }
    }
    try {
        int width = dim.width;
        int height = dim.height;
        if (ast.size() > 1) {
            IExpr arg1 = ast.arg1();
            if (!arg1.isList()) {
                arg1 = F.list(arg1);
            }
            primitivesDimension((IAST) arg1, dim);
            exportGraphicsSVG(buf, (IAST) arg1, dim);
        }
        if (dim.isAxes()) {
            double xScale = width / (dim.xMax - dim.xMin);
            double yScale = height / (dim.yMax - dim.yMin);
            double x1 = 0;
            // vertical axe
            // + "0.000000,233.333333 6.666667,233.333333");
            buf.append("<polyline points=\"");
            buf.append(Show2SVG.FORMATTER.format((x1 - dim.xMin) * xScale));
            buf.append(",");
            buf.append(Show2SVG.FORMATTER.format(0.0));
            buf.append(" ");
            buf.append(Show2SVG.FORMATTER.format((x1 - dim.xMin) * xScale));
            buf.append(",");
            buf.append(Show2SVG.FORMATTER.format(height));
            buf.append("\" style=\"stroke: rgb(0.000000%, 0.000000%, 0.000000%); stroke-opacity: 1; stroke-width: 0.666667px; fill: none\"/>\n");
            // horizontals axe
            double y1 = (-dim.yMin) * yScale;
            buf.append("<polyline points=\"");
            buf.append(Show2SVG.FORMATTER.format(0));
            buf.append(",");
            buf.append(Show2SVG.FORMATTER.format(y1));
            buf.append(" ");
            buf.append(Show2SVG.FORMATTER.format(width));
            buf.append(",");
            buf.append(Show2SVG.FORMATTER.format(y1));
            buf.append("\" style=\"stroke: rgb(0.000000%, 0.000000%, 0.000000%); stroke-opacity: 1; stroke-width: 0.666667px; fill: none\"/>\n");
        }
    } finally {
    }
}
Also used : Dimensions2D(org.matheclipse.core.graphics.Dimensions2D) EvalEngine(org.matheclipse.core.eval.EvalEngine) OptionArgs(org.matheclipse.core.eval.util.OptionArgs) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 2 with Dimensions2D

use of org.matheclipse.core.graphics.Dimensions2D in project symja_android_library by axkr.

the class Plot method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    if (Config.USE_MANIPULATE_JS) {
        IExpr temp = S.Manipulate.of(engine, ast);
        if (temp.headID() == ID.JSFormData) {
            return temp;
        }
        return F.NIL;
    }
    if ((ast.size() >= 3) && (ast.size() <= 4) && ast.arg2().isList()) {
        try {
            final IAST rangeList = (IAST) ast.arg2();
            if (rangeList.isAST3()) {
                if (!rangeList.arg1().isSymbol()) {
                    // `1` is not a valid variable.
                    return IOFunctions.printMessage(S.Plot, "ivar", F.list(rangeList.arg1()), engine);
                }
                final ISymbol x = (ISymbol) rangeList.arg1();
                final IExpr xMin = engine.evalN(rangeList.arg2());
                final IExpr xMax = engine.evalN(rangeList.arg3());
                if ((!(xMin instanceof INum)) || (!(xMax instanceof INum))) {
                    return F.NIL;
                }
                final double xMinD = ((INum) xMin).getRealPart();
                final double xMaxd = ((INum) xMax).getRealPart();
                if (xMaxd <= xMinD) {
                    return F.NIL;
                }
                double yMinD = 0.0f;
                double yMaxD = 0.0f;
                if ((ast.isAST3()) && ast.arg3().isList()) {
                    final IAST lsty = (IAST) ast.arg3();
                    if (lsty.isAST2()) {
                        final IExpr y0 = engine.evalN(lsty.arg1());
                        final IExpr y1 = engine.evalN(lsty.arg2());
                        if ((y0 instanceof INum) && (y1 instanceof INum)) {
                            yMinD = ((INum) y0).getRealPart();
                            yMaxD = ((INum) y1).getRealPart();
                        }
                    }
                }
                final IASTAppendable graphics = Graphics();
                IASTAppendable line = Line();
                IExpr temp;
                Dimensions2D dim = new Dimensions2D();
                if (ast.arg1().isList()) {
                    final IAST list = (IAST) ast.arg1();
                    int size = list.size();
                    final IASTAppendable primitives = F.ListAlloc(size);
                    for (int i = 1; i < size; i++) {
                        temp = plotLine(xMinD, xMaxd, yMinD, yMaxD, list.get(i), x, dim, engine);
                        if (temp.isPresent()) {
                            line.append(temp);
                            primitives.append(line);
                        }
                        if (i < size - 1) {
                            line = Line();
                        }
                    }
                    graphics.append(primitives);
                } else {
                    temp = plotLine(xMinD, xMaxd, yMinD, yMaxD, ast.arg1(), x, dim, engine);
                    if (temp.isPresent()) {
                        line.append(temp);
                        graphics.append(line);
                    }
                }
                IAST plotRange;
                if (dim.isValidRange()) {
                    plotRange = Rule(S.PlotRange, F.list(F.List(dim.xMin, dim.xMax), F.List(dim.yMin, dim.yMax)));
                } else {
                    plotRange = Rule(S.PlotRange, S.Automatic);
                }
                final IExpr[] options = { plotRange, Rule(S.AxesStyle, S.Automatic), Rule(S.AxesOrigin, F.list(F.C0, F.C0)), Rule(S.Axes, S.True), Rule(S.Background, S.White) };
                graphics.appendAll(F.function(S.List, options), 1, options.length);
                return Show(graphics);
            }
        } catch (RuntimeException rex) {
            LOGGER.debug("Plot.evaluate() failed", rex);
        }
    }
    return F.NIL;
}
Also used : Dimensions2D(org.matheclipse.core.graphics.Dimensions2D) ISymbol(org.matheclipse.core.interfaces.ISymbol) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) INum(org.matheclipse.core.interfaces.INum)

Example 3 with Dimensions2D

use of org.matheclipse.core.graphics.Dimensions2D in project symja_android_library by axkr.

the class ListLinePlot method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    if (Config.USE_MANIPULATE_JS) {
        IExpr temp = S.Manipulate.of(engine, ast);
        if (temp.headID() == ID.JSFormData) {
            return temp;
        }
        return F.NIL;
    }
    if ((ast.size() == 2) && ast.arg1().isList()) {
        try {
            double xMinD = Double.MAX_VALUE;
            double xMaxD = Double.MIN_VALUE;
            double yMinD = Double.MAX_VALUE;
            double yMaxD = Double.MIN_VALUE;
            final IASTAppendable graphics = Graphics();
            Dimensions2D dim = new Dimensions2D();
            IASTAppendable points = F.NIL;
            if (ast.arg1().isVector() > 0) {
                double[] allPoints = ast.arg1().toDoubleVector();
                if (allPoints != null && allPoints.length > 0) {
                    xMaxD = 1.0;
                    xMaxD = allPoints.length;
                    points = F.ast(S.List, allPoints.length);
                    for (int i = 0; i < allPoints.length; i++) {
                        if (allPoints[i] > yMaxD) {
                            yMaxD = allPoints[i];
                        } else if (allPoints[i] < yMinD) {
                            yMinD = allPoints[i];
                        }
                        points.append(F.list(F.num(i), F.num(allPoints[i])));
                    }
                }
            } else {
                int[] matrixDim = ast.arg1().isMatrix();
                if (matrixDim != null && matrixDim[1] == 2) {
                    double[][] allPoints = ast.arg1().toDoubleMatrix();
                    if (allPoints != null && allPoints.length > 0) {
                        xMaxD = allPoints.length;
                        points = F.ast(S.List, allPoints.length);
                        for (int i = 0; i < allPoints.length; i++) {
                            for (int j = 0; j < allPoints[i].length; j++) {
                                if (allPoints[i][j] > xMaxD) {
                                    xMaxD = allPoints[i][0];
                                } else if (allPoints[i][j] < xMinD) {
                                    xMinD = allPoints[i][0];
                                }
                                if (allPoints[i][j] > yMaxD) {
                                    yMaxD = allPoints[i][1];
                                } else if (allPoints[i][j] < yMinD) {
                                    yMinD = allPoints[i][1];
                                }
                            }
                            points.append(F.list(F.num(allPoints[i][0]), F.num(allPoints[i][1])));
                        }
                    }
                }
            }
            if (points.isPresent()) {
                graphics.append(F.Line(points));
                IAST plotRange;
                if (dim.isValidRange()) {
                    plotRange = Rule(S.PlotRange, F.list(F.List(dim.xMin, dim.xMax), F.List(dim.yMin, dim.yMax)));
                } else {
                    plotRange = Rule(S.PlotRange, S.Automatic);
                }
                final IExpr[] options = { plotRange, Rule(S.AxesStyle, S.Automatic), Rule(S.AxesOrigin, List(F.C0, F.C0)), Rule(S.Axes, S.True), Rule(S.Background, S.White) };
                graphics.appendAll(F.function(S.List, options), 1, options.length);
                return Show(graphics);
            }
        } catch (RuntimeException rex) {
            LOGGER.debug("ListLinePlot.evaluate() failed", rex);
        }
    }
    return F.NIL;
}
Also used : Dimensions2D(org.matheclipse.core.graphics.Dimensions2D) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 4 with Dimensions2D

use of org.matheclipse.core.graphics.Dimensions2D in project symja_android_library by axkr.

the class ListPlot method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    if (Config.USE_MANIPULATE_JS) {
        IExpr temp = S.Manipulate.of(engine, ast);
        if (temp.headID() == ID.JSFormData) {
            return temp;
        }
        return F.NIL;
    }
    if ((ast.size() == 2) && ast.arg1().isList()) {
        try {
            double xMinD = Double.MAX_VALUE;
            double xMaxD = Double.MIN_VALUE;
            double yMinD = Double.MAX_VALUE;
            double yMaxD = Double.MIN_VALUE;
            final IASTAppendable graphics = Graphics();
            Dimensions2D dim = new Dimensions2D();
            IASTAppendable points = F.NIL;
            if (ast.arg1().isVector() > 0) {
                double[] allPoints = ast.arg1().toDoubleVector();
                if (allPoints != null && allPoints.length > 0) {
                    xMaxD = 1.0;
                    xMaxD = allPoints.length;
                    points = F.ast(S.List, allPoints.length);
                    for (int i = 0; i < allPoints.length; i++) {
                        if (allPoints[i] > yMaxD) {
                            yMaxD = allPoints[i];
                        } else if (allPoints[i] < yMinD) {
                            yMinD = allPoints[i];
                        }
                        points.append(F.list(F.num(i), F.num(allPoints[i])));
                    }
                }
            } else {
                int[] matrixDim = ast.arg1().isMatrix();
                if (matrixDim != null && matrixDim[1] == 2) {
                    double[][] allPoints = ast.arg1().toDoubleMatrix();
                    if (allPoints != null && allPoints.length > 0) {
                        xMaxD = allPoints.length;
                        points = F.ast(S.List, allPoints.length);
                        for (int i = 0; i < allPoints.length; i++) {
                            for (int j = 0; j < allPoints[i].length; j++) {
                                if (allPoints[i][j] > xMaxD) {
                                    xMaxD = allPoints[i][0];
                                } else if (allPoints[i][j] < xMinD) {
                                    xMinD = allPoints[i][0];
                                }
                                if (allPoints[i][j] > yMaxD) {
                                    yMaxD = allPoints[i][1];
                                } else if (allPoints[i][j] < yMinD) {
                                    yMinD = allPoints[i][1];
                                }
                            }
                            points.append(F.list(F.num(allPoints[i][0]), F.num(allPoints[i][1])));
                        }
                    }
                }
            }
            if (points.isPresent()) {
                graphics.append(F.Point(points));
                IAST plotRange;
                if (dim.isValidRange()) {
                    plotRange = Rule(S.PlotRange, F.list(F.List(dim.xMin, dim.xMax), F.List(dim.yMin, dim.yMax)));
                } else {
                    plotRange = Rule(S.PlotRange, S.Automatic);
                }
                final IExpr[] options = { plotRange, Rule(S.AxesStyle, S.Automatic), Rule(S.AxesOrigin, List(F.C0, F.C0)), Rule(S.Axes, S.True), Rule(S.Background, S.White) };
                graphics.appendAll(F.function(S.List, options), 1, options.length);
                return Show(graphics);
            }
        } catch (RuntimeException rex) {
            LOGGER.debug("ListPlot.evaluate() failed", rex);
        }
    }
    return F.NIL;
}
Also used : Dimensions2D(org.matheclipse.core.graphics.Dimensions2D) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Aggregations

Dimensions2D (org.matheclipse.core.graphics.Dimensions2D)4 IAST (org.matheclipse.core.interfaces.IAST)4 IExpr (org.matheclipse.core.interfaces.IExpr)4 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)3 EvalEngine (org.matheclipse.core.eval.EvalEngine)1 OptionArgs (org.matheclipse.core.eval.util.OptionArgs)1 INum (org.matheclipse.core.interfaces.INum)1 ISymbol (org.matheclipse.core.interfaces.ISymbol)1