Arranging triangles in grid order
My program generated triangles automatically, however the arrangement of the triangles are random on JPanel. How can I plot the t开发者_如何学Goriangle in a grid order? Thanks.
EDITED:
for(int i = 0; i < 10; i++)
{
xCoord[0] = generator.nextInt(MAX_WIDTH);
yCoord[0] = generator.nextInt(MAX_HEIGHT);
xCoord[1] = (int) (xCoord[0] - xWidth);
xCoord[2] = (int) (xCoord[1] + (xWidth/2));
yCoord[1] = yCoord[0];
yCoord[2] = (int) (yCoord[1] - yHeight);
triangles.add( new Polygon(xCoord,yCoord, 3));
}
EDITED: OUTPUT DESIRED
How can I make the program generate many pattern but it MUST be in symmetrical form? e.g. left and right is symmetrical. I've tried to make Loop but so far it only generate 1 pattern. Help please :-(
*** *** OR ** ** OR *** *** etc (as long as it is symmetrical)
*** *** ** ** * *
* *
Take a look at my solution to your previous question.
EDIT:
Modifying the example in the link above, you can replace the randomized Polygon points with some that are more fixed. In the paintComponent
method, you can replace:
for (int j = 0; j < 3; j++) {
xCoord[j] = generator.nextInt(maxCellWidth)
+ (maxCellWidth * xMultiple);
yCoord[j] = generator.nextInt(maxCellHeight)
+ (maxCellHeight * yMultiple);
}
with this:
xCoord[0] = (maxCellWidth/2) + (maxCellWidth * xMultiple);
// use the following if you need the points to be fixed as well.
//xCoord[0] = generator.nextInt(maxCellWidth) + (maxCellWidth * xMultiple);
yCoord[0] = 5 + (maxCellHeight * yMultiple);
xCoord[1] = 5 + (maxCellWidth * xMultiple);
yCoord[1] = maxCellHeight - 5 + (maxCellHeight * yMultiple);
xCoord[2] = maxCellWidth - 5 + (maxCellWidth * xMultiple);
yCoord[2] = maxCellHeight - 5 + (maxCellHeight * yMultiple);
For fun, uncommenting the second setting of xCoord[0] will make the top point in a random spot along to x-axis, but the bottom two points of the triangles will be fixed and have a constant distance between them.
alt text http://img127.imageshack.us/img127/5809/picture5v.png
If your triangles are not all the same size, shape and orientation (or only approximately), then you could place them such that their circumcentre points are on a precise grid, giving you an approximation of being "equally" spaced. You can try other points, such as the centre of the nine-point circle to see if you can find a visually more pleasing arrangement.
See the [Wikipedia article on triangles][1] for more information about these points.
[1]: http://en.wikipedia.org/wiki/Triangle"Wikipedia article on triangles"
精彩评论