Click here to Skip to main content
15,897,371 members
Articles / Programming Languages / C#

Triangulation of Arbitrary Polygons

Rate me:
Please Sign up or sign in to vote.
4.54/5 (7 votes)
22 Nov 2009CPOL4 min read 58.1K   5K   31  
This article describes a way of triangulating polygons that is intuitively easy to understand and explain diagrammatically
#if (XNA)
using Microsoft.Xna.Framework;
#endif

namespace FarseerGames.FarseerPhysics.Mathematics
{
    /// <summary>
    /// Encapsulates the logic to do bilinear interpolation
    /// .V1    .V4
    /// .V2    .V3
    /// </summary>
    public class CircularInterpolator
    {
        private const float _twoOverPi = 1f/MathHelper.PiOver2;
        private float _circleValue1;
        private float _circleValue2;
        private float _maxValue = float.MaxValue;
        private float _minValue = float.MinValue;
        private float _value1;
        private float _value2;
        private float _value3;
        private float _value4;

        public CircularInterpolator(float value1, float value2, float value3, float value4, float minValue,
                                    float maxValue)
        {
            _value1 = value1;
            _value2 = value2;
            _value3 = value3;
            _value4 = value4;
            _minValue = minValue;
            _maxValue = maxValue;
        }

        public float GetValue(Vector2 position)
        {
            return GetValue(position.X, position.Y);
        }

        public float GetValue(float x, float y)
        {
            float theta;
            float lerpValue;
            float value = 0;

            float d = MathHelper.Distance(x, y);

            //quadrant 1
            if (x > 0 && y > 0)
            {
                theta = Calculator.ATan2(y, x);
                lerpValue = theta*_twoOverPi;
                _circleValue1 = MathHelper.Lerp(_value1, _value2, lerpValue);
                _circleValue2 = MathHelper.Lerp(_value3, _value4, lerpValue);
                value = MathHelper.Lerp(_circleValue1, _circleValue2, (1 - d)*.5f);
            }
            if (x < 0 && y > 0)
            {
                theta = Calculator.ATan2(y, x);
                lerpValue = (theta - MathHelper.PiOver2)*_twoOverPi;
                _circleValue1 = MathHelper.Lerp(_value2, _value3, lerpValue);
                _circleValue2 = MathHelper.Lerp(_value4, _value1, lerpValue);
                value = MathHelper.Lerp(_circleValue1, _circleValue2, (1 - d)*.5f);
            }
            if (x < 0 && y < 0)
            {
                theta = Calculator.ATan2(y, x) + MathHelper.TwoPi;
                lerpValue = (theta - MathHelper.Pi)*_twoOverPi;
                _circleValue1 = MathHelper.Lerp(_value3, _value4, lerpValue);
                _circleValue2 = MathHelper.Lerp(_value1, _value2, lerpValue);
                value = MathHelper.Lerp(_circleValue1, _circleValue2, (1 - d)*.5f);
            }
            if (x > 0 && y < 0)
            {
                theta = Calculator.ATan2(y, x) + MathHelper.TwoPi;
                lerpValue = (theta - 3*MathHelper.PiOver2)*_twoOverPi;
                _circleValue1 = MathHelper.Lerp(_value4, _value1, lerpValue);
                _circleValue2 = MathHelper.Lerp(_value2, _value3, lerpValue);
                value = MathHelper.Lerp(_circleValue1, _circleValue2, (1 - d)*.5f);
            }
            if (x == 0 && y > 0)
            {
                value = MathHelper.Lerp(_value2, _value4, (1 - y)/2f);
            }
            if (x == 0 && y < 0)
            {
                value = MathHelper.Lerp(_value4, _value2, (1 + y)/2f); //1--y
            }
            if (x > 0 && y == 0)
            {
                value = MathHelper.Lerp(_value1, _value3, (1 - x)/2f);
            }
            if (x < 0 && y == 0)
            {
                value = MathHelper.Lerp(_value3, _value1, (1 + x)/2f); //1--y
            }
            value = MathHelper.Clamp(value, _minValue, _maxValue);
            return value;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Singapore Singapore
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions