# Polygon and Algorithms on Polygon

## 1. Introduction

A polygon is a plane figure that is bounded by a closed circuit composed of a finite sequence of straight line segments.

This visualization features a few computational geometry algorithms that can be carried out on simple (non-crossing) polygons with 3 or more non-collinear points, such as determining their perimeters and areas, determining concavity or convexity, determining whether a point is inside or outside, and to cut them with a simple line.

## 2. Visualization

Vertices of a polygon can be ordered either in ClockWise (CW) or CounterClockWise (CCW) order. In this visualization, we prefer to use CCW order (although drawing polygon with vertices in CW order is also acceptable). Under the hood, we also set the first vertex = the last vertex to simplify implementation.

Note that we limit the drawn polygon to be a simple polygon, i.e. there is no edge intersection.

The number of vertices/corners of the polygon is stored in variable n. As polygon is a closed circuit, the number of edges/sides of the polygon is also n.

## 3. Available Operations

All available operations are listed in the left hand side menu as usual.

The first two are for giving simple input polygons and the next five are the computational geometry algorithms that you can run on the currently drawn polygon.

### 3-1. Draw Polygon

In this visualization, you can draw any simple polygon (at least 3 points), without any collinear points. The smallest such polygon is a triangle.

The polygon that you draw can be either convex (line connecting any two points inside the polygon will remain inside the polygon) or concave.

If you do not close the loop (draw an edge from last vertex back to vertex 0), we will do that automatically for you.

### 3-2. Example Polygons

We provide a few example polygons as a starting point.

Upon loading this visualization page, we will randomize the chosen example polygon.

### 3-3. Perimeter of a Polygon

The perimeter of a polygon is simply the sum of the lengths (Euclidean distances) of consecutive line segments (polygon edges).

This routine works for both convex and concave polygons and runs in O(n).

Without further ado, let's compute the Perimeter of the currently drawn polygon.

### 3-4. Area of a Polygon

When the vertices of a polygon are given in a circular manner (CW or CCW), we can compute its area using the Shoelace Formula.

This Shoelace Formula returns the area, which is half the cross products of vectors defined by edge endpoints.

This formula is versatile as it works for both convex and concave polygons. It can be computed in O(n).

Without further ado, let's compute the Area of the currently drawn polygon.