Figuring Out Clockwise Polygon Fill in a Program

  • Context: Undergrad 
  • Thread starter Thread starter ZippyDee
  • Start date Start date
  • Tags Tags
    Polygon Program
Click For Summary

Discussion Overview

The discussion revolves around determining how to ensure that a polygon is filled in a clockwise manner based on its vertex coordinates. Participants explore mathematical methods and coordinate transformations to achieve this goal, with a focus on programming applications.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Exploratory

Main Points Raised

  • One participant describes the challenge of ensuring that the fill color is consistently on the right side of each line segment when drawing a polygon.
  • Another participant suggests converting the vertex coordinates to polar coordinates and sorting them by angular coordinate, noting that this method is applicable if the polygon is convex.
  • A different approach is proposed involving the calculation of a line integral around the polygon's edge to determine circulation, which could indicate the direction of filling.
  • One participant provides a formula for calculating the signed area of the polygon, indicating that a positive result corresponds to a counter-clockwise order, while a negative result indicates a clockwise order.
  • A later reply introduces the idea of summing angles to determine the orientation, noting that the result could vary based on whether interior or exterior angles are summed, especially in cases where lines cross.

Areas of Agreement / Disagreement

Participants present multiple methods for determining the clockwise filling of the polygon, but there is no consensus on which method is superior or universally applicable. The discussion remains open with various approaches being explored.

Contextual Notes

Some methods proposed depend on specific conditions, such as the convexity of the polygon or the handling of crossing lines, which may affect the applicability of the suggested solutions.

ZippyDee
Messages
11
Reaction score
0
I don't know exactly how to explain this, but I'll try my best:

Let's say I have a set of points (P1, P2, P3...Pn), that are the vertexes of a n-sided polygon. As would be expected, the polygon is drawn simply by connecting the points in order (P1 to P2, P2 to P3, Pn to P1).

This is the hard part to explain...
The lines need to be always drawn "clockwise" rather than "counterclockwise." I'm not sure if that really makes any sense...


I've attached a picture that may help.

Notice that for the top polygon, when going from any point to the next point (for example P1 to P2), the fill color is always on the right hand side of the line between the points (as if you were standing on P1 and facing P2), but for the bottom polygon, the fill is always to the left of the line.


I know all the x/y coordinates of each point, and therefore can find the angle of any line from one point to another.

How do I mathematically figure out if the filled part is consistently on the right hand side of every line.

Thank you so much in advance!
-Zippy Dee
Ted Newman


Also, please note that this is for a program I am making, so positive Y is moving downward rather than upward.
 

Attachments

  • Screen shot 2010-09-25 at 10.37.04 PM.png
    Screen shot 2010-09-25 at 10.37.04 PM.png
    5 KB · Views: 590
Mathematics news on Phys.org
Can you convert them to polar coordinates and sort them according to their angular coordinate?

Find the angle of point 1, and subtract this angle from every point - an effective coordinate transform to put point 1 on the x-axis. This only works if the shape is convex. Otherwise you could go a longer way- create a vector field modelling the free vortex centered in the middle of the polygon, and perform the line integral around the edge of the polygon. If you go one direction you get a positive circulation, and the other direction you get a negative circulation.
 
Calculate

\sum_{i=1}^n x_i y_{i+1} - x_{i+1} y_i

in which (x_i, y_i) are the coordinates of the vertices. This will give you twice the signed area of the polygon, i.e. if the sign is positive, the ordering is counter-clockwise, otherwise it is clockwise.
 
Awesome, thanks! That's exactly what I was looking for!
 
I just realized that I could also sum up the angles and use that result...I would then either get 360 or -360, unless there were some lines in the polygon that crossed. Which number meant what would depend on whether I sum the interior or exterior angles.
 

Similar threads

Replies
1
Views
2K
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 14 ·
Replies
14
Views
4K
Replies
14
Views
11K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K