Exemple #1
0
        /// <summary>
        /// DONT FORGET TO SCALE ALL SO ONE PIXEL IS 1f
        /// </summary>
        public static void DrawLineFixedMinusValues(float x0float, float y0float, float x1float, float y1float, DelegateDDA1 del)
        {
            //UnityEngine.Debug.LogFormat("x0: {0}, y0: {1}, x1: {2}, y1: {3}", x0float, y0float, x1float, y1float);

            int x0int = ToInt(x0float);
            int y0int = ToInt(y0float);
            int x1int = ToInt(x1float);
            int y1int = ToInt(y1float);

            if (x0int == x1int)
            {
                if (y0int == y1int)  //inside one pixel
                {
                    del(x0int, y0int);
                    return;
                }
                //along x axis
                if (y0int < y1int)
                {
                    for (int y = y0int; y < y1int + 1; y++)
                    {
                        del(x0int, y);
                    }
                }
                else
                {
                    for (int y = y0int; y > y1int - 1; y--)
                    {
                        del(x0int, y);
                    }
                }
                return;
            }

            if (y0int == y1int)  //along y axis
            {
                if (x0int < x1int)
                {
                    for (int x = x0int; x < x1int + 1; x++)
                    {
                        del(x, y0int);
                    }
                }
                else
                {
                    for (int x = x0int; x > x1int - 1; x--)
                    {
                        del(x, y0int);
                    }
                }
                return;
            }

            //some funky DDA based line rasterization
            if (SomeMath.InRangeInclusive((x1float - x0float) / (y1float - y0float), -1, 1))     //y
            {
                float lineDirX = x1float - x0float;
                float lineDirY = y1float - y0float;
                float t0y, t1y;

                if (x0float < x1float)
                {
                    t0y = IntersectionY(x0int, x0float, y0float, lineDirX, lineDirY);
                    t1y = IntersectionY(x0int + 1, x0float, y0float, lineDirX, lineDirY);
                    //DrawPoint(x0int, t0y, Color.red, "t0");
                    //DrawPoint(x0int + 1, t1y, Color.green, "t1");
                }
                else
                {
                    t0y = IntersectionY(x0int + 1, x0float, y0float, lineDirX, lineDirY);
                    t1y = IntersectionY(x0int, x0float, y0float, lineDirX, lineDirY);
                    //DrawPoint(x0int + 1, t0y, Color.red, "t0");
                    //DrawPoint(x0int, t1y, Color.green, "t1");
                }

                int   xDelta    = Math.Sign(x1int - x0int);
                float yDelta    = t1y - t0y;
                float curYfloat = t0y;

                if (y1float < y0float)
                {
                    while (true)
                    {
                        curYfloat += yDelta;

                        for (; y0int > ToInt(curYfloat - 1); y0int--)
                        {
                            del(x0int, y0int);

                            if (y0int == y1int)
                            {
                                del(x1int, y1int);
                                return;
                            }
                        }
                        y0int  = ToInt(curYfloat);
                        x0int += xDelta;
                        //DrawPoint(x0int, curYfloat, Color.red, "y");
                    }
                }
                else
                {
                    while (true)
                    {
                        curYfloat += yDelta;

                        for (; y0int < ToInt(curYfloat + 1); y0int++)
                        {
                            del(x0int, y0int);

                            if (y0int == y1int)
                            {
                                if (x0int != x1int)
                                {
                                    del(x1int, y1int);
                                }
                                return;
                            }
                        }

                        y0int  = ToInt(curYfloat);
                        x0int += xDelta;
                        //DrawPoint(x0int, curYfloat, Color.red, "y");
                    }
                }
            }
            else     //x
            {
                float lineDirX = x1float - x0float;
                float lineDirY = y1float - y0float;
                float t0x, t1x;

                if (y0float < y1float)
                {
                    t0x = IntersectionX(y0int, x0float, y0float, lineDirX, lineDirY);
                    t1x = IntersectionX(y0int + 1, x0float, y0float, lineDirX, lineDirY);
                    //DrawPoint(t0x, y0int, Color.red, "t0");
                    //DrawPoint(t1x, y0int + 1, Color.green, "t1");
                }
                else
                {
                    t1x = IntersectionX(y0int, x0float, y0float, lineDirX, lineDirY);
                    t0x = IntersectionX(y0int + 1, x0float, y0float, lineDirX, lineDirY);
                    //DrawPoint(t1x, y0int, Color.green, "t1");
                    //DrawPoint(t0x, y0int + 1, Color.red, "t0");
                }

                int   yDelta    = Math.Sign(y1int - y0int);
                float xDelta    = t1x - t0x;
                float curXfloat = t0x;
                if (x1float < x0float)
                {
                    while (true)
                    {
                        curXfloat += xDelta;

                        for (; x0int > ToInt(curXfloat - 1); x0int--)
                        {
                            del(x0int, y0int);

                            if (x0int == x1int)
                            {
                                if (y0int != y1int)
                                {
                                    del(x1int, y1int);
                                }
                                return;
                            }
                        }

                        x0int  = ToInt(curXfloat);
                        y0int += yDelta;
                        //DrawPoint(curXfloat, y0int, Color.red, "x");
                    }
                }
                else
                {
                    while (true)
                    {
                        curXfloat += xDelta;

                        for (; x0int < ToInt(curXfloat + 1); x0int++)
                        {
                            del(x0int, y0int);

                            if (x0int == x1int)
                            {
                                if (y0int != y1int)
                                {
                                    del(x1int, y1int);
                                }
                                return;
                            }
                        }

                        x0int  = ToInt(curXfloat);
                        y0int += yDelta;
                        //DrawPoint(curXfloat, y0int, Color.red, "x");
                    }
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// DONT FORGET TO SCALE ALL SO ONE PIXEL IS 1f
        /// </summary>
        public static void DrawLine(float x0float, float y0float, float x1float, float y1float, DelegateDDA1 del)
        {
            //Debug.LogFormat("x0: {0}, y0: {1}, x1: {2}, y1: {3}", x0float, y0float, x1float, y1float);

            int x0int = (int)x0float;
            int y0int = (int)y0float;
            int x1int = (int)x1float;
            int y1int = (int)y1float;

            if (x0int == x1int)
            {
                if (y0int == y1int) //inside one pixel
                {
                    del(x0int, y0int);
                    return;
                }
                //along x axis
                if (y0int < y1int)
                {
                    for (int y = y0int; y < y1int + 1; y++)
                    {
                        del(x0int, y);
                    }
                }
                else
                {
                    for (int y = y1int; y < y0int + 1; y++)
                    {
                        del(x0int, y);
                    }
                }
                return;
            }

            if (y0int == y1int)  //along y axis
            {
                if (x0int < x1int)
                {
                    for (int x = x0int; x < x1int + 1; x++)
                    {
                        del(x, y0int);
                    }
                }
                else
                {
                    for (int x = x1int; x < x0int + 1; x++)
                    {
                        del(x, y0int);
                    }
                }
                return;
            }

            //some funky DDA based line rasterization
            if (SomeMath.InRangeInclusive((x1float - x0float) / (y1float - y0float), -1, 1))     //y
            {
                if (y1float < y0float)
                {
                    float temp;
                    temp    = x0float;
                    x0float = x1float;
                    x1float = temp;
                    temp    = y0float;
                    y0float = y1float;
                    y1float = temp;

                    x0int = (int)x0float;
                    y0int = (int)y0float;
                    x1int = (int)x1float;
                    y1int = (int)y1float;
                }
                //Handles.Label(new Vector2(x0float, y0float) * PIXEL_SIZE, "0");
                //Handles.Label(new Vector2(x1float, y1float) * PIXEL_SIZE, "1");

                float lineDirX = x1float - x0float;
                float lineDirY = y1float - y0float;
                float t0y, t1y;

                if (x0float < x1float)
                {
                    t0y = IntersectionY((float)x0int, x0float, y0float, lineDirX, lineDirY);
                    t1y = IntersectionY((float)(x0int + 1), x0float, y0float, lineDirX, lineDirY);
                    //DrawCross(x0int, t0y, Color.red, 10f, "t0");
                    //DrawCross(x0int + 1, t1y, Color.green, 10f, "t1");
                }
                else
                {
                    t0y = IntersectionY((float)(x0int + 1), x0float, y0float, lineDirX, lineDirY);
                    t1y = IntersectionY((float)x0int, x0float, y0float, lineDirX, lineDirY);
                    //DrawCross(x0int + 1, t0y, Color.red, 10f, "t0");
                    //DrawCross(x0int, t1y, Color.green, 10f, "t1");
                }

                int   xDelta    = Math.Sign((int)(x1int - x0int));
                float yDelta    = t1y - t0y;
                float curYfloat = t0y;

                //int curXint = x0int;
                //int curYint = y0int;
                //now its it values
                while (true)
                {
                    curYfloat += yDelta;
                    //int targetYint = (int)curYfloat;
                    //DrawLineDebug(x0int, y0int, x0int, (int)curYfloat, Color.magenta);

                    for (; y0int < (int)curYfloat + 1; y0int++)
                    {
                        del(x0int, y0int);
                        //grid[x0int][y0int] = true;

                        if (y0int == y1int)
                        {
                            del(x1int, y1int);
                            //grid[x1int][y1int] = true;
                            return;
                        }
                    }

                    y0int  = (int)curYfloat;
                    x0int += xDelta;
                    //DrawCross((curXint + 1), targetYfloat, 10f);
                }
            }
            else     //x
            {
                if (x1float < x0float)
                {
                    float temp;
                    temp    = x0float;
                    x0float = x1float;
                    x1float = temp;
                    temp    = y0float;
                    y0float = y1float;
                    y1float = temp;

                    x0int = (int)x0float;
                    y0int = (int)y0float;
                    x1int = (int)x1float;
                    y1int = (int)y1float;
                }
                //Handles.Label(new Vector2(x0float, y0float) * PIXEL_SIZE, "0");
                //Handles.Label(new Vector2(x1float, y1float) * PIXEL_SIZE, "1");

                float lineDirX = x1float - x0float;
                float lineDirY = y1float - y0float;
                float t0x, t1x;

                if (y0float < y1float)
                {
                    t0x = IntersectionX((float)y0int, x0float, y0float, lineDirX, lineDirY);
                    t1x = IntersectionX((float)(y0int + 1), x0float, y0float, lineDirX, lineDirY);
                    //DrawCross(t0x, y0int, Color.red, 10f, "t0");
                    //DrawCross(t1x, y0int + 1, Color.green, 10f, "t1");
                }
                else
                {
                    t1x = IntersectionX((float)y0int, x0float, y0float, lineDirX, lineDirY);
                    t0x = IntersectionX((float)(y0int + 1), x0float, y0float, lineDirX, lineDirY);
                    //DrawCross(t1x, y0int, Color.green, 10f, "t1");
                    //DrawCross(t0x, y0int + 1, Color.red, 10f, "t0");
                }

                int   yDelta    = Math.Sign((int)(y1int - y0int));
                float xDelta    = t1x - t0x;
                float curXfloat = t0x;
                //int curXint = x0int;
                //int curYint = y0int;
                //now its it values

                while (true)
                {
                    curXfloat += xDelta;
                    //int targetXint = (int)curXfloat;
                    //DrawLineDebug(x0int, y0int, (int)curXfloat, y0int, Color.magenta);

                    for (; x0int < (int)curXfloat + 1; x0int++)
                    {
                        del(x0int, y0int);

                        if (x0int == x1int)
                        {
                            del(x1int, y1int);
                            return;
                        }
                    }

                    x0int  = (int)curXfloat;
                    y0int += yDelta;
                    //DrawCross(targetXfloat, (curYint + 1), 10f);
                }
            }
        }