Search in sources :

Example 11 with IBezierCurve

use of net.sf.latexdraw.models.interfaces.shape.IBezierCurve in project latexdraw by arnobl.

the class TestShapeFactory method testCreateBezierCurveFromSameNbPoints.

@Test
public void testCreateBezierCurveFromSameNbPoints() {
    IBezierCurve bc = ShapeFactory.INST.createBezierCurve(Collections.singletonList(ShapeFactory.INST.createPoint()));
    bc = ShapeFactory.INST.createBezierCurveFrom(bc, ShapeFactory.INST.createPoint(1d, 2d));
    assertEquals(bc.getNbPoints(), bc.getFirstCtrlPts().size());
}
Also used : IBezierCurve(net.sf.latexdraw.models.interfaces.shape.IBezierCurve) Test(org.junit.Test) HelperTest(net.sf.latexdraw.HelperTest)

Example 12 with IBezierCurve

use of net.sf.latexdraw.models.interfaces.shape.IBezierCurve in project latexdraw by arnobl.

the class PSTLatexdrawListener method exitPsbezier.

@Override
public void exitPsbezier(final net.sf.latexdraw.parsers.pst.PSTParser.PsbezierContext ctx) {
    // Transforming all the parsed points
    Stream<IPoint> stream = IntStream.range(0, ctx.p1.size()).mapToObj(i -> Stream.of(ctx.p1.get(i), ctx.p2.get(i), ctx.p3.get(i))).flatMap(s -> s).map(pt -> ShapeFactory.INST.createPoint(ctx.pstctx.coordToAdjustedPoint(pt)));
    // Managing the optional last point
    if (ctx.p4 == null) {
        stream = Stream.concat(Stream.of(ShapeFactory.INST.createPoint(ctx.pstctx.originToPoint())), stream);
    } else {
        stream = Stream.concat(stream, Stream.of(ShapeFactory.INST.createPoint(ctx.pstctx.coordToAdjustedPoint(ctx.p4))));
    }
    final List<IPoint> allPts = stream.collect(Collectors.toList());
    // Point 1: shape point, point 2 : second control point (ignored), point 3 : first control point
    final List<IPoint> pts = IntStream.range(0, allPts.size()).filter(i -> i % 3 == 0).mapToObj(i -> allPts.get(i)).collect(Collectors.toList());
    final List<IPoint> ctrls = IntStream.range(2, allPts.size()).filter(i -> i % 3 == 2).mapToObj(i -> allPts.get(i)).collect(Collectors.toList());
    if (allPts.size() > 1) {
        ctrls.add(0, allPts.get(1));
    }
    boolean closed = false;
    // Closing the shape
    if (pts.size() > 2 && pts.get(0).equals(pts.get(pts.size() - 1))) {
        pts.remove(pts.size() - 1);
        ctrls.remove(ctrls.size() - 1);
        closed = true;
    }
    final IBezierCurve bc = ShapeFactory.INST.createBezierCurve(pts);
    bc.setOpened(!closed);
    setShapeParameters(bc, ctx.pstctx);
    setArrows(bc, ctx.pstctx);
    // Setting the control points
    IntStream.range(0, bc.getFirstCtrlPts().size()).forEach(i -> bc.getFirstCtrlPtAt(i).setPoint(ctrls.get(i)));
    // Updating the second control points to the first ones
    bc.updateSecondControlPoints();
    if (ctx.pstctx.starredCmd(ctx.cmd)) {
        setShapeForStar(bc);
    }
    shapes.peek().addShape(bc);
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) ShapeFactory(net.sf.latexdraw.models.ShapeFactory) IRectangularShape(net.sf.latexdraw.models.interfaces.shape.IRectangularShape) Token(org.antlr.v4.runtime.Token) IGrid(net.sf.latexdraw.models.interfaces.shape.IGrid) MathUtils(net.sf.latexdraw.models.MathUtils) Deque(java.util.Deque) BorderPos(net.sf.latexdraw.models.interfaces.shape.BorderPos) IArrowableSingleShape(net.sf.latexdraw.models.interfaces.shape.IArrowableSingleShape) ICircle(net.sf.latexdraw.models.interfaces.shape.ICircle) IShape(net.sf.latexdraw.models.interfaces.shape.IShape) LineStyle(net.sf.latexdraw.models.interfaces.shape.LineStyle) DviPsColors(net.sf.latexdraw.view.latex.DviPsColors) TicksStyle(net.sf.latexdraw.models.interfaces.shape.TicksStyle) Point2D(javafx.geometry.Point2D) ArcStyle(net.sf.latexdraw.models.interfaces.shape.ArcStyle) AxesStyle(net.sf.latexdraw.models.interfaces.shape.AxesStyle) IPoint(net.sf.latexdraw.models.interfaces.shape.IPoint) FreeHandStyle(net.sf.latexdraw.models.interfaces.shape.FreeHandStyle) IEllipse(net.sf.latexdraw.models.interfaces.shape.IEllipse) IFreehand(net.sf.latexdraw.models.interfaces.shape.IFreehand) IStandardGrid(net.sf.latexdraw.models.interfaces.shape.IStandardGrid) ITriangle(net.sf.latexdraw.models.interfaces.shape.ITriangle) TextPosition(net.sf.latexdraw.models.interfaces.shape.TextPosition) PlottingStyle(net.sf.latexdraw.models.interfaces.shape.PlottingStyle) IPolygon(net.sf.latexdraw.models.interfaces.shape.IPolygon) IPolyline(net.sf.latexdraw.models.interfaces.shape.IPolyline) IText(net.sf.latexdraw.models.interfaces.shape.IText) ICircleArc(net.sf.latexdraw.models.interfaces.shape.ICircleArc) IGroup(net.sf.latexdraw.models.interfaces.shape.IGroup) IBezierCurve(net.sf.latexdraw.models.interfaces.shape.IBezierCurve) Collectors(java.util.stream.Collectors) PlotStyle(net.sf.latexdraw.models.interfaces.shape.PlotStyle) DotStyle(net.sf.latexdraw.models.interfaces.shape.DotStyle) Tuple(net.sf.latexdraw.util.Tuple) ArrowStyle(net.sf.latexdraw.models.interfaces.shape.ArrowStyle) List(java.util.List) Stream(java.util.stream.Stream) Color(net.sf.latexdraw.models.interfaces.shape.Color) FillingStyle(net.sf.latexdraw.models.interfaces.shape.FillingStyle) IPlot(net.sf.latexdraw.models.interfaces.shape.IPlot) IRhombus(net.sf.latexdraw.models.interfaces.shape.IRhombus) IAxes(net.sf.latexdraw.models.interfaces.shape.IAxes) IRectangle(net.sf.latexdraw.models.interfaces.shape.IRectangle) IDot(net.sf.latexdraw.models.interfaces.shape.IDot) ArrayDeque(java.util.ArrayDeque) Collections(java.util.Collections) IBezierCurve(net.sf.latexdraw.models.interfaces.shape.IBezierCurve) IPoint(net.sf.latexdraw.models.interfaces.shape.IPoint)

