Ejemplo n.º 1
0
        public static IPositionSet getBlockLine(IPosition refPos, float itv0, IPosition startPos, IPosition endPos)
        {
            rX  = refPos.GetX();
            rY  = refPos.GetY();
            itv = itv0;
            float sX = startPos.GetX();
            float sY = startPos.GetY();
            float eX = endPos.GetX();
            float eY = endPos.GetY();

            PositionSetEdit_ImplementByICollectionTemplate result = new PositionSetEdit_ImplementByICollectionTemplate();

            //判断起点终点是否相同
            if ((sX == eX) && (sY == eY))
            {
                return(result);
            }

            //判断起点终点横坐标是否相同

            //判断起点终点纵坐标是否相同

            int xDir = (eX > sX) ? 1 : (eX == sX) ? 0 : -1;
            int yDir = (eY > sY) ? 1 : (eY == sY) ? 0 : -1;

            //最简单的情况:一条线
            if (getM(sX) == getM(eX) && getN(sY) == getN(eY))
            {
                float px = getM(sX) * itv + itv / 2 + rX;
                float py = getN(sY) * itv + itv / 2 + rY;
                result.AddPosition(new Position_Point(px, py));
            }
            else if (getM(sX) == getM(eX))
            {
                for (int i = getN(sY); (getN(eY) - i) * yDir >= 0; i += yDir)
                {
                    float px = getM(sX) * itv + itv / 2 + rX;
                    float py = getN(sY) * itv + itv / 2 + rY;
                    result.AddPosition(new Position_Point(px, py));
                }
            }
            else if (getN(sY) == getN(eY))
            {
                for (int i = getM(sX); (getM(eX) - i) * xDir >= 0; i += xDir)
                {
                    float px = getM(sX) * itv + itv / 2 + rX;
                    float py = getN(sY) * itv + itv / 2 + rY;
                    result.AddPosition(new Position_Point(px, py));
                }
            }
            //一般情况:斜线
            float x = sX;
            float y = sY;
            float k = (eY - sY) / (eX - sX);
            float nextX, nextY;

            int offsetX = 0, offsetY = 0;

            if (xDir > 0)
            {
                nextX = (getM(sX) + 1) * itv + rX;
            }
            else
            {
                nextX = getM(sX) * itv + rX;
            }

            if (yDir > 0)
            {
                nextY = (getN(sY) + 1) * itv + rY;
            }
            else
            {
                nextY = getN(sY) * itv + rY;
            }

            while ((eX - x) * xDir >= 0 || (eY - y) * yDir >= 0)
            {
                float px = (getM(x) + offsetX) * itv + itv / 2 + rX;
                float py = (getN(y) + offsetY) * itv + itv / 2 + rY;
                offsetX = offsetY = 0;

                result.AddPosition(new Position_Point(px, py));

                float  dx = Math.Abs(nextX - x);
                float  dy = Math.Abs(nextY - y);
                double dk = dy / dx;
                if (dk > Math.Abs(k))
                {
                    x = nextX;
                    if (xDir < 0)
                    {
                        offsetX = -1;
                    }
                    nextX += itv * xDir;
                    y      = sY + (x - sX) * k;
                }
                else if (dk < Math.Abs(k))
                {
                    y = nextY;
                    if (yDir < 0)
                    {
                        offsetY = -1;
                    }
                    nextY += itv * yDir;
                    x      = sX + (y - sY) / k;
                }
                else
                {
                    x = nextX;
                    y = nextY;
                    if (xDir < 0)
                    {
                        offsetX = -1;
                    }
                    if (yDir < 0)
                    {
                        offsetY = -1;
                    }
                    nextX += itv * xDir;
                    nextY += itv * yDir;
                }
            }


            IList <IPosition> poslist = result.ToIlist();

            //去掉重复点
            for (int i = 0; i < poslist.Count - 1;)
            {
                if (poslist[i].GetX() == poslist[i + 1].GetX() && poslist[i].GetY() == poslist[i + 1].GetY())
                {
                    poslist.RemoveAt(i + 1);
                }
                else
                {
                    i++;
                }
            }
            //去掉两个端点
            poslist.RemoveAt(0);
            poslist.RemoveAt(poslist.Count - 1);

            return(result);
        }