Point inside a polygon

1

I need to create a function that checks whether a point is inside a polygon. For that, I researched the internet some solutions that could help me. I found one that seems to be of great help, but in one of the functions needed to run the main function, there was an error. The function is as follows:

// A função checa se o ponto Q está no segmento PR.
bool NoSegmento(Ponto P, Ponto Q, Ponto R)
{
    if (Q.x <= max(P.x, R.x) && Q.x >= min(P.x, R.x) && Q.y <= max(P.y, R.y) 
&& Q.y >= min(P.y, R.y))
        return true;
    return false;
}

However, when compiling, I get some warnings and errors, for example:

implicit declaration of function 'max' [-Wimplicit-function-declaration]|
implicit declaration of function 'min' [-Wimplicit-function-declaration]|
undefined reference to 'max'|
undefined reference to 'min'|

I would like to know if the MAX and MIN functions are functions of some library and what is the purpose of sweating the bool type for the function.

    
asked by anonymous 27.06.2017 / 17:28

2 answers

2

This function checks whether a Ponto is contained within the area occupied by a Rectangle. The points P and R are the coordinates of two opposite vertices of this Rectangle:

Assuming that Ponto looks something like this:

typedef struct _Ponto
{
    int x;
    int y;
} Ponto;

To compile your code in C++ :

#include <algorithm>

bool NoRetangulo( Ponto P, Ponto Q, Ponto R )
{
    return( (Q.x <= std::max(P.x, R.x)) && (Q.x >= std::min(P.x, R.x)) &&
            (Q.y <= std::max(P.y, R.y)) && (Q.y >= std::min(P.y, R.y)) );
}

To compile your code in C :

#define max(a,b)  ( (a > b) ? a : b )
#define min(a,b)  ( (a < b) ? a : b )

int NoRetangulo( Ponto P, Ponto Q, Ponto R )
{
    return( (Q.x <= max(P.x, R.x)) && (Q.x >= min(P.x, R.x)) &&
            (Q.y <= max(P.y, R.y)) && (Q.y >= min(P.y, R.y)) );
}

Improving the idea (in C++ ):

#include <algorithm>


typedef struct Retangulo_
{
    Ponto a;
    Ponto b;
} Retangulo;


int NoRetangulo( Retangulo R, Ponto X )
{
    return( (X.x <= std::max(R.a.x, R.b.x)) && (X.x >= std::min(R.a.x, R.b.x)) &&
            (X.y <= std::max(R.a.y, R.b.y)) && (X.y >= std::min(R.a.y, R.b.y)) );
}
    
27.06.2017 / 20:11
0

The min and max functions are from the C ++ algorithm library see the reference in the documentation

Add in your code:

#import <algorithm>

and modify its function to use the namespace std:

std::min() e std::max()
    
27.06.2017 / 18:24