在曲线中间添加方向

时间:2016-04-23 01:18:32

标签: java draw

我使用QuadCurve2D在节点之间绘制曲线

QuadCurve2D curve = new QuadCurve2D.Double(start.getX(), start.getY(), c1, c2, end.getX(), end.getY());

我使用以下代码为曲线添加方向,其中g2dGraphics2D对象

g2d.draw(curve);

Polygon arrowHead = new Polygon();
arrowHead.addPoint(0, 5);
arrowHead.addPoint(-5, -5);
arrowHead.addPoint(5, -5);

AffineTransform at = new AffineTransform();
at.setToIdentity();
double angle = Math.atan2(curve.getY2() - curve.getY1(), curve.getX2() - curve.getX1());
at.translate(curve.getX2(), curve.getY2());
at.rotate((angle - Math.PI / 2d));

g2d.setTransform(at);
g2d.fill(arrowHead);

我想在曲线中间显示方向而不是最后,我尝试更改.translate()参数,但它出现在陌生的地方。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

首先我迭代了路径以获得它的长度,然后我得到了中心的点并且引出了我的方向

PathIterator iterator = curve.getPathIterator(null, 1);
int count = 0;
double[] coordinates = new double[2];
while (!iterator.isDone()) {
    ++count;
    iterator.next();
}

iterator = curve.getPathIterator(null, 1);
int a = count / 2;
int b = a + 1;
count = 0;
while (!iterator.isDone()) {
    count++;
    iterator.currentSegment(coordinates);
    if (count == a) {
        x1 = (int) coordinates[0];
        y1 = (int) coordinates[1];
    } else if (count == b) {
        x2 = (int) coordinates[0];
        y2 = (int) coordinates[1];
    }
    iterator.next();
}

我的方向成了

Polygon arrowHead = new Polygon();
arrowHead.addPoint(0, 5);
arrowHead.addPoint(-5, -5);
arrowHead.addPoint(5, -5);

AffineTransform at = new AffineTransform();
at.setToIdentity();
double angle = Math.atan2(y2 - y1, x2 - x1);
at.translate(x2, y2);
at.rotate((angle - Math.PI / 2d));