user1968818
user1968818

Reputation: 67

Do order of coordinates affect the drawing of polygon in Java

I am asking if there is any particular order that i need to input my set of coordinates for the drawing of polygon using Graphics2D class in Java.

For example, do values of my coordinates(X,Y) need to be arranged in descending/ascending order for both X and Y arrays of coordinates?

Or another example is if i wish to draw a polygon and i have 4 sets of points, topleft, topright, bottomright and bottomleft and i simply input them in this order to drawPolygon method in Java to get a drawn polygon with corners corresponding to all this 4 points.

Or i can just arrange my coordinates in any random order?

Thanks in advance.

Upvotes: 3

Views: 3707

Answers (2)

Angus Johnson
Angus Johnson

Reputation: 4643

To understand polygon filling in general you have to understand edge direction, winding order and the selected polygon fill rule.

Edge direction is determined by the order that vertices have been declared. For example ...

Polygon poly= new Polygon();
poly.addPoint(10, 10);
poly.addPoint(100, 10);
poly.addPoint(100, 100);
poly.addPoint(10, 100);

Polygons are drawn by joining adjacent vertices (from an ordered list of vertices) to form edges. The last vertex in the list is also joining the first (as if the list were circular). The first edge in the polygon above is constructed from the first two vertices - Point(10,10) and Point(100,10).

Whenever polygons self-intersect or overlap, to understand how the polygons will be drawn, you need a knowledge of both winding order and the applied polygon filling rule. When polygons overlap, polygon sub-regions are created - discrete regions that are enclosed by edges. The winding order of these sub-regions and the applied polygon filling rule determine whether these sub-regions are filled or not.


(source: angusj.com)

The winding number for any given polygon sub-region can be derived by:

  • set the winding count to zero
  • from a point (P1) that's within a given sub-region, draw an imaginary horizontal line to another point that's outside the polygon or polygons (P2)
  • while traversing this line from P1 to P2, for each polygon edge that crosses this imaginary line - if it's heading up then increment the winding count, otherwise decrement the winding count.

According to the Java Graphics2D documentation, fillPolygon only uses the even-odd fill rule where only odd numbered sub-regions are filled.


(source: angusj.com)

Upvotes: 3

HugoRune
HugoRune

Reputation: 13799

The polygon is drawn from each point to the following point.

So the two points of an edge have to be neighbors in the list of points you submit to DrawPolygon.

If you want to draw a polygon between the points A, B, C and D, you will need to submit these points in the order

  • A,B,C,D or
  • D,A,B,C or
  • C,D,A,B or
  • B,C,D,A or
  • D,C,B,A or
  • A,D,C,B or
  • B,A,D,C or
  • C,B,A,D or

All other combinations of A,B,C and D will produce a polygon with the same points but different edges

This is the polygon you get if you use one of the above orders

A------B
|      |
|      |
|      |
D------C

This is the polygon you get if you use for example A,B,D,C

A------B
 \    /
  \  /
   *
  / \
 /   \
D-----C

Upvotes: 1

Related Questions