示例#1
0
        private void ClosestInPerimeter(ref float minDistSq, ref Pos closestPos, Coord center, int perimSize, float x, float z, float minDist, float maxDist)
        ///finds the closest in a rectangular (square) perimeter. Just a helper for Closest. PerDist is the perimeter size (distance from center).
        {
            if (perimSize == 0)             //in current cell
            {
                Cell curCell = cells[center];
                for (int i = 0; i < curCell.count; i++)
                {
                    float curDistSq = (curCell.poses[i].x - x) * (curCell.poses[i].x - x) + (curCell.poses[i].z - z) * (curCell.poses[i].z - z);
                    if (curDistSq < minDistSq && curDistSq >= minDist * minDist)
                    {
                        minDistSq = curDistSq; closestPos = curCell.poses[i];
                    }
                }
            }

            else             //in perimeter
            {
                for (int s = 0; s < perimSize; s++)
                {
                    foreach (Coord c in center.DistanceStep(s, perimSize))
                    {
                        //checking cell in range
                        if (!(c.x >= cells.rect.offset.x && c.x < cells.rect.offset.x + cells.rect.size.x &&
                              c.z >= cells.rect.offset.z && c.z < cells.rect.offset.z + cells.rect.size.z))
                        {
                            continue;
                        }

                        Cell curCell = cells[c];
                        for (int i = 0; i < curCell.count; i++)
                        {
                            float curDistSq = (curCell.poses[i].x - x) * (curCell.poses[i].x - x) + (curCell.poses[i].z - z) * (curCell.poses[i].z - z);
                            if (curDistSq < minDistSq && curDistSq >= minDist * minDist)
                            {
                                minDistSq = curDistSq; closestPos = curCell.poses[i];
                            }
                        }
                    }
                }
            }
        }
示例#2
0
        public void DrawGizmo()
        {
            Coord center = new Coord(3, 3);

            for (int dist = 0; dist < 5; dist++)
            {
                Gizmos.color = new Color(1f * dist / 5, 1 - 1f * dist / 5, 0, 1);
                for (int i = 0; i < dist; i++)
                {
                    foreach (Coord c in center.DistanceStep(i, dist))
                    {
                        if (cells.rect.CheckInRange(c))
                        {
                            cells[c].rect.DrawGizmo();
                        }
                    }
                }
            }


            foreach (Coord c in center.DistanceArea(cells.rect))
            {
                //	Gizmos.color = new Color(1f*counter/cells.count, 1-1f*counter/cells.count, 0, 1);
                //	cells[c].rect.DrawGizmo();
                //	counter++;
            }

            for (int x = 0; x < resolution; x++)
            {
                for (int z = 0; z < resolution; z++)
                {
                    //Gizmos.color = new Color(1f*c/cells.Length, 1-1f*c/cells.Length, 0, 1);
                    //cells[x,z].rect.DrawGizmo();
                }
            }
        }