如何在 .Net 中绘制有向线?

发布于 2024-10-03 17:54:58 字数 408 浏览 2 评论 0原文

关于如何在 .Net 中绘制有向线有什么建议吗?我目前正在使用 Graphics.DrawLine 和带破折号的笔。但破折号指向两个方向。我希望它们都指向一个方向。

using (var p = new Pen(Color.FromArgb(190, Color.Green), 5))
{
    p.StartCap = LineCap.Round;
    p.EndCap = LineCap.ArrowAnchor;
    p.CustomEndCap = new AdjustableArrowCap(3, 3);
    p.DashStyle = DashStyle.Dash;
    p.DashCap = DashCap.Triangle;
    e.Graphics.DrawLine(p, startPt, pt);
}

Any suggestions on how to draw a directed line in .Net? I'm currently using Graphics.DrawLine with a Pen with dashes. But the dashes point in both directions. I want them to all point in a single direction.

using (var p = new Pen(Color.FromArgb(190, Color.Green), 5))
{
    p.StartCap = LineCap.Round;
    p.EndCap = LineCap.ArrowAnchor;
    p.CustomEndCap = new AdjustableArrowCap(3, 3);
    p.DashStyle = DashStyle.Dash;
    p.DashCap = DashCap.Triangle;
    e.Graphics.DrawLine(p, startPt, pt);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牵你的手,一向走下去 2024-10-10 17:54:58

这可能不是很有帮助,但是我们在我参与的其他项目中完成此类事情的方法是实现一个自定义“笔”,它接受要绘制的线,计算实际的破折号,然后绘制每个破折号都是带有“真实”笔的单独线条。如果您要做这样的事情,那么您实际上可以将“笔”级 StartCap 和/或 EndCap 应用于每个破折号。

自定义“笔”必须接受破折号/间隙长度的数组或序列(或者可能是标准笔样式的枚举,这些样式将在内部解释为特定的破折号/间隙长度)。

许多年前,我们使用基于 GDI 的绘图系统做到了这一点,我们必须支持比 GDI 支持的内置线条样式更多的线条样式。

这是一个有趣的练习,但它也可能比它的价值更麻烦,除非你真的必须按照你所描述的方式绘制破折号。

[编辑]

如果您愿意的话,这里有一些示例代码,说明您可以如何执行此操作。

第一个是 Graphics 对象的扩展方法。它需要一支笔、虚线长度、间隙长度、起点和终点。它沿着线 p1->p2 进行插值,绘制“dash”长度段,然后跳过“gap”长度段。传入的笔应该是实心的,带有箭头端盖(以达到您要求的效果)。

代码相当粗糙,但行为或多或少是这样的:
如果输入线比总破折号+间隙长度短,则使用输入笔按原样绘制该线。
如果输入线长于总的破折号+间隙长度,则绘制“破折号”长度的线,直到该线的“剩余部分”小于破折号+间隙长度,此时剩余部分按原样绘制输入笔。

如果您想实现路径绘制,那么您必须在中间顶点周围进行插值(除非您想节省成本并独立计算每个线段的破折号)。

    public static void DrawDashedLine(this Graphics g, Pen p, float dash, float gap, PointF s, PointF e)
    {
      float dx = e.X - s.X;
      float dy = e.Y - s.Y;

      float len = (float)Math.Sqrt(dx * dx + dy * dy);
      float remainder = len;

      float vx = dx / len;
      float vy = dy / len;

      if (len <= dash + gap)
      {
        g.DrawLine(p, s, e);
        return;
      }

      PointF last = s;

      while (remainder > dash + gap)
      {
        PointF p1 = new PointF(last.X, last.Y);
        PointF p2 = new PointF(p1.X + vx*dash, p1.Y + vy*dash);

        g.DrawLine(p, p1, p2);

        last = new PointF(p2.X + vx*gap, p2.X + vy*gap);

        remainder = remainder - dash - gap;
      }

      if (remainder > 0)
      {
        g.DrawLine(p, last, e);
      }
    }
  }

您可以这样称呼它:

private void button1_Click(object sender, EventArgs e)
{
  using (var p = new Pen(Color.FromArgb(190, Color.Green), 5))
  {
    p.StartCap = LineCap.Round;
    p.EndCap = LineCap.ArrowAnchor;
    p.CustomEndCap = new AdjustableArrowCap(3, 3);
    p.DashStyle = DashStyle.Solid;
    var graph = this.CreateGraphics();
    graph.DrawDashedLine(p, 20, 10, new PointF(20, 20), new PointF(500, 500));
  } 
}

我并不声称该代码很棒(甚至不一定很好;-),但如果您确实对绘制有向线感兴趣,它应该为您提供一个良好的起点。

