# PointInPolygon

vtk-examples/Cxx/Utilities/PointInPolygon

Question

### Code¶

PointInPolygon.cxx

#include <vtkNew.h>
#include <vtkPoints.h>
#include <vtkPolygon.h>

int main(int, char*[])
{
// Create the polygon
vtkNew<vtkPolygon> polygon;
polygon->GetPoints()->InsertNextPoint(0.0, 0.0, 0.0);
polygon->GetPoints()->InsertNextPoint(1.0, 0.0, 0.0);
polygon->GetPoints()->InsertNextPoint(1.0, 1.0, 0.0);
polygon->GetPoints()->InsertNextPoint(0.0, 1.0, 0.0);

double testIn[3] = {0.5, 0.5, 0.0};
double testOut[3] = {2.0, 0.5, 0.0};

double n[3];
polygon->ComputeNormal(
polygon->GetPoints()->GetNumberOfPoints(),
static_cast<double*>(polygon->GetPoints()->GetData()->GetVoidPointer(0)),
n);

double bounds[6];
polygon->GetPoints()->GetBounds(bounds);

std::cout << "testIn in polygon? "
<< polygon->PointInPolygon(
testIn, polygon->GetPoints()->GetNumberOfPoints(),
static_cast<double*>(
polygon->GetPoints()->GetData()->GetVoidPointer(0)),
bounds, n)
<< std::endl;

std::cout << "testOut in polygon? "
<< polygon->PointInPolygon(
testOut, polygon->GetPoints()->GetNumberOfPoints(),
static_cast<double*>(
polygon->GetPoints()->GetData()->GetVoidPointer(0)),
bounds, n)
<< std::endl;

/*
std::cout << "New functions:" << std::endl;

std::cout << "testIn in polygon? " << polygon->PointInPolygon(testIn) <<
std::endl;

std::cout << "testOut in polygon? " << polygon->PointInPolygon(testOut) <<
std::endl;
*/
return EXIT_SUCCESS;
}


### CMakeLists.txt¶

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(PointInPolygon)

find_package(VTK COMPONENTS
CommonCore
CommonDataModel
)

if (NOT VTK_FOUND)
message(FATAL_ERROR "PointInPolygon: Unable to find the VTK build folder.")
endif()

# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
target_link_libraries(PointInPolygon PRIVATE ${VTK_LIBRARIES} ) # vtk_module_autoinit is needed vtk_module_autoinit( TARGETS PointInPolygon MODULES${VTK_LIBRARIES}
)


cd PointInPolygon/build


If VTK is installed:

cmake ..


If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:

cmake -DVTK_DIR:PATH=/home/me/vtk_build ..


Build the project:

make


and run it:

./PointInPolygon


WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.