Search in sources :

Example 21 with Point2DArray

use of com.ait.lienzo.client.core.types.Point2DArray in project lienzo-core by ahome-it.

the class IsoscelesTrapezoid method parse.

private boolean parse(final Attributes attr) {
    final double hig = attr.getHeight();
    final double top = attr.getTopWidth();
    final double bot = attr.getBottomWidth();
    if ((hig > 0) && (top > 0) && (bot > 0)) {
        final double sub = Math.abs(top - bot);
        final Point2DArray list = new Point2DArray();
        if (0 == sub) {
            list.push(0, 0);
            list.push(top, 0);
            list.push(top, hig);
            list.push(0, hig);
        } else {
            if (top > bot) {
                list.push(0, 0);
                list.push(top, 0);
                list.push((sub / 2.0) + bot, hig);
                list.push((sub / 2.0), hig);
            } else {
                list.push((sub / 2.0), 0);
                list.push((sub / 2.0) + top, 0);
                list.push(bot, hig);
                list.push(0, hig);
            }
        }
        final Point2D p0 = list.get(0);
        m_list.M(p0);
        final double corner = getCornerRadius();
        if (corner <= 0) {
            final int size = list.size();
            for (int i = 1; i < size; i++) {
                m_list.L(list.get(i));
            }
            m_list.Z();
        } else {
            Geometry.drawArcJoinedLines(m_list, list.push(p0), corner);
        }
        return true;
    }
    return false;
}
Also used : Point2DArray(com.ait.lienzo.client.core.types.Point2DArray) Point2D(com.ait.lienzo.client.core.types.Point2D)

Example 22 with Point2DArray

use of com.ait.lienzo.client.core.types.Point2DArray in project lienzo-core by ahome-it.

the class Line method prepare.

/**
 * Draws this line
 *
 * @param context
 */
@Override
protected boolean prepare(final Context2D context, final Attributes attr, final double alpha) {
    final Point2DArray list = attr.getPoints();
    if ((null != list) && (list.size() == 2)) {
        if (attr.isDefined(Attribute.DASH_ARRAY)) {
            if (false == LienzoCore.get().isNativeLineDashSupported()) {
                final DashArray dash = attr.getDashArray();
                if (dash != null) {
                    final double[] data = dash.getNormalizedArray();
                    if (data.length > 0) {
                        if (setStrokeParams(context, attr, alpha, false)) {
                            final Point2D p0 = list.get(0);
                            final Point2D p1 = list.get(1);
                            context.beginPath();
                            drawDashedLine(context, p0.getX(), p0.getY(), p1.getX(), p1.getY(), data, attr.getStrokeWidth() / 2);
                            context.restore();
                        }
                        return true;
                    }
                }
            }
        }
        context.beginPath();
        final Point2D p0 = list.get(0);
        context.moveTo(p0.getX(), p0.getY());
        final Point2D p1 = list.get(1);
        context.lineTo(p1.getX(), p1.getY());
        return true;
    }
    return false;
}
Also used : Point2DArray(com.ait.lienzo.client.core.types.Point2DArray) Point2D(com.ait.lienzo.client.core.types.Point2D) DashArray(com.ait.lienzo.client.core.types.DashArray)

Example 23 with Point2DArray

use of com.ait.lienzo.client.core.types.Point2DArray in project lienzo-core by ahome-it.

the class PolyLine method parse.

@Override
public boolean parse(final Attributes attr) {
    Point2DArray list = attr.getPoints();
    if (null != list) {
        list = list.noAdjacentPoints();
        final int size = list.size();
        if (size > 1) {
            final PathPartList path = getPathPartList();
            path.M(list.get(0));
            final double corner = getCornerRadius();
            if (corner <= 0) {
                for (int i = 1; i < size; i++) {
                    path.L(list.get(i));
                }
            } else {
                Geometry.drawArcJoinedLines(path, list, corner);
            }
            return true;
        }
    }
    return false;
}
Also used : Point2DArray(com.ait.lienzo.client.core.types.Point2DArray) PathPartList(com.ait.lienzo.client.core.types.PathPartList)

Example 24 with Point2DArray

use of com.ait.lienzo.client.core.types.Point2DArray in project lienzo-core by ahome-it.

the class Arrow method prepare.

/**
 * Draws this arrow.
 *
 * @param context the {@link Context2D} used to draw this arrow.
 */
@Override
protected boolean prepare(final Context2D context, final Attributes attr, final double alpha) {
    // is null for invalid arrow definition
    final Point2DArray list = getPolygon();
    if ((null != list) && (list.size() > 2)) {
        Point2D point = list.get(0);
        context.beginPath();
        context.moveTo(point.getX(), point.getY());
        final int leng = list.size();
        for (int i = 1; i < leng; i++) {
            point = list.get(i);
            context.lineTo(point.getX(), point.getY());
        }
        context.closePath();
        return true;
    }
    return false;
}
Also used : Point2DArray(com.ait.lienzo.client.core.types.Point2DArray) Point2D(com.ait.lienzo.client.core.types.Point2D)

Example 25 with Point2DArray

use of com.ait.lienzo.client.core.types.Point2DArray in project lienzo-core by ahome-it.

the class Arrow method getPolygon.

