Search in sources :

Example 1 with ArrowType

use of com.ait.lienzo.shared.core.types.ArrowType in project lienzo-core by ahome-it.

the class Arrow method getPolygon.

private Point2DArray getPolygon() {
    if (m_polygon == null) {
        Point2DArray arr = new Point2DArray();
        try {
            ArrowType type = getArrowType();
            double a = Geometry.toRadians(getArrowAngle());
            double sina = Math.sin(a);
            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].
            double b_degrees = 180 - getBaseAngle() - getArrowAngle();
            double b = Geometry.toRadians(b_degrees);
            double sinb = Math.sin(b);
            double cosb = Math.cos(b);
            double w = getBaseWidth();
            double aw = getHeadWidth();
            // arr.getPoint(0);
            Point2D s = getStart();
            // arr.getPoint(1);
            Point2D e = getEnd();
            Point2D dv = e.sub(s);
            // unit vector in the direction of SE
            Point2D dx = dv.unit();
            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
                // 
                double r = aw / (2 * sina);
                double z = r * cosa;
                Point2D p2 = e.sub(dx.mul(z)).sub(dy.mul(aw / 2));
                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) {
                    double r2 = (aw - w) / (2 * sinb);
                    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) {
                Point2D q0 = e.add(dy.mul(-w / 2));
                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
                // 
                double r = aw / (2 * sina);
                double z = r * cosa;
                Point2D q2 = s.add(dx.mul(z)).sub(dy.mul(aw / 2));
                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) {
                    double r2 = (aw - w) / (2 * sinb);
                    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) {
                Point2D p0 = s.add(dy.mul(-w / 2));
                Point2D p6 = s.add(dy.mul(w / 2));
                arr.push(p6);
                arr.push(p0);
            } else // ArrowType.AT_END_TAPERED
            {
                arr.push(s);
            }
        } catch (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

Point2D (com.ait.lienzo.client.core.types.Point2D)1 Point2DArray (com.ait.lienzo.client.core.types.Point2DArray)1 GeometryException (com.ait.lienzo.client.core.util.GeometryException)1 ArrowType (com.ait.lienzo.shared.core.types.ArrowType)1