7    VisuAlgo.net / /polygon Login  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.
Please login if you are a repeated visitor or register for an (optional) free account first.

X Esc

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.

X Esc

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.

X Esc

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.

X Esc

We provide a few example polygons as a starting point.

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

X Esc

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.

X Esc

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.

X Esc

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.

X Esc

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.

X Esc

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

X Esc

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

X Esc

X Esc

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

X Esc

-／+：减缓／增加速度

X Esc

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.

X Esc

Example Polygon

isConvex(P)

inPolygon(pt, P)

cutPolygon（ln，P） Concave

Convex

Mountain

Maze

#### 关于

VisuAlgo在2011年由Steven Halim博士概念化，作为一个工具，帮助他的学生更好地理解数据结构和算法，让他们自己和自己的步伐学习基础。
VisuAlgo包含许多高级算法，这些算法在Steven Halim博士的书（“竞争规划”，与他的兄弟Felix Halim博士合作）和其他书中讨论。今天，一些高级算法的可视化/动画只能在VisuAlgo中找到。

VisuAlgo不是从一开始就设计为在小触摸屏（例如智能手机）上工作良好，因为需要满足许多复杂的算法可视化，需要大量的像素和点击并拖动手势进行交互。一个令人尊敬的用户体验的最低屏幕分辨率为1024x768，并且只有着陆页相对适合移动设备。
VisuAlgo是一个正在进行的项目，更复杂的可视化仍在开发中。

zh, id, kr, vn, th.

#### 团队

Dr Steven Halim, Senior Lecturer, School of Computing (SoC), National University of Singapore (NUS)
Dr Felix Halim, Software Engineer, Google (Mountain View)

Koh Zi Chun, Victor Loh Bo Huai

Phan Thi Quynh Trang, Peter Phandi, Albert Millardo Tjindradinata, Nguyen Hoang Duy

Rose Marie Tan Zhao Yun, Ivan Reinaldo

Jonathan Irvin Gunawan, Nathan Azaria, Ian Leow Tze Wei, Nguyen Viet Dung, Nguyen Khac Tung, Steven Kester Yuwono, Cao Shengze, Mohan Jishnu

Erin Teo Yi Ling, Wang Zi

Truong Ngoc Khanh, John Kevin Tjahjadi, Gabriella Michelle, Muhammad Rais Fathin Mudzakir

List of translators who have contributed ≥100 translations can be found at statistics page.

#### 使用条款

VisuAlgo是地球上的计算机科学社区免费。如果你喜欢VisuAlgo，我们对你的唯一的要求就是通过你知道的方式，比如：Facebook、Twitter、课程网页、博客评论、电子邮件等告诉其他计算机方面的学生/教师：VisuAlgo网站的神奇存在

VisuAlgo不是一个完成的项目。 Steven Halim博士仍在积极改进VisuAlgo。如果您在使用VisuAlgo并在我们的可视化页面/在线测验工具中发现错误，或者如果您想要求添加新功能，请联系Dr Steven Halim博士。他的联系邮箱是他的名字加谷歌邮箱后缀：StevenHalim@gmail.com。