private Point2DArray getPolygon() {
    if (m_polygon == null) {
        final Point2DArray arr = new Point2DArray();
        try {
            final ArrowType type = getArrowType();
            final double a = Geometry.toRadians(getArrowAngle());
            final double sina = Math.sin(a);
            final double cosa = Math.cos(a);
            // NOTE: b is not the base angle here, it's the corner EAB
            // i.e. going from E to A to B, where B is point[2] and A is the
            // intersection of the midline (thru S and E)
            // and the line thru point [1] and [2].
            final double b_degrees = 180 - getBaseAngle() - getArrowAngle();
            final double b = Geometry.toRadians(b_degrees);
            final double sinb = Math.sin(b);
            final double cosb = Math.cos(b);
            final double w = getBaseWidth();
            final double aw = getHeadWidth();
            // arr.getPoint(0);
            final Point2D s = getStart();
            // arr.getPoint(1);
            final Point2D e = getEnd();
            final Point2D dv = e.sub(s);
            // unit vector in the direction of SE
            final Point2D dx = dv.unit();
            final Point2D dy = dx.perpendicular();
            if ((type == ArrowType.AT_END) || (type == ArrowType.AT_END_TAPERED) || (type == ArrowType.AT_BOTH_ENDS)) {
                // cosa*r
                // 
                // S----+---E
                // | a/ sina*r=aw/2
                // sina*r | / r
                // |/
                // 2
                // 
                final double r = aw / (2 * sina);
                final double z = r * cosa;
                final Point2D p2 = e.sub(dx.mul(z)).sub(dy.mul(aw / 2));
                final Point2D p4 = e.sub(dx.mul(z)).add(dy.mul(aw / 2));
                // cosb*r2
                // 
                // 1---+
                // \b |
                // \ | sinb*r2=(aw-w)/2
                // r2 \|
                // 2
                // 
                Point2D p1 = p2.add(dy.mul((aw - w) / 2));
                Point2D p5 = p4.sub(dy.mul((aw - w) / 2));
                if (b_degrees != 90) {
                    final double r2 = (aw - w) / (2 * sinb);
                    final Point2D d1 = dx.mul(r2 * cosb);
                    p1 = p1.sub(d1);
                    p5 = p5.sub(d1);
                }
                arr.push(p1);
                arr.push(p2);
                arr.push(e);
                arr.push(p4);
                arr.push(p5);
            } else if (type == ArrowType.AT_START) {
                final Point2D q0 = e.add(dy.mul(-w / 2));
                final Point2D q6 = e.add(dy.mul(w / 2));
                arr.push(q0);
                arr.push(q6);
            } else // ArrowType.AT_START_TAPERED
            {
                arr.push(e);
            }
            if ((type == ArrowType.AT_START) || (type == ArrowType.AT_START_TAPERED) || (type == ArrowType.AT_BOTH_ENDS)) {
                // cosa*r
                // 
                // S----+---E
                // | a/ sina*r=aw/2
                // sina*r | / r
                // |/
                // 2
                // 
                final double r = aw / (2 * sina);
                final double z = r * cosa;
                final Point2D q2 = s.add(dx.mul(z)).sub(dy.mul(aw / 2));
                final Point2D q4 = s.add(dx.mul(z)).add(dy.mul(aw / 2));
                // cosb*r2
                // 
                // 1---+
                // \b |
                // \ | sinb*r2=(aw-w)/2
                // r2 \|
                // 2
                // 
                Point2D q1 = q2.add(dy.mul((aw - w) / 2));
                Point2D q5 = q4.sub(dy.mul((aw - w) / 2));
                if (b_degrees != 90) {
                    final double r2 = (aw - w) / (2 * sinb);
                    final Point2D d1 = dx.mul(r2 * cosb);
                    q1 = q1.add(d1);
                    q5 = q5.add(d1);
                }
                arr.push(q5);
                arr.push(q4);
                arr.push(s);
                arr.push(q2);
                arr.push(q1);
            } else if (type == ArrowType.AT_END) {
                final Point2D p0 = s.add(dy.mul(-w / 2));
                final Point2D p6 = s.add(dy.mul(w / 2));
                arr.push(p6);
                arr.push(p0);
            } else // ArrowType.AT_END_TAPERED
            {
                arr.push(s);
            }
        } catch (final GeometryException e) {
        // This can happen e.g. when S and E are the same point.
        // Leave m_polygon array empty and the draw code will simply not draw it.
        }
        m_polygon = arr;
    }
    return m_polygon;
}
Also used : Point2DArray(com.ait.lienzo.client.core.types.Point2DArray) Point2D(com.ait.lienzo.client.core.types.Point2D) GeometryException(com.ait.lienzo.client.core.util.GeometryException) ArrowType(com.ait.lienzo.shared.core.types.ArrowType)

Aggregations

Point2DArray (com.ait.lienzo.client.core.types.Point2DArray)47 Point2D (com.ait.lienzo.client.core.types.Point2D)37 BoundingBox (com.ait.lienzo.client.core.types.BoundingBox)6 HashSet (java.util.HashSet)6 Set (java.util.Set)5 Direction (com.ait.lienzo.shared.core.types.Direction)4 IControlHandle (com.ait.lienzo.client.core.shape.wires.IControlHandle)3 IControlHandleList (com.ait.lienzo.client.core.shape.wires.IControlHandleList)3 PathPartList (com.ait.lienzo.client.core.types.PathPartList)3 Attributes (com.ait.lienzo.client.core.shape.Attributes)2 MultiPath (com.ait.lienzo.client.core.shape.MultiPath)2 WiresMagnet (com.ait.lienzo.client.core.shape.wires.WiresMagnet)2 DashArray (com.ait.lienzo.client.core.types.DashArray)2 Transform (com.ait.lienzo.client.core.types.Transform)2 NFastDoubleArrayJSO (com.ait.tooling.nativetools.client.collection.NFastDoubleArrayJSO)2 Test (org.junit.Test)2 Group (com.ait.lienzo.client.core.shape.Group)1 Line (com.ait.lienzo.client.core.shape.Line)1 Node (com.ait.lienzo.client.core.shape.Node)1 OrthogonalPolyLine (com.ait.lienzo.client.core.shape.OrthogonalPolyLine)1