CS61B选做 lab5 六边形
有一说一,我本来都想放弃这个了,但是教案上面强调分层思想的理解和学习,所以硬着头皮画完了
思路:
1. 单个六边形
2. 添加单个左侧六边形
3. 添加多个左侧六边形
4. 添加单个右侧六边形
5. 添加多个右侧六边形
6. 添加单个上侧六边形
7. 添加多个上侧六边形
8. 添加单个下侧六边形
9. 添加多个右侧六边形
10. 最后按照要求的Tesselation of Hexagons的模式进行绘制
在main函数中,只调用最高层级的Tesselation六边形绘制函数
代码:我在github上面翻了好久 没有找到啥lab5的资料,基本都是跳过去了 所以只能抱着又菜又爱玩的心态自己写了,代码写的很冗长,希望大家可以批评指正:
唯一的参考就是老师教案中给了绘制单个六边形的一些思路,很有启发性(先确认每行Tile的个数,再进一步确认每行相对于第0行的起始位置偏差,最后逐行添加完成单个六边形的绘制 :)
package byog.lab5;
import edu.princeton.cs.algs4.StdRandom;
import javafx.geometry.Pos;
import org.junit.Test;
import static org.junit.Assert.*;
import byog.TileEngine.TERenderer;
import byog.TileEngine.TETile;
import byog.TileEngine.Tileset;
import javax.swing.text.Position;
import java.awt.*;
import java.util.Random;
/**
* Draws a world consisting of hexagonal regions.
*/
public class HexWorld {
static final int WIDTH = 80;
static final int HEIGHT = 50;
public static final long SEED = 2873123;
public static Random RANDOM = new Random(SEED);
public static class Position {
public int x;
public int y;
public Position (int x_pos, int y_pos) {
x = x_pos;
y = y_pos;
}
}
/** return the total elements in "r" row
* @param s The size of the hex.
* @param r The row number where i = 0 is the row at the bottom
* @return
*/
public static int hexRowWidth(int s, int r) {
int RowExact = r;
if(r >= s){
//When row bigger than size, we need do some calculate, otherwise, we need use s + (RowExact * 2)
RowExact = (s * 2) - 1 - RowExact;
}
return (s + (RowExact * 2));
}
/** return the offset x position in "r" row
* @param s The size of the hex.
* @param r The row number where i = 0 is the row at the bottom
* ..
* ....
* .... <-- r = 1, the x offset position is -1
* ..
* @return
*/
public static int hexRowOffset (int s, int r) {
int RowExact = r;
if(r >= s)