如何创建一条线,其头部距离末端有一段距离 - java / awt
How to create a line whose head is some distance from its end - java / awt
我有积分
A(x1, y1)
B(x2, y2)
我需要画这条线的箭头,但不是在最后。它必须离终点有一段距离。这东西要怎么弄?
我有:
---------------------->
我需要:
----------------->-----
见图:
这是有角度的:
结果:
谢谢大家的帮助。这是另一个。
让我们创建主函数并画一条线和箭头:
private void drawLineWithArrowHead(Point from, Point to, Graphics2D graphics){
Polygon arrowHead = new Polygon();
arrowHead.addPoint( 0,6);
arrowHead.addPoint( -6, -6);
arrowHead.addPoint( 6,-6);int y1,y2,x1,x2;
x1=from.getPosX();
y1=from.getPosY();
x2=to.getPosX();
y2=to.getPosY();
Line2D.Double line = new Line2D.Double(x1,y1,x2,y2);
graphics.draw(line);
让我们加载旧的仿射变换并从行中获取新的:
AffineTransform tx, old_tx = graphics.getTransform();
tx = calcAffineTransformation(line);
一些数学:
double dx = (x2-x1), dy = (y2-y1);
double len = Math.sqrt(dx*dx + dy*dy);
double udx = dx/len, udy = dy/len;
double cordx = x2 - (size-5) * udx, cordy = y2 - (size-5) * udy;
double r_cordx = x2 - (size+3) * udx, r_cordy = y2 - (size+3) * udy;
现在放置箭头:
tx.setToIdentity(); // null transform to origin
double angle = Math.atan2(line.y2 - line.y1, line.x2 - line.x1);
!! important !! must firstly translate secondly rotate
tx.translate( cordx, cordy ); // setup of cord of arrowhead
tx.rotate((angle - Math.PI / 2d)); // head rotate
graphics.setTransform(tx); // set transform for graphics
graphics.fill(arrowHead);
graphics.setTransform(old_tx); // get original transform back
获取线位置和旋转的 CalcAffineTransformation 函数:
private AffineTransform calcAffineTransformation(Line2D.Double line) {
AffineTransform transformation = new AffineTransform();
transformation.setToIdentity();
double angle = Math.atan2(line.y2 - line.y1, line.x2 - line.x1);
transformation.translate(line.x2, line.y2);
transformation.rotate((angle - Math.PI / 2d));
return transformation;
}
就是这样。这就是代码的作用:
你的线有方向矢量
dx, dy = (x2 - x1), (y2 - y1)
它的长度是
len = sqrt(dx*dx + dy*dy)
单位方向矢量为
udx, udy = dx/len, dy/len
距离末端 D 处的点(据我了解,这是箭头的头点):
x3, y3 = x2 - D * udx, y2 - D * udy
你还需要其他东西来制作箭头吗?
我有积分
A(x1, y1)
B(x2, y2)
我需要画这条线的箭头,但不是在最后。它必须离终点有一段距离。这东西要怎么弄?
我有:
---------------------->
我需要:
----------------->-----
见图:
这是有角度的:
结果:
谢谢大家的帮助。这是另一个。
让我们创建主函数并画一条线和箭头:
private void drawLineWithArrowHead(Point from, Point to, Graphics2D graphics){
Polygon arrowHead = new Polygon();
arrowHead.addPoint( 0,6);
arrowHead.addPoint( -6, -6);
arrowHead.addPoint( 6,-6);int y1,y2,x1,x2;
x1=from.getPosX();
y1=from.getPosY();
x2=to.getPosX();
y2=to.getPosY();
Line2D.Double line = new Line2D.Double(x1,y1,x2,y2);
graphics.draw(line);
让我们加载旧的仿射变换并从行中获取新的:
AffineTransform tx, old_tx = graphics.getTransform();
tx = calcAffineTransformation(line);
一些数学:
double dx = (x2-x1), dy = (y2-y1);
double len = Math.sqrt(dx*dx + dy*dy);
double udx = dx/len, udy = dy/len;
double cordx = x2 - (size-5) * udx, cordy = y2 - (size-5) * udy;
double r_cordx = x2 - (size+3) * udx, r_cordy = y2 - (size+3) * udy;
现在放置箭头:
tx.setToIdentity(); // null transform to origin
double angle = Math.atan2(line.y2 - line.y1, line.x2 - line.x1);
!! important !! must firstly translate secondly rotate
tx.translate( cordx, cordy ); // setup of cord of arrowhead
tx.rotate((angle - Math.PI / 2d)); // head rotate
graphics.setTransform(tx); // set transform for graphics
graphics.fill(arrowHead);
graphics.setTransform(old_tx); // get original transform back
获取线位置和旋转的 CalcAffineTransformation 函数:
private AffineTransform calcAffineTransformation(Line2D.Double line) {
AffineTransform transformation = new AffineTransform();
transformation.setToIdentity();
double angle = Math.atan2(line.y2 - line.y1, line.x2 - line.x1);
transformation.translate(line.x2, line.y2);
transformation.rotate((angle - Math.PI / 2d));
return transformation;
}
就是这样。这就是代码的作用:
你的线有方向矢量
dx, dy = (x2 - x1), (y2 - y1)
它的长度是
len = sqrt(dx*dx + dy*dy)
单位方向矢量为
udx, udy = dx/len, dy/len
距离末端 D 处的点(据我了解,这是箭头的头点):
x3, y3 = x2 - D * udx, y2 - D * udy
你还需要其他东西来制作箭头吗?