Example #1
0
        public static Coord Extend(this Radius r, Coord center, Coord middle, int radiusLength, bool surpassEdges, int width, int height)
        {
            if (!surpassEdges && (center.X < 0 || center.X >= width || center.Y < 0 || center.Y > height ||
                                  middle.X < 0 || middle.X >= width || middle.Y < 0 || middle.Y > height))
            {
                return(Coord.Get(0, 0));
            }
            if (radiusLength < 1)
            {
                return(center);
            }
            double theta = Math.Atan2(middle.Y - center.Y, middle.X - center.X),
                   cosTheta = Math.Cos(theta), sinTheta = Math.Sin(theta);

            Coord end = Coord.Get(middle.X, middle.Y);

            switch (r)
            {
            case SquidGrid.Radius.Square:
            case SquidGrid.Radius.Cube:
            case SquidGrid.Radius.Diamond:
            case SquidGrid.Radius.Octahedron: {
                int rad2 = 0;
                if (surpassEdges)
                {
                    while (Radius(r, center.X, center.Y, end.X, end.Y) < radiusLength)
                    {
                        rad2++;
                        end = Coord.Get(Convert.ToInt32(cosTheta * rad2) + center.X
                                        , Convert.ToInt32(sinTheta * rad2) + center.Y);
                    }
                }
                else
                {
                    while (Radius(r, center.X, center.Y, end.X, end.Y) < radiusLength)
                    {
                        rad2++;
                        end = Coord.Get(Clamp(Convert.ToInt32(cosTheta * rad2) + center.X, 0, width)
                                        , Clamp(Convert.ToInt32(sinTheta * rad2) + center.Y, 0, height));
                        if (end.X == 0 || end.X == width - 1 || end.Y == 0 || end.Y == height - 1)
                        {
                            return(end);
                        }
                    }
                }

                return(end);
            }

            default: {
                end = Coord.Get(Clamp(Convert.ToInt32(cosTheta * radiusLength) + center.X, 0, width)
                                , Clamp(Convert.ToInt32(sinTheta * radiusLength) + center.Y, 0, height));
                if (!surpassEdges)
                {
                    long edgeLength = 0;
                    //                    if (end.x == 0 || end.x == width - 1 || end.y == 0 || end.y == height - 1)
                    if (end.X < 0)
                    {
                        // wow, we lucked out here. the only situation where cos(angle) is 0 is if the angle aims
                        // straight up or down, and then x cannot be < 0 or >= width.
                        edgeLength = Convert.ToInt32((0 - center.X) / cosTheta);
                        end        = end.ChangeY(Clamp(Convert.ToInt32(sinTheta * edgeLength) + center.Y, 0, height));
                    }
                    else if (end.X >= width)
                    {
                        // wow, we lucked out here. the only situation where cos(angle) is 0 is if the angle aims
                        // straight up or down, and then x cannot be < 0 or >= width.
                        edgeLength = Convert.ToInt32((width - 1 - center.X) / cosTheta);
                        end        = end.ChangeY(Clamp(Convert.ToInt32(sinTheta * edgeLength) + center.Y, 0, height));
                    }

                    if (end.Y < 0)
                    {
                        // wow, we lucked out here. the only situation where sin(angle) is 0 is if the angle aims
                        // straight left or right, and then y cannot be < 0 or >= height.
                        edgeLength = Convert.ToInt32((0 - center.Y) / sinTheta);
                        end        = end.ChangeX(Clamp(Convert.ToInt32(cosTheta * edgeLength) + center.X, 0, width));
                    }
                    else if (end.Y >= height)
                    {
                        // wow, we lucked out here. the only situation where sin(angle) is 0 is if the angle aims
                        // straight left or right, and then y cannot be < 0 or >= height.
                        edgeLength = Convert.ToInt32((height - 1 - center.Y) / sinTheta);
                        end        = end.ChangeX(Clamp(Convert.ToInt32(cosTheta * edgeLength) + center.X, 0, width));
                    }
                }
                return(end);
            }
            }
        }