ICollection、自訂枚舉器,提供 Add/RemoveAt/Clear/Offset、Bounds(外切矩形)。Lines: PointArrayList[]),提供 Bounds、Draw、CreateBitmap。IEnumerable(唯一方法 GetEnumerator())。GetEnumerator() 回傳實作 IEnumerator 的物件(MoveNext、Current、Reset)。IEnumerable,一個實作 IEnumerator,內部邏輯完全自訂。ICollection(繼承 IEnumerable)→ 自動支援 foreach。MyPointEnumerator : IEnumerator 封裝陣列索引遍歷邏輯。Bounds 屬性快速取得所有點的外切矩形(用於剪切優化與命中測試)。[XmlArrayItem(typeof(PointArrayList), ElementName = "Line")]
public ArrayList Lines { get; } // XML 序列化時元素名為
XmlArrayItem 屬性指定集合元素型別與 XML 元素名稱。Bounds:聚合所有 PointArrayList.Bounds 取聯集。public void Draw(Graphics g, Rectangle clipRect) {
using (Pen pen = new Pen(Color, LineWidth)) {
foreach (PointArrayList line in Lines)
if (clipRect.IntersectsWith(line.Bounds))
g.DrawLines(pen, line.ToArray());
}
}
DrawLines 單呼叫繪製全線條,**端點自動連接**,配合 clipRect.IntersectsWith 剪切優化。