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.

**Remarks**: By default, we show e-Lecture Mode for first time (or non logged-in) visitor.

If you are an NUS student and a repeat visitor, please __login__.

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**.

Pro-tip 1: Since you are not __logged-in__, you may be a first time visitor (or not an NUS student) who are not aware of the following keyboard shortcuts to navigate this e-Lecture mode: **[PageDown]**/**[PageUp]** to go to the next/previous slide, respectively, (and if the drop-down box is highlighted, you can also use **[→ or ↓/← or ↑]** to do the same),and **[Esc]** to toggle between this e-Lecture mode and exploration mode.

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.

Pro-tip 2: We designed this visualization and this e-Lecture mode to look good on 1366x768 resolution **or larger** (typical modern laptop resolution in 2021). We recommend using Google Chrome to access VisuAlgo. Go to full screen mode (**F11**) to enjoy this setup. However, you can use zoom-in (**Ctrl +**) or zoom-out (**Ctrl -**) to calibrate this.

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.

Pro-tip 3: Other than using the typical media UI at the bottom of the page, you can also control the animation playback using keyboard shortcuts (in Exploration Mode): **Spacebar** to play/pause/replay the animation, **←**/**→** to step the animation backwards/forwards, respectively, and **-**/**+** to decrease/increase the animation speed, respectively.

We provide a few example polygons as a starting point.

Upon loading this visualization page, we will randomize the chosen example 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

of the currently drawn 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

of the currently drawn polygon.A polygon is called a **Convex** polygon if we draw a line between **any two** different points inside the polygon and the line always remain inside the polygon. Otherwise, the polygon is called **Concave**.

There is a far easier method to check if a given polygon (assume no three collinear points) is convex without using the direct definition above. We can check if **all three** consecutive vertices of the polygon form the same kind of turn (all CCWs or all CWs). This check is clearly O(**n**).

Without further ado, let's check if the currently drawn polygon

.There are a few algorithms for checking if a point (pt1) is inside a polygon or not. We reckon the most robust algorithm is the __Winding Number algorithm__ that computes the sum of angles subtended by each edge/side of the polygon with pt1 as the origin. As there are only **n** such angles, this check also runs in O(**n**).

The input simple polygon can be as complicated as the currently displayed "MAZE" test case. Try

and test cases.In Exploration Mode, you will be asked to provide the tested point (pt1) as additional input of this operation.

We can cut a convex polygon with a straight line defined by two points (pt1, pt2). The result of the cut are two smaller but also convex polygons. This algorithm currently returns the smaller polygon on 'the left side' of the cutting line (pt1, pt2).

Note that although possible, cutting a Concave polygon is more complicated as it may result in more than two (and possibly degenerate) polygons and is thus not supported in this visualization.

Try

to see the default version of this routine and where we swap pt1 and pt2 to get the other side of the cut.In Exploration Mode, you will be asked to provide two points to define the cut line (pt1 and pt2) as additional input of this operation.

This routine also runs in O(**n**).

There is one more computational geometry visualization in VisuAlgo: __Convex Hull__.

You can now use some of these algorithm on polygon routines to solve a few programming exercises: __UVa 11265 - The Sultan's Problem__ and __Kattis - robotprotection__.

You are allowed to use/modify our implementation code for various polygon algorithms:__polygon.cpp____polygon.java____polygon.py____polygon.ml__

You have reached the last slide. Return to 'Exploration Mode' to start exploring!

Note that if you notice any bug in this visualization or if you want to request for a new visualization feature, do not hesitate to drop an email to the project leader: Dr Steven Halim via his email address: stevenhalim at gmail dot com.

__Close__

Draw Polygon

Example Polygon

perimeter(P)

area(P)

isConvex(P)

inPolygon(pt, P)

cutPolygon(ln, P)