Reputation: 67
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
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:
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
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
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