CS61B选做 lab5 六边形

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)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值