Algorithm 在线绘制箭头算法 是否有人在给定直线中间画一个箭头的算法。我搜索过谷歌,但没有找到任何好的实现
另外,我真的不介意这种语言,但如果它是Java,那就太好了,因为它是我在这方面使用的语言Algorithm 在线绘制箭头算法 是否有人在给定直线中间画一个箭头的算法。我搜索过谷歌,但没有找到任何好的实现,algorithm,drawing,line,Algorithm,Drawing,Line,另外,我真的不介意这种语言,但如果它是Java,那就太好了,因为它是我在这方面使用的语言 提前感谢。这里有一个函数,可以将箭头的头部画在p点上。您可以将其设置为直线的中点。dx和dy是线的方向,由(x1-x0,y1-y0)给出。这将给出一个按线长度缩放的箭头。如果希望箭头始终保持相同大小,请规格化此方向 private static void DrawArrow(Graphics g, Pen pen, Point p, float dx, float dy) { const doubl
提前感谢。这里有一个函数,可以将箭头的头部画在p点上。您可以将其设置为直线的中点。dx和dy是线的方向,由(x1-x0,y1-y0)给出。这将给出一个按线长度缩放的箭头。如果希望箭头始终保持相同大小,请规格化此方向
private static void DrawArrow(Graphics g, Pen pen, Point p, float dx, float dy)
{
const double cos = 0.866;
const double sin = 0.500;
PointF end1 = new PointF(
(float)(p.X + (dx * cos + dy * -sin)),
(float)(p.Y + (dx * sin + dy * cos)));
PointF end2 = new PointF(
(float)(p.X + (dx * cos + dy * sin)),
(float)(p.Y + (dx * -sin + dy * cos)));
g.DrawLine(pen, p, end1);
g.DrawLine(pen, p, end2);
}
这里有一个向直线添加箭头的方法。 你只需要给它你的箭头和尾巴的坐标
private static void drawArrow(int tipX, int tailX, int tipY, int tailY, Graphics2D g)
{
int arrowLength = 7; //can be adjusted
int dx = tipX - tailX;
int dy = tipY - tailY;
double theta = Math.atan2(dy, dx);
double rad = Math.toRadians(35); //35 angle, can be adjusted
double x = tipX - arrowLength * Math.cos(theta + rad);
double y = tipY - arrowLength * Math.sin(theta + rad);
double phi2 = Math.toRadians(-35);//-35 angle, can be adjusted
double x2 = tipX - arrowLength * Math.cos(theta + phi2);
double y2 = tipY - arrowLength * Math.sin(theta + phi2);
int[] arrowYs = new int[3];
arrowYs[0] = tipY;
arrowYs[1] = (int) y;
arrowYs[2] = (int) y2;
int[] arrowXs = new int[3];
arrowXs[0] = tipX;
arrowXs[1] = (int) x;
arrowXs[2] = (int) x2;
g.fillPolygon(arrowXs, arrowYs, 3);
}
这是一个指向直线中间的箭头吗?还是来自它?还是沿着直线的箭头?沿着直线的箭头。如何调整箭头的大小?我喜欢根据线的长度设置,但当前箭头大小与线大小的比率不适合我的应用。