Jump to content
  • Advertisement
Sign in to follow this  
Paragon123

Triangle Templates...

This topic is 790 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, so this feels like a dumb question... but I have been having trouble.

I am creating a tile map generator. The tile maps are rectangular in shape, the first step breaks the map into smaller rectangles (using BSP/Recursive division). The next step creates "Sub tilemaps" for each rectangle generated by the BSP. The Maps are then quilted back together to get the total map. The issue is that I want to be able to apply masks to the sub rectangles to indicate shapes to follow while building... the "Mask" is a 2D boolean array, true means that whatever tile was generated should be placed and false means that it should be skipped.

 

I have currently, two masks working, the Rectangle and Elipse... these were pretty easy because they were symetrical. I am having trouble with Triangles...  

Here is the code for the rectangle

        public static bool[,] Rectangle(int w, int h, int edgeWidth, bool invert)
        {
            bool[,] ret=new bool[w,h];

            for (int i = 0; i < w; i++)
                for (int j = 0; j < h; j++)
                    ret[i, j] = invert ? false : true;


            for(int e=0;e<edgeWidth;e++)
                for(int i=0;i<w;i++)
                {
                    ret[i, 0 + e] = invert ? true : false;
                    ret[i, (h-1) - e] = invert ? true : false;
                }
            for(int e=0;e<edgeWidth;e++)
                for(int j=0;j<h;j++)
                {
                    ret[0 + e, j] = invert ? true : false;
                    ret[(w-1) - e, j] = invert ? true : false;
                }
            return ret;
        }

And the circle

        public static bool[,] Circle(int xRadii,int yRadii, int edgeWidth, bool invert)
        {
            int w = xRadii * 2;
            int h = yRadii * 2;
            bool[,] ret = new bool[w, h];
            for (int i = 0; i < w; i++)
                for (int j = 0; j < h; j++)
                {
                    int xDelta = xRadii - i;
                    int yDelta = yRadii - j;

                    double dEdge = (Math.Pow(xDelta, 2) / Math.Pow(xRadii, 2) + Math.Pow(yDelta, 2) / Math.Pow(yRadii, 2));
                    double dInner = (Math.Pow(xDelta, 2) / Math.Pow(xRadii - edgeWidth, 2) + Math.Pow(yDelta, 2) / Math.Pow(yRadii - edgeWidth, 2));

                    if ( dEdge< 1)
                    {
                        if (dInner < 1)
                            ret[i, j] = invert?false:true;
                        else ret[i, j] = invert?true:false;
                    }
                    else ret[i, j] = false;
                }

            return ret;
        }

Finally the unsuitable triangle algorithm

        public static bool[,] Triangle(int h, int edgeWidth, bool invert)
        {
            int w = 2*h;
            bool[,] ret = new bool[w, h];
            for (int i = 0; i < w; i++)
                for (int j = 0; j < h; j++)
                    ret[i, j] = false;

            for (int j = 0; j < h; j++)
            {
                for (int i = -j; i <= j; i++)
                    if (j-Math.Abs(i) >= edgeWidth && j<h-edgeWidth)
                        ret[h+i, j] = invert ? false :true;
                    else
                        ret[h+i, j] = invert ? true : false;
            }
            return ret;
        }

This algorithm only produces triangles with a flat bottom and a pointy top... I need to be able to create triangle with at least the ability to rotate 0,90,180,270 degress if not 0,45,90,etc.. or better yet, arbitrary rotation... or arbitrary triangles (Different angle sizes)... and I am just stuck. 

 

While we're at it... if anyone has tips for the same sort of algorithm for other interesting shapes I'd love to see those as well... possibly a star or some such. 

 

And Finally... if I were to have a predefined rectangular mask... how would I scale that to an arbitrary rectangle size? I could do it if I only had the vertex and had to scale the cords of the vertex... but not sure how to do it with a set of tiles...

my only thought atm is to divide the final size by the original size (x and y individually) then repeat the tiles as neccessary... i.e

X0X0X

0X0X0 

 

scaled to 8x8 would come out:

xx00xx00xx

xx00xx00xx

xx00xx00xx

xx00xx00xx

00xx00xx00

00xx00xx00

00xx00xx00

00xx00xx00

 

Does this seem correct? I have doubts...

 

 

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!