Пример #1
0
 private static void EllipsePlot(double cx, double cy, double x, double y)
 {
     CSys.SetDotLocInfor(new Point(cx, cy));
     CSys.SetDotLocInfor(new Point(cx + x, cy + y));
     CSys.SetDotLocInfor(new Point(cx - x, cy + y));
     CSys.SetDotLocInfor(new Point(cx + x, cy - y));
     CSys.SetDotLocInfor(new Point(cx - x, cy - y));
 }
 /// <summary>
 /// 开始平移
 /// </summary>
 public static void BeginTranslation()
 {
     for (int i = 0; i < TranslationDistance; i++)
     {
         CSys.RefreshCordinateSys();
         StraitLineAlgo.DrawStraitLineDDA(new pointLoc(i + 1, 0), new pointLoc(i + 1, TranslationLinelength));
         CSys.DrawDots();
     }
 }
 /// <summary>
 /// DDA画线法
 /// </summary>
 /// <param name="Sx">起点横坐标</param>
 /// <param name="Sy">起点纵坐标</param>
 /// <param name="Ex">终点横坐标</param>
 /// <param name="Ey">终点纵坐标</param>
 public static void DrawStraitLineDDA(double Sx, double Sy, double Ex, double Ey)
 {
     if (Sx < Ex)//直线趋势向右
     {
         double y = Sy, k;
         k = (Ey - Sy) / (Ex - Sx);
         for (double x = Sx; x <= Ex; x++)
         {
             CSys.SetDotLocInfor(new Point(x, y));
             y = y + k;
         }
     }
     else if (Sx > Ex)//直线趋势向左
     {
         double k, tmpX, tmpY;
         tmpX = Sx;//互换输入起点终点的横坐标
         Sx   = Ex;
         Ex   = tmpX;
         tmpY = Sy;//互换输入起点终点的纵坐标
         Sy   = Ey;
         Ey   = tmpY;
         double y = Sy;
         k = (Ey - Sy) / (Ex - Sx);
         for (double x = Sx; x <= Ex; x++)
         {
             CSys.SetDotLocInfor(new Point(x, y));
             y = y + k;
         }
     }
     else//直线竖直
     {
         if (Sy < Ey)//从上往下画线
         {
             double x = Sx;
             for (double y = Sy; y <= Ey; y++)
             {
                 CSys.SetDotLocInfor(new Point(x, y));
             }
         }
         else//从下往上画线
         {
             double tmpX, tmpY;
             tmpX = Sx;//互换输入起点终点的横坐标
             Sx   = Ex;
             Ex   = tmpX;
             tmpY = Sy;//互换输入起点终点的纵坐标
             Sy   = Ey;
             Ey   = tmpY;
             double x = Sx;
             for (double y = Sy; y <= Ey; y++)
             {
                 CSys.SetDotLocInfor(new Point(x, y));
             }
         }
     }
 }
 /// <summary>
 /// 矩形生成算法-填充
 /// </summary>
 /// <param name="width">宽</param>
 /// <param name="height">高</param>
 /// <param name="Sx">矩形左上角点横坐标</param>
 /// <param name="Sy">矩形左上角点纵坐标</param>
 public static void RectangleFillAlgo(int Sx, int Sy, int width, int height)
 {
     CSys.SetDotLocInfor(new Point(Sx, Sy));
     for (int i = 0; i < width; i++)
     {
         for (int j = 0; j < height; j++)
         {
             CSys.SetDotLocInfor(new Point(Sx + i, Sy + j));
         }
     }
 }
Пример #5
0
 /// <summary>
 /// 中点画圆算法设置点的位置信息
 /// </summary>
 /// <param name="Cx">圆心横坐标</param>
 /// <param name="Cy">圆心纵坐标</param>
 private static void SetPointsLocInfor(double Cx, double Cy, double x, double y)
 {
     CSys.SetDotLocInfor(new Point(Cx, Cy));
     CSys.SetDotLocInfor(new Point(Cx + x, Cy + y));
     CSys.SetDotLocInfor(new Point(Cx - x, Cy + y));
     CSys.SetDotLocInfor(new Point(Cx + x, Cy - y));
     CSys.SetDotLocInfor(new Point(Cx - x, Cy - y));
     CSys.SetDotLocInfor(new Point(Cx + y, Cy + x));
     CSys.SetDotLocInfor(new Point(Cx - y, Cy + x));
     CSys.SetDotLocInfor(new Point(Cx + y, Cy - x));
     CSys.SetDotLocInfor(new Point(Cx - y, Cy - x));
 }
        /// <summary>
        /// 开始旋转。旋转的方向为顺时针
        /// </summary>
        /// <param name="angle">旋转的角度</param>
        public static void BeginRotate(double angle)
        {
            for (int i = 0; i < CSys.CellNum; i++)
            {
                for (int j = 0; j < CSys.CellNum; j++)
                {
                    if (CSys.CanvasDotsArray[i, j] == 1)
                    {
                        double x = i * Math.Cos(angle) - j * Math.Sin(angle);
                        double y = j * Math.Cos(angle) + i * Math.Sin(angle);

                        CSys.SetDotLocInfor(new System.Windows.Point(Math.Floor(x), Math.Floor(y)));
                    }
                }
            }

            CSys.DrawDots();
        }
