Commit 32549a15 authored by Libor's avatar Libor
Browse files

Added math implementation for 2D circles

parent 2d742bfd
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -4,6 +4,6 @@ echo "Bitmasks seem to be alright at the moment"
echo
echo
echo "Currently most important is to implement math funcs,"
echo "as the origin ones are exponentially complex in 2D"
echo "as the original ones are exponentially complex, in 2D"
echo "its complexity is at most 2^2, in 3D its at most 2^3 (!)"
echo "which lightweigth GPU threads won't handle."
+28 −0
Original line number Diff line number Diff line
#ifndef _TNLCIRCLE2D_H_INCLUDED_
#define _TNLCIRCLE2D_H_INCLUDED_

class tnlCircle2D
{
public:
    tnlCircle2D( unsigned x,
                 unsigned y,
                 unsigned r );

    bool isIntercept( unsigned x1,
                      unsigned x2,
                      unsigned y1,
                      unsigned y2 );

    bool isInInterval( unsigned x1,
                       unsigned x2,
                       unsigned x );

    ~tnlCircle2D()

private:
    unsigned x;
    unsigned y;
    unsigned r;
};

#endif // _TNLCIRCLE2D_H_INCLUDED_
+55 −0
Original line number Diff line number Diff line
#ifndef _TNLCIRCLE2D_IMPL_H_INCLUDED_
#define _TNLCIRCLE2D_IMPL_H_INCLUDED_

#include <cmath>
#include "tnlCircle2D.h"

tnlCircle2D::tnlCircle2D( unsigned x,
                          unsigned y,
                          unsigned r )
{
    this->x = x;
    this->y = y;
    this->r = r;
}

tnlCircle2D::isIntercept( unsigned x1,
                          unsigned x2,
                          unsigned y1,
                          unsigned y2 )
{
    unsigned R = this->r * this->r;
    unsigned X = this->x * this->x;
    unsigned Y = this->y * this->y;

    unsigned aux = x1 - this->x;
    if( R - aux * aux > 0 &&
        this->isInInterval( y1, y2, sqrt( R - aux * aux ) + this->y ) )
        return true;
    
    aux = x2 - this->x;
    if( R - aux * aux > 0 &&
        this->isInInterval( y1, y2, sqrt( R - aux * aux ) + this->y ) )
        return true;

    aux = y1 - this->y;
    if( R - aux * aux > 0 &&
        this->isInInterval( x1, x2, sqrt( R - aux * aux ) + this->x ) )
        return true;

    aux = y2 - this->y;
    if( R - aux * aux > 0 &&
        this->isInInterval( x1, x2, sqrt( R - aux * aux ) + this->x ) )
        return true;

    return false;
}

tnlCircle2D::isInInterval( unsigned x1,
                           unsigned x2,
                           unsigned x )
{
    return ( ( x1 < x ) and ( x < x2 ) );
}

#endif // _TNLCIRCLE2D_IMPL_H_INCLUDED_