Tutorial 1d : Polygons |
||
Introduction There's nothing complicated about using Glide to render polygons, but it's a little different from the other drawing functions so we're going to cover it in a separate tutorial. So far, we've seen how to draw three different shapes - dots, lines and triangles. Glide uses a different function for each of those shapes because they always have the same number of vertices - a dot always has one, a line always has two and a triangle always has three. Of course, a non-triangular polygon can have any number of vertices greater than three. For example, a square has four vertices; an octagon has eight. It would be impractical and impossible for Glide to offer a different function for every conceivable polygon.
Note: when you tell Glide to render a polygon, it first converts it to a series of triangles which are then rendered individually. Because of this, Glide can only work with convex polygons - that is, a polygon with all internal angles less than 180 degrees. Using grDrawPolygonVertexList We'll look first at how to draw a polygon using the grDrawPolygonVertexList function, which is the simpler of the two. To use it, we must:
As with all other rendering functions, Glide uses the GrVertex structure to hold information about each vertex of a polygon. In the previous tutorials, we've been creating and working with several individual vertices, such as with the grDrawTriangle function:
Tell Glide how many vertices there are and draw the polygon When we call the grDrawPolygonVertexList function to draw the polygon, we use two parameters. The first parameter tells Glide how many vertices the polygon has and the second is the array of vertices that should be used.
For example, to render a pentagon, the code would be:
Using grDrawPolygon The grDrawPolygon function is a little more complicated, but it is also much more powerful. To use it, one more step is necessary, thus:
We create an array of vertices in the same way as we did for the grDrawPolygonVertexList function, although we don't have to create them in any particular order:
The grDrawPolygonVertexList function will use any number of vertices, which will be specified in an array of indices. For instance, we could define ten vertices, and then give Glide an array of five indices specifying that the 1st, 2nd, 4th, 7th and 9th vertices should be used.
Remembering that our array of vertices is zero-based, the code to do this would be:
When we call the grDrawPolygon function to draw the polygon, we use three parameters. The first parameter tells Glide how many vertices the polygon has, the second is the array of indices and the third is the array of vertices.
For example, to render a pentagon using the 1st, 2nd, 4th, 7th and 9th vertices from an array of ten, the code would be:
The red pentagon and green triangle are drawn using the grDrawPolygon function, with all eight vertices declared in the same array and indexed using two separate arrays. The blue pentagon and magenta triangle are drawn using the grDrawPolygonVertexList function, with both sets of vertices being declared in two separate arrays. Next Tutorial | Main Page This tutorial is ©1998 by Andrew Smith. No part of this tutorial may be reproduced without permission. If you want to reproduce any of this tutorial for non-commercial purposes then I'm not likely to try and stop you, but please ask me first. |