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());
}
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);
}
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;
}
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;
}
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());
}
Aggregations