终于获得NewBing的体验资格
萌新的第一问
起初我是这样让Bing给我写了一段代码
优化
然后让他给我做的优化
重新整理问题
最终我重新整理了如下问题
1.帮我写个程序好么?
2.用.NET编写;
实现一个HUM对象,该对象有x,y属性表示其在场景中的位置,有view属性表示其可视范围默认值等于20;
实现一个范围8000*6000的SCENE对象,在场景里随机位置增加10000个HUM,View默认值设为25;
请使用四叉树算法实现管理HUM对象的位置;
用定时器实现每秒随机挑选一个HUM对象,移动HUM对象随机移动1单位;
实现当HUM移动时,输出哪些对象离开/进入了该对象可视范围。
如下是 NewBing 给我的答复,但是在代码发送中多次中断.
我就随口问了句你的答复会有字数限制么?
Bing的回答是:他的回答没有字数限制,但是由于技术原因内容过长时时会被截断,我可以分段发送给你。
于是我说,请把上面的回复分多段发送给我,于是得到如下代码。
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Timers;
namespace QuadTreeExample
{
public static class Program
{
public static void Main(String[] args)
{
new Scene();
Console.ReadLine();
}
}
public class Hum
{
public int X { get; set; }
public int Y { get; set; }
public int View { get; set; } = 20;
public Hum(int x, int y)
{
X = x;
Y = y;
}
}
public class Scene
{
private const int Width = 1000;
private const int Height = 1000;
private const int HumCount = 2000000;
private readonly QuadTree<Hum> _quadTree;
private readonly List<Hum> _hums;
private readonly Random _random;
private readonly Timer _timer;
public Scene()
{
_quadTree = new QuadTree<Hum>(0, 0, Width, Height);
_hums = new List<Hum>();
_random = new Random();
_timer = new Timer(1000);
_timer.Elapsed += MoveRandomHum;
for (int i = 0; i < HumCount; i++)
{
var hum = new Hum(_random.Next(Width), _random.Next(Height)) { View = 25 };
_quadTree.Insert(hum);
_hums.Add(hum);
}
_timer.Start();
}
private void MoveRandomHum(object sender, ElapsedEventArgs e)
{
Stopwatch stopwatch = Stopwatch.StartNew();
var hum = _hums[_random.Next(HumCount)];
var oldX = hum.X;
var oldY = hum.Y;
hum.X += _random.Next(-1, 2);
hum.Y += _random.Next(-1, 2);
if (hum.X < 0) hum.X = 0;
if (hum.Y < 0) hum.Y = 0;
if (hum.X >= Width) hum.X = Width - 1;
if (hum.Y >= Height) hum.Y = Height - 1;
if (oldX != hum.X || oldY != hum.Y)
{
_quadTree.Move(hum, oldX, oldY);
var oldViewRect = new Rect(oldX - hum.View, oldY - hum.View, oldX + hum.View, oldY + hum.View);
var newViewRect = new Rect(hum.X - hum.View, hum.Y - hum.View, hum.X + hum.View, hum.Y + hum.View);
var oldInView = _quadTree.Query(oldViewRect).ToList();
var newInView = _quadTree.Query(newViewRect).ToList();
foreach (var h in oldInView.Except(newInView))
{
Console.WriteLine($"Hum at ({h.X}, {h.Y}) left view of Hum at ({hum.X}, {hum.Y})");
}
foreach (var h in newInView.Except(oldInView))
{
Console.WriteLine($"Hum at ({h.X}, {h.Y}) entered view of Hum at ({hum.X}, {hum.Y})");
}
}
Console.WriteLine($"UseTime:{stopwatch.ElapsedMilliseconds}ms");
}
}
public class QuadTree<T> where T : Hum
{
private readonly int _x;
private readonly int _y;
private readonly int _width;
private readonly int _height;
private readonly List<T> _items;
private QuadTree<T>[] _nodes;
public QuadTree(int x, int y, int width, int height)
{
_x = x;
_y = y;
_width = width;
_height = height;
_items = new List<T>();
}
public void Insert(T item)
{
if (!Contains(item.X, item.Y)) return;
if (_items.Count < 4 || Math.Abs(_width) <= 1 || Math.Abs(_height) <= 1)
{
_items.Add(item);
return;
}
if (_nodes == null) Subdivide();
foreach (var node in _nodes)
{
node.Insert(item);
}
}
public void Move(T item, int oldX, int oldY)
{
if (Contains(oldX, oldY))
{
Remove(item);
Insert(item);
}
}
public void Remove(T item)
{
if (!Contains(item.X, item.Y)) return;
if (_items.Remove(item)) return;
if (_nodes != null)
{
foreach (var node in _nodes)
{
node.Remove(item);
}
}
}
public IEnumerable<T> Query(Rect rect)
{
if (!Intersects(rect)) yield break;
foreach (var item in _items)
{
if (rect.Contains(item.X, item.Y))
{
yield return item;
}
}
if (_nodes != null)
{
foreach (var node in _nodes)
{
foreach (var item in node.Query(rect))
{
yield return item;
}
}
}
}
private void Subdivide()
{
var halfWidth = _width / 2;
var halfHeight = _height / 2;
_nodes = new[]
{
new QuadTree<T>(_x, _y, halfWidth, halfHeight),
new QuadTree<T>(_x + halfWidth, _y, halfWidth, halfHeight),
new QuadTree<T>(_x, _y + halfHeight, halfWidth, halfHeight),
new QuadTree<T>(_x + halfWidth, _y + halfHeight, halfWidth, halfHeight),
};
foreach (var item in _items)
{
foreach (var node in _nodes)
{
node.Insert(item);
}
}
_items.Clear();
}
private bool Contains(int x, int y)
{
return x >= _x && y >= _y && x < _x + _width && y < _y + _height;
}
private bool Intersects(Rect rect)
{
return rect.Left < _x + _width && rect.Right > _x && rect.Top < _y + _height && rect.Bottom > _y;
}
}
public struct Rect
{
public int Left { get; }
public int Top { get; }
public int Right { get; }
public int Bottom { get; }
public Rect(int left, int top, int right, int bottom)
{
Left = left;
Top = top;
Right = right;
Bottom = bottom;
}
public bool Contains(int x, int y)
{
return x >= Left && y >= Top && x < Right && y < Bottom;
}
}
}
咨询GRPC问题
其中我还咨询了 GRPC的相关问题,如下
请求优化
结果得到反向优化,经过我的质疑 NewBing 很爽快的承认了自己错误并跟我道歉。。
最后:微软必硬,2023计算机又一革命