# Polygon

VTKExamples/Cxx/GeometricObjects/Polygon

### Code

Polygon.cxx

    #include <vtkVersion.h>
    #include <vtkSmartPointer.h>
    #include <vtkPolygon.h>
    #include <vtkCellArray.h>
    #include <vtkPolyData.h>
    #include <vtkPolyDataMapper.h>
    #include <vtkActor.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderer.h>
    #include <vtkRenderWindowInteractor.h>

    int main(int, char *[])
    {
      // Setup four points
      vtkSmartPointer<vtkPoints> points =
        vtkSmartPointer<vtkPoints>::New();
      points->InsertNextPoint(0.0, 0.0, 0.0);
      points->InsertNextPoint(1.0, 0.0, 0.0);
      points->InsertNextPoint(1.0, 1.0, 0.0);
      points->InsertNextPoint(0.0, 1.0, 0.0);

      // Create the polygon
      vtkSmartPointer<vtkPolygon> polygon =
        vtkSmartPointer<vtkPolygon>::New();
      polygon->GetPointIds()->SetNumberOfIds(4); //make a quad
      polygon->GetPointIds()->SetId(0, 0);
      polygon->GetPointIds()->SetId(1, 1);
      polygon->GetPointIds()->SetId(2, 2);
      polygon->GetPointIds()->SetId(3, 3);

      // Add the polygon to a list of polygons
      vtkSmartPointer<vtkCellArray> polygons =
        vtkSmartPointer<vtkCellArray>::New();
      polygons->InsertNextCell(polygon);

      // Create a PolyData
      vtkSmartPointer<vtkPolyData> polygonPolyData =
        vtkSmartPointer<vtkPolyData>::New();
      polygonPolyData->SetPoints(points);
      polygonPolyData->SetPolys(polygons);

      // Create a mapper and actor
      vtkSmartPointer<vtkPolyDataMapper> mapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
    #if VTK_MAJOR_VERSION <= 5
      mapper->SetInput(polygonPolyData);
    #else
      mapper->SetInputData(polygonPolyData);
    #endif

      vtkSmartPointer<vtkActor> actor =
        vtkSmartPointer<vtkActor>::New();
      actor->SetMapper(mapper);

      // Visualize
      vtkSmartPointer<vtkRenderer> renderer =
        vtkSmartPointer<vtkRenderer>::New();
      vtkSmartPointer<vtkRenderWindow> renderWindow =
        vtkSmartPointer<vtkRenderWindow>::New();
      renderWindow->AddRenderer(renderer);
      vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
      renderWindowInteractor->SetRenderWindow(renderWindow);

      renderer->AddActor(actor);
      renderer->SetBackground(.5,.3,.31); // Background color salmon

      renderWindow->Render();
      renderWindowInteractor->Start();

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 2.8)

    PROJECT(Polygon)

    find_package(VTK REQUIRED)
    include(${VTK_USE_FILE})

    add_executable(Polygon MACOSX_BUNDLE Polygon.cxx )

    target_link_libraries(Polygon ${VTK_LIBRARIES})

### Download and Build Polygon

Danger

The generation of tar files has not been ported to the new VTKExamples. Some tarballs may be missing or out-of-date.

Click here to download Polygon and its CMakeLists.txt file. Once the tarball Polygon.tar has been downloaded and extracted,

    cd Polygon/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:

    ./Polygon

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