Draw parallel Line

后端 未结 1 1110
旧时难觅i
旧时难觅i 2020-12-20 06:33

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 intersec

相关标签:
1条回答
  • 2020-12-20 06:58

    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

    0 讨论(0)
提交回复
热议问题