Draw parallel Line

陌路散爱 提交于 2019-12-18 07:25:39

问题


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();
}

回答1:


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

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



来源:https://stackoverflow.com/questions/8450289/draw-parallel-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!