Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用斜接创建围绕引导线的线_C#_.net_Winforms_Drawing2d_Miter - Fatal编程技术网

C# 使用斜接创建围绕引导线的线

C# 使用斜接创建围绕引导线的线,c#,.net,winforms,drawing2d,miter,C#,.net,Winforms,Drawing2d,Miter,我正在将图形绘制到WinForms Picturebox中。现在,我正在寻找一种可能性来“复制”一条线(一组点),这样两条结果线的位置就会与原始线保持固定距离。就像在这张照片中,我有红线,想得到黑色的: 我想把线向上/向右/向右上移动几个像素,但这会导致奇怪的重叠线 有没有其他方法可以满足我的要求?任何想法都将不胜感激。谢谢 作为图形布局算法的一部分,我创建了一个函数,它可以完全满足您几个月前的需要。我用python和PyQt写的。我刚刚粘贴了代码。这应该很容易翻译成c 更新: 从我的pyth

我正在将图形绘制到WinForms Picturebox中。现在,我正在寻找一种可能性来“复制”一条线(一组点),这样两条结果线的位置就会与原始线保持固定距离。就像在这张照片中,我有红线,想得到黑色的:

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


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

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

更新:

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

这导致了这张图:


请注意,黑线和红线的大小不一样。这似乎很有希望。恐怕我对python一无所知。哇,太棒了!非常感谢,这正是我想要的,一个非常快速和详细的答案!
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;
}
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);
}