祝你好运!

This might not be very helpful, but the way that we accomplished this type of thing on other projects that I have been involved with is to implement a custom "pen" that accepts the line to be drawn, calculates the actual dashes, and then draws each dash as an individual line with a "real" Pen. If you were to do something like this, then you could actually apply a "pen" level StartCap and/or EndCap to each dash.

The custom "pen" would have to accept an array or sequence of dash/gap lengths (or maybe an enumeration of standard pen styles that would be interpreted internally as specific dash/gap lengths).

We did this many years ago with a GDI-based drawing system where we had to support more line styles than the built in line styles that GDI supported.

It is an interesting exercise, but it is also probably more trouble than it is worth unless you REALLY have to have the dashes drawn as you describe.

[EDIT]

Here is some sample code for how you might do this, if you are so inclined.

First is an extension method on the Graphics object. It takes a pen, dash length, gap length, start point, and end point. It interpolates along the line p1->p2, drawing a "dash" length segment and then skipping a "gap" length segment. The passed-in pen should be solid with an arrow endcap (to achieve the effect you were asking for).

The code is pretty rough, but the behavior is, more or less, like this:
If the input line is shorter then the total dash+gap length, then the line is drawn as-is using the input pen.
If the input line is longer than the total dash+gap length, then "dash" length lines are drawn until the "remainder" of the line is less than the dash+gap length, at which point the remainder is drawn as-is with the input pen.

If you wanted to implement Path drawing, then you would have to interpolate around the intermediate vertices (unless you wanted to cheap out and just compute dashes for each segment indepdently).

    public static void DrawDashedLine(this Graphics g, Pen p, float dash, float gap, PointF s, PointF e)
    {
      float dx = e.X - s.X;
      float dy = e.Y - s.Y;

      float len = (float)Math.Sqrt(dx * dx + dy * dy);
      float remainder = len;

      float vx = dx / len;
      float vy = dy / len;

      if (len <= dash + gap)
      {
        g.DrawLine(p, s, e);
        return;
      }

      PointF last = s;

      while (remainder > dash + gap)
      {
        PointF p1 = new PointF(last.X, last.Y);
        PointF p2 = new PointF(p1.X + vx*dash, p1.Y + vy*dash);

        g.DrawLine(p, p1, p2);

        last = new PointF(p2.X + vx*gap, p2.X + vy*gap);

        remainder = remainder - dash - gap;
      }

      if (remainder > 0)
      {
        g.DrawLine(p, last, e);
      }
    }
  }

Here is how you would call it:

private void button1_Click(object sender, EventArgs e)
{
  using (var p = new Pen(Color.FromArgb(190, Color.Green), 5))
  {
    p.StartCap = LineCap.Round;
    p.EndCap = LineCap.ArrowAnchor;
    p.CustomEndCap = new AdjustableArrowCap(3, 3);
    p.DashStyle = DashStyle.Solid;
    var graph = this.CreateGraphics();
    graph.DrawDashedLine(p, 20, 10, new PointF(20, 20), new PointF(500, 500));
  } 
}

I don't claim that the code is great (or even necessarily good ;-), but it should give you a good starting point if you are really interested in drawing your directed line.

Good luck!

乖乖 2024-10-10 17:54:58

嗯...也许我遗漏了一些东西,但是当我使用这个(几乎)完全相同的代码时,我得到一个只有一个点的定向箭头。

这是我的代码:

private void button2_Click(object sender, EventArgs e)
{
    using (var p = new Pen(Color.FromArgb(190, Color.Green), 5))
    {
        p.StartCap = LineCap.Round;
        p.EndCap = LineCap.ArrowAnchor;
        p.CustomEndCap = new AdjustableArrowCap(3, 3);
        p.DashStyle = DashStyle.Dash;
        p.DashCap = DashCap.Triangle;
        var graph = this.CreateGraphics();
        graph.DrawLine(p, new Point(20, 20), new Point(150, 150));
    }
}

和输出:
替代文本

Hmm... maybe I am missing something, but when I use this (almost) exact same code, I get a directed arrow with only one point.

Here is my code:

private void button2_Click(object sender, EventArgs e)
{
    using (var p = new Pen(Color.FromArgb(190, Color.Green), 5))
    {
        p.StartCap = LineCap.Round;
        p.EndCap = LineCap.ArrowAnchor;
        p.CustomEndCap = new AdjustableArrowCap(3, 3);
        p.DashStyle = DashStyle.Dash;
        p.DashCap = DashCap.Triangle;
        var graph = this.CreateGraphics();
        graph.DrawLine(p, new Point(20, 20), new Point(150, 150));
    }
}

And the output:
alt text

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文