Example 13 with IBezierCurve

use of net.sf.latexdraw.models.interfaces.shape.IBezierCurve in project latexdraw by arnobl.

the class PlotViewHelper method updateCurve.

public IBezierCurve updateCurve(final IPlot shape, final double posX, final double posY, final double minX, final double maxX, final double step) {
    // The algorithm follows this definition:
    // https://stackoverflow.com/questions/15864441/how-to-make-a-line-curve-through-points
    final double scale = 0.33d;
    final IBezierCurve bc = ShapeFactory.INST.createBezierCurve(fillPoints(shape, posX, posY, minX, maxX, step));
    bc.setOpened(shape.getPlotStyle() != PlotStyle.CCURVE);
    bc.copy(shape);
    int i = 0;
    final int last = bc.getPoints().size() - 1;
    for (final IPoint pt : bc.getPoints()) {
        if (i == 0) {
            final IPoint p2 = bc.getPtAt(i + 1);
            final IPoint tangent = p2.substract(pt);
            final IPoint q1 = pt.add(tangent.zoom(scale));
            bc.setXFirstCtrlPt(q1.getX(), i);
            bc.setYFirstCtrlPt(q1.getY(), i);
        } else if (i == last) {
            final IPoint p0 = bc.getPtAt(i - 1);
            final IPoint tangent = pt.substract(p0);
            final IPoint q0 = pt.substract(tangent.zoom(scale));
            bc.setXFirstCtrlPt(q0.getX(), i);
            bc.setYFirstCtrlPt(q0.getY(), i);
        } else {
            final IPoint p0 = bc.getPtAt(i - 1);
            final IPoint p2 = bc.getPtAt(i + 1);
            final IPoint tangent = p2.substract(p0).normalise();
            final IPoint q0 = pt.substract(tangent.zoom(scale * pt.substract(p0).magnitude()));
            bc.setXFirstCtrlPt(q0.getX(), i);
            bc.setYFirstCtrlPt(q0.getY(), i);
        }
        i++;
    }
    bc.updateSecondControlPoints();
    return bc;
}
Also used : IBezierCurve(net.sf.latexdraw.models.interfaces.shape.IBezierCurve) IPoint(net.sf.latexdraw.models.interfaces.shape.IPoint) IPoint(net.sf.latexdraw.models.interfaces.shape.IPoint)

