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.
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: Since you are not logged-in, you may be a first time visitor who are not aware of the following keyboard shortcuts to navigate this e-Lecture mode: [PageDown] to advance to the next slide, [PageUp] to go back to the previous slide, [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.
Another pro-tip: We designed this visualization and this e-Lecture mode to look good on 1366x768 resolution or larger (typical modern laptop resolution in 2017). 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.
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 Perimeter 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 Area 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 IsConvex.
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 InsidePolygon and OutsidePolygon 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 Left Side to see the default version of this routine and Right Side 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 are allowed to use/modify our implementation code that can be downloaded from the companion Competitive Programming book website (ch7_04_polygon.cpp/java inside ch7.zip).
e-Lecture: The content of this slide is hidden and only available for legitimate CS lecturer worldwide. Drop an email to visualgo.info at gmail dot com if you want to activate this CS lecturer-only feature and you are really a CS lecturer (show your University staff profile).
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.