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.
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.
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).
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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…