Reputation: 1835
I'm making a mfc application to make a somewhat drawing mechanism. Using a polyline the user can draw figures, and on pressing the enter key the current point of the line is joined to the starting point[to form a closed polygon]. I think you get the idea. Now, I'm using an STL array to store each vertex of the polygon- in simple words, every point at which the left-mouse button is clicked while drawing the figure is stored in the array.
std::array<CPoint,11> v; //vertices
I'm using the following mechanism to output the elements of this array, i.e the points:
for(int j=0 ; j<v.size() ; j++ )
{
s.Format(L"%d %d\n",v[j].x, v[j].y);
aDC.TextOutW(x+=20,y+=20,s ); //each time print the coordinates
s=" "; //at a different location
}
During execution, when the user draws the figure by clicking points around the screen, those points are stored in the array. The array's declared with 12 elements, but rarely does a shape have 12 vertices. The rest of the element(the empty ones) remain (0,0)- and yet these are outputted in the loop. So what I get printed is 3-4 coordinates and a lots of (0,0)s. Is there a way to print only those elements in which the vertices are stored(I hope you get what I mean). Something like vertices[n]=/*some character that signifies the last element*/
. My question is, what will this character be? like a '\0' in a string.
Upvotes: 1
Views: 117
Reputation:
Well, if you intend to use the (0; 0)
point as invalid, you can just check a point for being at (0; 0)
:
if (vertices[i].x != 0 && vertices[i].y != 0) {
// valid
}
Upvotes: 0
Reputation: 726479
It does not look like there is a good candidate for an "end marker" in a CPoint
structure: every pair of {x,y}
represents a legal point, at least theoretically.
If you insist on using a fixed array (presumably, for performance reasons), you can also store the number of polygon vertices in a separate variable (continuing your string analogy, that would be a "Pascal string" of points, rather than a "C string" of points).
If using an array is not essential, you may want to switch to a std::vector<CPoint>
, a container that is better suited for representing structures of variable size, such as polygons.
Finally, you could designate one point as illegal (say, at {std::numeric_limits<long>::max(), std::numeric_limits<long>::max()}
), create an instance of that point statically, and use it as an end-of-sequence marker. In this case, consider expanding your array by one, and use the end marker as a sentinel.
Upvotes: 2