Commit 8353c418 authored by Libor's avatar Libor
Browse files

Improved circle unittests. Fixed circles.

parent 2a70170e
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -11,7 +11,8 @@ public:
    bool isIntercept( float x1,
                      float x2,
                      float y1,
                      float y2 );
                      float y2,
                      bool verbose = false );

    bool isInInterval( float x1,
                       float x2,
+42 −5
Original line number Diff line number Diff line
#ifndef _TNLCIRCLE2D_IMPL_H_INCLUDED_
#define _TNLCIRCLE2D_IMPL_H_INCLUDED_

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

@@ -16,31 +17,67 @@ tnlCircle2D::tnlCircle2D( unsigned a,
bool tnlCircle2D::isIntercept( float x1,
                               float x2,
                               float y1,
                               float y2 )
                               float y2,
                               bool verbose )
{
    if( this->isInInterval( x1, x2, this->a - this->r ) &&
        this->isInInterval( x1, x2, this->a + this->r ) &&
        this->isInInterval( y1, y2, this->b - this->r ) &&
        this->isInInterval( y1, y2, this->b + this->r ) )
    {
        if( verbose )
            std::cout << "Circle is inside area." << std::endl;
        return true;
    }
    else if( verbose )
        std::cout << "Circle is not inside area." << std::endl;

    float R = this->r * this->r;
    float A = this->a * this->a;
    float B = this->b * this->b;

    float aux = x1 - this->a;
    if( R - aux * aux >= 0 &&
        this->isInInterval( y1, y2, sqrt( R - aux * aux ) + this->b ) )
        ( this->isInInterval( y1, y2, sqrt( R - aux * aux ) + this->b ) ||
        this->isInInterval( y1, y2, -sqrt( R - aux * aux ) + this->b ) ) )
    {
        if( verbose )
            std::cout << "Circle intercepts left boundry of area." << std::endl;
        return true;
    }
    
    aux = x2 - this->a;
    if( R - aux * aux >= 0 &&
        this->isInInterval( y1, y2, sqrt( R - aux * aux ) + this->b ) )
        ( this->isInInterval( y1, y2, sqrt( R - aux * aux ) + this->b ) ||
        this->isInInterval( y1, y2, -sqrt( R - aux * aux ) + this->b ) ) )
    {
        if( verbose )
            std::cout << "Circle intercepts right boundry of area." << std::endl;
        return true;
    }

    aux = y1 - this->b;
    if( R - aux * aux >= 0 &&
        this->isInInterval( x1, x2, sqrt( R - aux * aux ) + this->a ) )
        ( this->isInInterval( x1, x2, sqrt( R - aux * aux ) + this->a ) ||
        this->isInInterval( x1, x2, -sqrt( R - aux * aux ) + this->a ) ) )
    {
        if( verbose )
            std::cout << "Circle intercepts bottom boundry of area." << std::endl;
        return true;
    }

    aux = y2 - this->b;
    if( R - aux * aux >= 0 &&
        this->isInInterval( x1, x2, sqrt( R - aux * aux ) + this->a ) )
        ( this->isInInterval( x1, x2, sqrt( R - aux * aux ) + this->a ) ||
        this->isInInterval( x1, x2, sqrt( R - aux * aux ) + this->a ) ) )
    {
        if( verbose )
            std::cout << "Circle intercepts top boundry of area." << std::endl;
        return true;
    }

    if( verbose )
        std::cout << "Circle does not intercept area." << std::endl;

    return false;
}
+18 −3
Original line number Diff line number Diff line
@@ -5,8 +5,23 @@ using namespace std;

int main()
{   // dost spatnej unittest -- vylepsit
    tnlCircle2D* circle = new tnlCircle2D( 5, 5, 5 );
    cout << "circle->isIntercept(0, 5, 0, 5) == " << 
            circle->isIntercept( 0, 5, 0, 5 ) << endl;
    tnlCircle2D* circle = new tnlCircle2D( 5, 5, 4 );
    cout << "Testing whole circle inside area: ";
    if( circle->isIntercept( 0, 10, 0, 10, true ) )
        cout << "Ok" << endl;
    else
        cout << "Test failed." << endl;

    cout << "Testing whole area inside circle: ";
    if( !circle->isIntercept( 4, 6, 4, 6, true ) )
        cout << "Ok" << endl;
    else
        cout << "Test failed." << endl;

    cout << "Testing left boundry intercept: ";
    if( circle->isIntercept( 3, 7, 0, 2, true ) )
        cout << "Ok" << endl;
    else
        cout << "Test failed." << endl;
    return 0;
}