本文介绍了绘制平行线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组代表一条线的点。它可能是封闭的形状或开放的形状。
我需要绘制一条平行线,除了原始线之外没有任何交点。

我有以下代码来返回生成的线。我在形状的角度有问题。



我的代码是:

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

for(int i = 0; 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;

$ b其他if(dx {
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)宽度;
second.X - =(float)width;
}
else if(dy {
first.X + =(float)width;
second.X + =(float)width;
}
else
{
continue;
}
}
final.Add(first);
final.Add(second);
}
return final.ToArray();
}


解决方案

我想出了如何做它,但它很复杂。这是我做的一个例子的截图。



我需要三个班级为了这。


  1. class Line 它使用系数<$描述无限长线对于等式 a , b c > A * X + b * Y + C = 0 。构造函数接受两个 PointF 并计算系数。该线具有最接近原点的中心。沿着线的任何点都可以被描述为沿线方向与中心的距离。

  2. class LineSeg 它通过指定一条线来描述一条线段,以及一条从线条中心开始和结束的距离(见上文)。 class PolyLine ,它只是 LineSeg 的集合,可以通过列表 PointF来初始化。我已经添加了一个选项来描述封闭的多行,最后在最初的点添加一个线段。


一条无限长线的偏移量是通过取一条线上的一点并将它沿垂直于线的方向移动然后计算系数 a b ,并且 c 通过该点以相同的方向。生成的无限行是通过找到它们的交点来修剪它的相邻行。



完全解释是复杂的,但您可以自由探索源代码并询问的问题。该项目的源代码可通过进行访问。


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();
}
解决方案

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 an 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 coefficients a, b, and c through that point with the same direction. The reaulting 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.

这篇关于绘制平行线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 12:18