【问题标题】:Create Lines around a guiding line using Miterjoin使用 Miterjoin 围绕引导线创建线
【发布时间】:2011-02-09 22:21:08
【问题描述】:

我正在将图形绘制到 WinForms 图片框中。现在我正在寻找“复制”一条线(点数组)的可能性,以便两条生成的线与原始线相距固定距离。就像在这张照片中,我有红线,想得到黑线:

Picture of lines http://img227.imageshack.us/img227/2341/linesb.png

我想只是将线向上/向右/向上移动几个像素,但这会导致奇怪的重叠线。

还有其他方法可以满足我的要求吗?任何想法将不胜感激。谢谢!

【问题讨论】:

  • 注意黑线和红线的大小不一样。

标签: c# .net winforms drawing2d miter


【解决方案1】:

我创建了一个函数,它完全在几个月前作为图形布局算法的一部分完成了您需要的工作。我是用 python 和 PyQt 写的。我刚刚粘贴了代码here at codepad。这应该很容易翻译成 c#。

更新:

从我的 python sn-p 一对一翻译它(喜欢做那些图形的东西:))。由于我的原始代码是为两个以上的输出行而设计的,所以我也将其带入了 c# 版本。对于距离红线 20 像素的两条黑线,只需通过 width = 40num = 2。返回的锯齿状数组表示一个线数组(外部数组),每条线由一个点数组(内部)表示。

public PointF[][] MultiplyLine(PointF[] line, int width, int num)
{
    if (num == 1) return new PointF[][] { line };
    if (num < 1) throw new ArgumentOutOfRangeException();
    if (line.Length < 2) return Enumerable.Range(0, num)
                  .Select(x => line).ToArray();

    Func<float, float, PointF> normVec = (x, y) => {
        float len = (float)Math.Sqrt((double)(x * x + y * y));
        return len == 0 ? new PointF(1f, 0f) : new PointF(x / len, y / len);
    };

    PointF[][] newLines = Enumerable.Range(0, num)
                  .Select(x => new PointF[line.Length]).ToArray();

    float numinv = 1f / (float)(num - 1), cor = 0f;
    PointF vec1 = PointF.Empty, vec2 = PointF.Empty, vec3 = PointF.Empty;

    int j = -1, i = -1;
    foreach (PointF p in line)
    {
        bool first = j == -1, last = j == line.Length - 2; j++;

        if (!last)
            vec1 = normVec(line[j + 1].Y - p.Y, -line[j + 1].X + p.X);
        if (!first)
            vec2 = normVec(-line[j - 1].Y + p.Y, line[j - 1].X - p.X);
        if (!first && !last)
        {
            vec3 = normVec(vec1.X + vec2.X, vec1.Y + vec2.Y);
            cor = (float)Math.Sin((Math.PI - 
                  Math.Acos(vec1.X * vec2.X + vec1.Y * vec2.Y)) / 2);
            cor = cor == 0 ? 1 : cor;
            vec3 = new PointF(vec3.X / cor, vec3.Y / cor);
        }

        i = -1;
        foreach (PointF[] newLine in newLines)
        {
            i++; cor = (float)width * ((float)i * numinv - 0.5f);
            vec1 = first ? vec1 : last ? vec2 : vec3;
            newLine[j] = new PointF(vec1.X * cor + p.X, vec1.Y * cor + p.Y);
        }
    }

    return newLines;
}

为了试用,我使用了这个小示例(与我的 PyQt 代码中的示例相同):

PointF[] pts = new PointF[] { 
    new PointF(100f, 100f), new PointF(300f, 200f), 
    new PointF(500f, 200f), new PointF(300f, 500f), 
    new PointF(600f, 450f), new PointF(650f, 180f), 
    new PointF(800f, 180f), new PointF(800f, 500f), 
    new PointF(200f, 700f)
};

pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
using(Graphics g = Graphics.FromImage(pictureBox1.Image)){
    g.DrawLines(new Pen(Color.Red), pts);

    foreach (PointF[] line in MultiplyLine(pts, 80, 14))
        g.DrawLines(new Pen(Color.Black), line);
}

这导致了这个图形:

outlines around line http://img41.imageshack.us/img41/8606/lines2.th.png

【讨论】:

  • 这似乎很有希望。恐怕我对python一无所知。
  • 哇,太棒了!非常感谢,这正是我想要的,并且是一个非常快速和详细的答案!
猜你喜欢
  • 2012-07-26
  • 2016-11-07
  • 1970-01-01
  • 2019-10-06
  • 2014-01-13
  • 1970-01-01
  • 2012-01-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多