/// <summary> /// 获取到所属于的块; /// </summary> public RectCoord GetChunk(RectCoord coord) { short x = (short)Math.Round(coord.X / (float)width); short y = (short)Math.Round(coord.Y / (float)height); return(new Grids.RectCoord(x, y)); }
RectCoord SouthwestAdge(RectCoord coord) { int x = coord.X - width / 2; int y = coord.Y - height / 2; return(new RectCoord((short)x, (short)y)); }
RectCoord NorthEastAdge(RectCoord coord) { int x = coord.X + width / 2; int y = coord.Y + height / 2; return(new RectCoord((short)x, (short)y)); }
/// <summary> /// 获取到块的中心; /// </summary> public RectCoord GetCenter(RectCoord chunk) { int x = chunk.X * width; int y = chunk.Y * height; return(new RectCoord((short)x, (short)y)); }
/// <summary> /// 获取到矩形像素中心点; /// </summary> public Vector3 GetCenter(RectCoord coord) { float x = coord.X * width; float z = coord.Y * height; return(new Vector3(x, 0, z)); }
/// <summary> /// 获取到矩形的UV坐标; /// </summary> public Vector2 GetUV(Vector3 position, out RectCoord coord) { Vector2 local = GetLocal(position, out coord); Vector2 uv = new Vector2(local.x / width, local.y / height); return(uv); }
/// <summary> /// 获取到这个矩形范围内的所有矩形元素; /// </summary> public IEnumerable <RectCoord> RectRange(Rect rect) { RectCoord southwest = GetCoord(rect.min); RectCoord northeast = GetCoord(rect.max); return(RectCoord.Range(southwest, northeast)); }
/// <summary> /// 获取到UV坐标并且限制在指定的矩形之内; /// </summary> public Vector2 GetUV(RectCoord clamp, Vector3 position) { Vector2 local = GetLocal(clamp, position); Vector2 uv = new Vector2(local.x / width, local.y / height); return(Clamp01(uv)); }
/// <summary> /// 获取到指定矩形的本地坐标; /// </summary> public Vector2 GetLocal(RectCoord clamp, Vector3 position) { Vector3 center = GetCenter(clamp); Vector2 southwestPoint = new Vector2(center.x - widthHalf, center.z - heightHalf); Vector2 local = new Vector2(position.x - southwestPoint.x, position.z - southwestPoint.y); return(local); }
/// <summary> /// 获取到矩形的本地坐标; /// </summary> public Vector2 GetLocal(Vector3 position, out RectCoord coord) { coord = GetCoord(position); Vector3 center = GetCenter(coord); Vector2 southwestPoint = new Vector2(center.x - widthHalf, center.z - heightHalf); Vector2 local = new Vector2(position.x - southwestPoint.x, position.z - southwestPoint.y); return(local); }
/// <summary> /// 获取到块内所有的点; /// </summary> IEnumerable <RectCoord> ChunkRange(RectCoord chunk) { RectCoord center = GetCenter(chunk); RectCoord southwest = SouthwestAdge(center); RectCoord northeast = NorthEastAdge(center); for (var x = southwest.X; x <= northeast.X; x++) { for (var y = southwest.Y; y <= northeast.Y; y++) { yield return(new RectCoord(x, y)); } } }
/// <summary> /// 获取到矩形节点的矩形表示; /// </summary> public Rect GetRect(RectCoord coord) { Vector3 center = GetCenter(coord); return(CenterToRect(center)); }
/// <summary> /// 迭代范围; /// </summary> public IEnumerable <RectCoord> Range() { return(RectCoord.Range(Southwest, Northeast)); }
public RectRange(RectCoord center, int width, int height) : this(center.X, center.Y, width, height) { }