Пример #7
0
        /// <summary>
        /// 加载指定图像文件,此函数仅供内部类调用
        /// </summary>
        /// <param name="fileName">文件路径。如@"xx/xx/xx.bmp"</param>
        private static void LoadBmpFile(string fileName)
        {
            Bitmap bitmap = new Bitmap(fileName);
            Color  color;

            for (int i = 0; i < bitmap.Width; i++)
            {
                for (int j = 0; j < bitmap.Height; j++)
                {
                    color = bitmap.GetPixel(i, j);

                    if (color.Name != "ffffffff")
                    {
                        CSys.SetDotLocInfor(new System.Windows.Point(i, j));
                    }
                }
            }
        }
        /// <summary>
        /// Bresenham画线算法
        /// </summary>
        /// <param name="Sx">起点横坐标</param>
        /// <param name="Sy">起点纵坐标</param>
        /// <param name="Ex">终点横坐标</param>
        /// <param name="Ey">终点纵坐标</param>
        private static void BresenhamStraitLine(double Sx, double Sy, double Ex, double Ey)
        {
            double dx = Ex - Sx, dy = Ey - Sy, x = Sx, y = Sy, f = 2 * dy - dx, k = dy / dx;

            CSys.SetDotLocInfor(new Point(x, y));
            while (x < Ex)
            {
                if (f < 0)
                {
                    f += 2 * dy;
                }
                else
                {
                    f += 2 * (dy - dx);
                    y += k;
                }
                x++;
                CSys.SetDotLocInfor(new Point(x, y));
            }
        }
        /// <summary>
        /// 双步画线法
        /// </summary>
        /// <param name="Sx">起点横坐标</param>
        /// <param name="Sy">起点纵坐标</param>
        /// <param name="Ex">终点横坐标</param>
        /// <param name="Ey">终点纵坐标</param>
        public static void TwoStepsDrawStraitLine(double Sx, double Sy, double Ex, double Ey)
        {
            double dx, dy, x = Sx, f, y = Sy, k;

            dx = Ex - Sx;
            dy = Ey - Sy;
            k  = dy / dx;

            if (k > 0 && !double.IsInfinity(k)) //包含从左上到右下画线,右下到左上画线
            {
                if (dx > 0 && dy > 0)           //从左上到右下画线
                {
                    #region k>0, dy>0, dx>0
                    if (k > 0 && k <= 0.5)
                    {
                        f = 4 * dy - dx;
                        CSys.SetDotLocInfor(new Point(Sx, Sy));
                        while (x < Ex)
                        {
                            if (f < 0)
                            {
                                CSys.SetDotLocInfor(new Point(x + 1, y));
                                CSys.SetDotLocInfor(new Point(x + 2, y));
                                f += 4 * dy;
                            }
                            else
                            {
                                if (f < 2 * dy)
                                {
                                    CSys.SetDotLocInfor(new Point(x + 1, y));
                                    CSys.SetDotLocInfor(new Point(x + 2, y + 1));
                                    y += 1;
                                }
                                else
                                {
                                    CSys.SetDotLocInfor(new Point(x + 1, y + 1));
                                    CSys.SetDotLocInfor(new Point(x + 2, y + 1));
                                    y += 1;
                                }
                                f += 4 * dy - 2 * dx;
                            }
                            x += 2;
                        }
                    }
                    else if (k <= 1)
                    {
                        f = 4 * dy - 3 * dx;
                        CSys.SetDotLocInfor(new Point(Sx, Sy));

                        while (x < Ex)
                        {
                            if (f >= 0)
                            {
                                CSys.SetDotLocInfor(new Point(x + 1, y + 1));
                                CSys.SetDotLocInfor(new Point(x + 2, y + 2));
                                f += 4 * (dy - dx);
                                y += 2;
                            }
                            else
                            {
                                if (f < 2 * (dy - dx))
                                {
                                    CSys.SetDotLocInfor(new Point(x + 1, y));
                                    CSys.SetDotLocInfor(new Point(x + 2, y + 1));
                                    y += 1;
                                }
                                else
                                {
                                    CSys.SetDotLocInfor(new Point(x + 1, y + 1));
                                    CSys.SetDotLocInfor(new Point(x + 2, y + 1));
                                    y += 1;
                                }
                                f += 4 * dy - 2 * dx;
                            }
                            x += 2;
                        }
                    }
                    else//k>1
                    {
                    }
                    #endregion
                }
                else//从右下到左上画线
                {
                    #region k>0, dy<0, dx<0
                    double tmpX, tmpY;
                    tmpX = Sx;//互换输入起点终点的横坐标
                    Sx   = Ex;
                    Ex   = tmpX;
                    tmpY = Sy;//互换输入起点终点的纵坐标
                    Sy   = Ey;
                    Ey   = tmpY;

                    dx = Ex - Sx;//重新给各个变量赋值
                    dy = Ey - Sy;
                    x  = Sx;
                    y  = Sy;
                    k  = dy / dx;

                    if (k >= 0 && k <= 0.5)
                    {
                        f = 4 * dy - dx;
                        CSys.SetDotLocInfor(new Point(Sx, Sy));
                        while (x < Ex)
                        {
                            if (f < 0)
                            {
                                CSys.SetDotLocInfor(new Point(x + 1, y));
                                CSys.SetDotLocInfor(new Point(x + 2, y));
                                f += 4 * dy;
                            }
                            else
                            {
                                if (f < 2 * dy)
                                {
                                    CSys.SetDotLocInfor(new Point(x + 1, y));
                                    CSys.SetDotLocInfor(new Point(x + 2, y + 1));
                                    y += 1;
                                }
                                else
                                {
                                    CSys.SetDotLocInfor(new Point(x + 1, y + 1));
                                    CSys.SetDotLocInfor(new Point(x + 2, y + 1));
                                    y += 1;
                                }
                                f += 4 * dy - 2 * dx;
                            }
                            x += 2;
                        }
                    }
                    else if (k <= 1)
                    {
                        f = 4 * dy - 3 * dx;
                        CSys.SetDotLocInfor(new Point(Sx, Sy));

                        while (x < Ex)
                        {
                            if (f >= 0)
                            {
                                CSys.SetDotLocInfor(new Point(x + 1, y + 1));
                                CSys.SetDotLocInfor(new Point(x + 2, y + 2));
                                f += 4 * (dy - dx);
                                y += 2;
                            }
                            else
                            {
                                if (f < 2 * (dy - dx))
                                {
                                    CSys.SetDotLocInfor(new Point(x + 1, y));
                                    CSys.SetDotLocInfor(new Point(x + 2, y + 1));
                                    y += 1;
                                }
                                else
                                {
                                    CSys.SetDotLocInfor(new Point(x + 1, y + 1));
                                    CSys.SetDotLocInfor(new Point(x + 2, y + 1));
                                    y += 1;
                                }
                                f += 4 * dy - 2 * dx;
                            }
                            x += 2;
                        }
                    }
                    else//k>1
                    {
                    }
                    #endregion
                }
            }
            else if (k < 0 && !double.IsNegativeInfinity(k)) //包含从左下到右上画线、右上到左下画线
            {
                if (dx > 0 && dy < 0)                        //从左下到右上画线
                {
                }
                else//从右上到左下画线
                {
                }
            }
            else if (k == 0)  //直线水平
            {
                if (Sx <= Ex) //自左往右画线
                {
                    for (x = Sx; x < Ex; x++)
                    {
                        CSys.SetDotLocInfor(new Point(x, y));
                    }
                }
                else//自右往左画线
                {
                    double tmpX;
                    tmpX = Sx;//互换输入起点终点的横坐标
                    Sx   = Ex;
                    Ex   = tmpX;

                    for (x = Sx; x < Ex; x++)
                    {
                        CSys.SetDotLocInfor(new Point(x, y));
                    }
                }
            }
            else//直线竖直
            {
                if (Sy < Ey)//自上往下画线
                {
                    for (y = Sy; y < Ey; y++)
                    {
                        CSys.SetDotLocInfor(new Point(x, y));
                    }
                }
                else//自下往上画线
                {
                    double tmpY;
                    tmpY = Sy;//互换输入起点终点的纵坐标
                    Sy   = Ey;
                    Ey   = tmpY;

                    x = Sx;
                    for (y = Sy; y < Ey; y++)
                    {
                        CSys.SetDotLocInfor(new Point(x, y));
                    }
                }
            }
        }
Пример #10
0
 /// <summary>
 /// 刷新坐标系
 /// </summary>
 public static void RefreshCordinateSys()
 {
     CSys.ClearCSys();        //清除子Canvas的子元素
     CSys.ShowGrid(50);       //重绘网格
     CSys.ClearDotLocInfor(); //清除点信息
 }