Makman2 / CE3D

A terminal 3D engine
GNU General Public License v3.0
4 stars 0 forks source link

Introduce Linear function handling object #162

Closed Makman2 closed 9 years ago

Makman2 commented 9 years ago

Simplifies tasks with linear functions, especially when creating a function from two given points. In fact it just capsules a linear function with some nice methods. For example get the function value, get inverse function value or control function parameters (gradient and y-axis-intersection).

The prototype looks like this:

Header (Linear.h):

namespace Functions
{

    class Linear
    {
    public:
        // Initializes with the zero function.
        Linear();
        Linear(float gradient, float yaxis);
        Linear(Point const& p1, Point const& p2);
        virtual ~Linear();

        float GetGradient() const;
        void SetGradient(float value);
        float GetYAxis() const;
        void SetYAxis(float value);

        // Returns the value of the linear function f(x).
        float FX(float x) const;
        // Returns the inverse value of the linear function f^-1(y).
        float FY(float y) const;

        float operator ()(float x) const;

    private:
        float m_gradient;
        float m_yaxis;
    };

}

and code (Linear.cpp):

namespace Functions
{

    Linear::Linear()
        : Linear(0, 0)
    {}

    Linear::Linear(float gradient, float yaxis)
        : m_gradient(gradient)
        , m_yaxis(yaxis)
    {}

    Linear::Linear(Point const& p1, Point const& p2)
        : m_gradient(static_cast<float>(p1.Y - p2.Y) / static_cast<float>(p1.X - p2.X))
        , m_yaxis(p1.Y - m_gradient * p1.X)
    {}

    Linear::~Linear()
    {}

    float Linear::GetGradient() const
    {
        return m_gradient;
    }

    void Linear::SetGradient(float value)
    {
        m_gradient = value;
    }

    float Linear::GetYAxis() const
    {
        return m_yaxis;
    }

    void Linear::SetYAxis(float value)
    {
        m_yaxis = value;
    }

    float Linear::FX(float x) const
    {
        return m_gradient * x + m_yaxis;
    }

    float Linear::FY(float y) const
    {
        return (y - m_yaxis) / m_gradient;
    }

    float Linear::operator ()(float x) const
    {
        return FX(x);
    }

}

Will be implemented inside the DrawLine() and DrawTriangle() functions for render.

sils commented 9 years ago

looks cool

Makman2 commented 9 years ago

Where do you think I should place the code files? Shall I make a new folder like 'math' or put it in 'util'?

sils commented 9 years ago

a math folder makes sense IMO since we'll need that anyway one time

sils commented 9 years ago

merged