Example 14 with IBezierCurve

use of net.sf.latexdraw.models.interfaces.shape.IBezierCurve in project latexdraw by arnobl.

the class LShapeFactory method createBezierCurveFrom.

@Override
public IBezierCurve createBezierCurveFrom(final IBezierCurve bc, final IPoint pointToAdd) {
    if (bc == null || !MathUtils.INST.isValidPt(pointToAdd))
        return null;
    final List<IPoint> pts = new ArrayList<>(bc.getPoints());
    final List<IPoint> ptsCtrl = new ArrayList<>(bc.getFirstCtrlPts());
    pts.add(pointToAdd);
    ptsCtrl.add(createPoint(pointToAdd.getX(), pointToAdd.getY() + IBezierCurve.DEFAULT_POSITION_CTRL));
    final IBezierCurve copy = new LBezierCurve(pts, ptsCtrl);
    copy.copy(bc);
    return copy;
}
Also used : IBezierCurve(net.sf.latexdraw.models.interfaces.shape.IBezierCurve) ArrayList(java.util.ArrayList) IPoint(net.sf.latexdraw.models.interfaces.shape.IPoint)

Example 15 with IBezierCurve

use of net.sf.latexdraw.models.interfaces.shape.IBezierCurve in project latexdraw by arnobl.

the class TestCanvasCreation method testDrawBezierCurve.

@Test
public void testDrawBezierCurve() {
    pencil.setCurrentChoice(EditionChoice.BEZIER_CURVE);
    final Point2D pos = point(canvas).query();
    moveTo(pos).clickOn(MouseButton.PRIMARY).moveBy(-20d, -100d).clickOn(MouseButton.PRIMARY).moveBy(-100d, 50d).clickOn(MouseButton.SECONDARY).sleep(SLEEP);
    assertEquals(1, drawing.size());
    assertTrue(drawing.getShapeAt(0) instanceof IBezierCurve);
    final IBezierCurve sh = (IBezierCurve) drawing.getShapeAt(0);
    assertEquals(-Canvas.getMargins() + canvas.screenToLocal(pos).getX(), sh.getPtAt(0).getX(), 1d);
    assertEquals(-Canvas.getMargins() + canvas.screenToLocal(pos).getY(), sh.getPtAt(0).getY(), 1d);
    assertTrue(sh.isOpened());
    assertEquals(3, sh.getNbPoints());
}
Also used : IBezierCurve(net.sf.latexdraw.models.interfaces.shape.IBezierCurve) Point2D(javafx.geometry.Point2D) Test(org.junit.Test)

Aggregations

IBezierCurve (net.sf.latexdraw.models.interfaces.shape.IBezierCurve)28 Test (org.junit.Test)20 HelperTest (net.sf.latexdraw.HelperTest)6 IPoint (net.sf.latexdraw.models.interfaces.shape.IPoint)5 IFreehand (net.sf.latexdraw.models.interfaces.shape.IFreehand)3 IPolygon (net.sf.latexdraw.models.interfaces.shape.IPolygon)3 IPolyline (net.sf.latexdraw.models.interfaces.shape.IPolyline)3 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 Point2D (javafx.geometry.Point2D)2 MathUtils (net.sf.latexdraw.models.MathUtils)2 ShapeFactory (net.sf.latexdraw.models.ShapeFactory)2 BorderPos (net.sf.latexdraw.models.interfaces.shape.BorderPos)2 IAxes (net.sf.latexdraw.models.interfaces.shape.IAxes)2 ICircle (net.sf.latexdraw.models.interfaces.shape.ICircle)2 ICircleArc (net.sf.latexdraw.models.interfaces.shape.ICircleArc)2 IDot (net.sf.latexdraw.models.interfaces.shape.IDot)2 IEllipse (net.sf.latexdraw.models.interfaces.shape.IEllipse)2 IGrid (net.sf.latexdraw.models.interfaces.shape.IGrid)2