Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
596 views
in Technique[技术] by (71.8m points)

c# - Draw parallel Line

I have a set of points representing a line. It might be a closed shape or an open one. I need to draw a parallel line that goes besides the original one without any intersection.

I have the following code to return the generated line. I have problems in the angles of the shape. Some point goes over the original line.

My Code is:

PointF[] GetParrarel(PointF[] lst, double width, float distance)
{
    List<PointF> final = new List<PointF>();
    width = width + distance;

    for (int i = 0; i < lst.Length-1 ; i++)
    {
        int index = i;
        PointF current = lst[index];
        PointF next = lst[index + 1];
        double dx = next.X - current.X;
        double dy = next.Y - current.Y;
        PointF first = current;
        PointF second = next;
        if (dx > 0)
        {
            if (dy == 0)
            {
                first.Y += (float)width;
                second.Y += (float)width;
            }
            first.X += (float)width;
            second.X += (float)width;

        }
        else if (dx < 0)
        {
            if (dy == 0)
            {
                first.Y -= (float)width;
                second.Y -= (float)width;
            }
            first.X -= (float)width;
            second.X -= (float)width;                    
        }
        else //// X = 0 
        {
            if (dy > 0)
            {
                first.X -= (float)width;
                second.X -= (float)width;
            }
            else if (dy < 0)
            {
                first.X += (float)width;
                second.X += (float)width;                       
            }
            else
            {
                continue;
            }
        }
        final.Add(first);
        final.Add(second);
    }
    return final.ToArray();
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I figured out how to do it, but it is complex. Here is a screenshot of an example I did. Screenshot

I needed three classes for this.

  1. class Line which describes an infinite line using the coefficients a, b, c for the equation a*x+b*y+c=0. The constructor takes two PointF and calculates the coefficients. The line has a "center" which is the point closest to the origin. Any point along the line can be described as a distance from the "center" along the line direction.

  2. class LineSeg which describes a line segment, by specifying a line, as well as a starting and ending distance from the line center (see above).

  3. class PolyLine which is just a collection of LineSeg and can be initialized by a list of PointF. I have added an option to describe a closed poly-line by adding a line segment to the initial point in the end.

The offset of an infinite line is calculated by taking a point on the line and moving it in a direction normal to the line and then calculating the new coefficient c through that point with the same direction (c -> c + offset*sqrt(a^2+b^2)). The resulting infinite line is "trimmed" by it's neighboring lines by finding their intersection points.

To fully explain is complicated, but you are free to explore the source code and ask questions. The source code for the project is accessible here.

NOTE: The distance of a line with equation a*x+b*y+c=0 to the origin is distance = c/sqrt(a^2+b^2). So to offset a line you need a new c that results in the new distance to be distance+offset


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...