/// <summary> /// 获取反射点坐标及碰撞线段 /// </summary> /// <param name="seg"></param> /// <returns></returns> public Point? GetClosestReflectionPoint(LineSegment seg, out LineSegment collision_seg) { if (!this.BorderRect.IntersectsWith(new Rectangle(seg.Left, seg.Top, seg.Right - seg.Left, seg.Bottom - seg.Top))) //先用矩形进行快速判断 { collision_seg = null; return null; } Dictionary<Point?, int> pt_idx_dict = new Dictionary<Point?, int>(); for (int i = 0; i < this.border_segments.Length; i++) { Point? pt = seg.GetIntersectionPoint(this.border_segments[i]); if (pt != null && !pt_idx_dict.Keys.Contains(pt)) pt_idx_dict.Add(pt, i); } if(pt_idx_dict.Count == 0) //没有交点 { collision_seg = null; return null; } KeyValuePair<Point?, int> pt_idx = new KeyValuePair<Point?, int>(); double min_d2 = double.PositiveInfinity; foreach(KeyValuePair<Point?, int> kvp in pt_idx_dict) { double dist2 = Math.Pow(kvp.Key.Value.X - seg.TailPosition.X, 2) + Math.Pow(kvp.Key.Value.Y - seg.TailPosition.Y, 2); if(dist2 > 0 && dist2 < min_d2) { min_d2 = dist2; pt_idx = kvp; } } collision_seg = this.border_segments[pt_idx.Value]; return pt_idx.Key; }
/// <summary> /// 线段类的单元测试 /// </summary> /// <param name="epsilon"></param> /// <returns></returns> public static string RunTest(double epsilon=1e-3) { LineSegment This = new LineSegment(); LineSegment That = new LineSegment(); DateTime time = DateTime.Now; StringBuilder sb = new StringBuilder("LineSegment Class Test Begin at " + time.ToString("yyyy-MM-dd HH:mm:ss") + "\n"); test_log.Add(time, ""); //test Length sb.AppendLine("----------------------------"); sb.AppendLine("length test 1"); Debug.Assert(new LineSegment(new Point(0, 0), new Point(0, 0)).Length == 0, "length should be 0"); sb.AppendLine("length test 2"); Debug.Assert(new LineSegment(new Point(0, 1), new Point(1, 0)).Length == Math.Sqrt(2), "length should be sqrt(2)"); sb.AppendLine("length test 3"); Debug.Assert(new LineSegment(new Point(2, 0), new Point(0, 0)).Length == 2, "length should be 2"); sb.AppendLine("length test 4"); Debug.Assert(new LineSegment(new Point(0, 0), new Point(0, 2)).Length == 2, "length should be 2"); //test DirectionRadius sb.AppendLine("----------------------------"); sb.AppendLine("direction test 1"); Debug.Assert(new LineSegment(new Point(0, 0), new Point(0, 0)).DirectionRadian == 0, "direction should be 0"); sb.AppendLine("direction test 2"); Debug.Assert(new LineSegment(new Point(1, 0), new Point(0, 0)).DirectionRadian == 0, "direction should be 0"); sb.AppendLine("direction test 3"); Debug.Assert(new LineSegment(new Point(0, 0), new Point(0, 1)).DirectionRadian == Math.PI / 2, "direction should be pi/2"); sb.AppendLine("direction test 4"); Debug.Assert(new LineSegment(new Point(0, 0), new Point(1, 0)).DirectionRadian == Math.PI, "direction should be pi"); sb.AppendLine("direction test 5"); Debug.Assert(new LineSegment(new Point(0, 1), new Point(0, 0)).DirectionRadian == Math.PI / 2 * 3, "direction should be pi/2*3"); sb.AppendLine("direction test 6"); Debug.Assert(new LineSegment(new Point(1, 0), new Point(0, 1)).DirectionRadian == Math.PI / 4, "direction should be pi/4"); sb.AppendLine("direction test 7"); Debug.Assert(new LineSegment(new Point(0, 0), new Point(1, 1)).DirectionRadian == Math.PI / 4 * 3, "direction should be pi/4*3"); sb.AppendLine("direction test 8"); Debug.Assert(new LineSegment(new Point(0, 1), new Point(1, 0)).DirectionRadian == Math.PI / 4 * 5, "direction should be pi/4*5"); sb.AppendLine("direction test 9"); Debug.Assert(new LineSegment(new Point(1, 1), new Point(0, 0)).DirectionRadian == Math.PI / 4 * 7, "direction should be pi/4*7"); //test Angle sb.AppendLine("----------------------------"); This = new LineSegment(new Point(1, 0), new Point(0, 0)); sb.AppendLine("angle test 1"); That = new LineSegment(new Point(1, 0), new Point(0, 0)); Debug.Assert(This.Angle(That) == 0, "angle should be 0"); sb.AppendLine("angle test 2"); That = new LineSegment(new Point(0, 0), new Point(0, 1)); Debug.Assert(This.Angle(That) == Math.PI / 2, "angle should be pi/2"); sb.AppendLine("angle test 3"); That = new LineSegment(new Point(0, 0), new Point(1, 0)); Debug.Assert(This.Angle(That) == Math.PI, "angle should be pi"); sb.AppendLine("angle test 4"); That = new LineSegment(new Point(0, 1), new Point(0, 0)); Debug.Assert(This.Angle(That) == Math.PI / 2, "angle should be pi/2"); sb.AppendLine("angle test 5"); That = new LineSegment(new Point(1, 0), new Point(0, 1)); Debug.Assert(This.Angle(That) > Math.PI / 4 - epsilon && This.Angle(That) < Math.PI / 4 + epsilon, "angle should be pi/4"); sb.AppendLine("angle test 6"); Debug.Assert(new LineSegment(new Point(1, 0), new Point(0, 0)).Angle(new LineSegment(new Point(0, 0), new Point(1, 1))) == Math.PI / 4 * 3, "angle should be pi/4*3"); sb.AppendLine("angle test 7"); Debug.Assert(new LineSegment(new Point(1, 0), new Point(0, 0)).Angle(new LineSegment(new Point(0, 1), new Point(1, 0))) == Math.PI / 4 * 3, "angle should be pi/4*3"); sb.AppendLine("angle test 8"); That = new LineSegment(new Point(1, 1), new Point(0, 0)); Debug.Assert(This.Angle(That) > Math.PI / 4 - epsilon && This.Angle(That) < Math.PI / 4 + epsilon, "angle should be pi/4"); sb.AppendLine("angle test 9"); That = new LineSegment(new Point(0, 0), new Point(0, 0)); Debug.Assert(This.Angle(That) == 0, "angle should be 0"); //test JudgeIntersection sb.AppendLine("----------------------------"); This = new LineSegment(new Point(2, 2), new Point(0, 2)); sb.AppendLine("intersection test 1"); That = new LineSegment(new Point(1, 2), new Point(0, 2)); Debug.Assert(This.JudgeIntersection(That) == false, "intersection should be false"); sb.AppendLine("intersection test 2"); That = new LineSegment(new Point(1, 1), new Point(0, 1)); Debug.Assert(This.JudgeIntersection(That) == false, "intersection should be false"); sb.AppendLine("intersection test 3"); That = new LineSegment(new Point(0, 1), new Point(1, 1)); Debug.Assert(This.JudgeIntersection(That) == false, "intersection should be false"); sb.AppendLine("intersection test 4"); That = new LineSegment(new Point(1, 0), new Point(1, 1)); Debug.Assert(This.JudgeIntersection(That) == false, "intersection should be false"); sb.AppendLine("intersection test 5"); That = new LineSegment(new Point(1, 1), new Point(1, 0)); Debug.Assert(This.JudgeIntersection(That) == false, "intersection should be false"); sb.AppendLine("intersection test 6"); That = new LineSegment(new Point(1, 1), new Point(0, 2)); Debug.Assert(This.JudgeIntersection(That) == true, "intersection should be true"); sb.AppendLine("intersection test 7"); That = new LineSegment(new Point(1, 1), new Point(2, 2)); Debug.Assert(This.JudgeIntersection(That) == true, "intersection should be true"); sb.AppendLine("intersection test 8"); That = new LineSegment(new Point(3, 1), new Point(2, 2)); Debug.Assert(This.JudgeIntersection(That) == true, "intersection should be true"); sb.AppendLine("intersection test 9"); That = new LineSegment(new Point(2, 1), new Point(1, 2)); Debug.Assert(This.JudgeIntersection(That) == true, "intersection should be true"); sb.AppendLine("intersection test 10"); That = new LineSegment(new Point(1, 2), new Point(2, 3)); Debug.Assert(This.JudgeIntersection(That) == true, "intersection should be true"); sb.AppendLine("intersection test 11"); That = new LineSegment(new Point(0, 0), new Point(2, 3)); Debug.Assert(This.JudgeIntersection(That) == true, "intersection should be true"); sb.AppendLine("intersection test 12"); That = new LineSegment(new Point(1, 0), new Point(0, 3)); Debug.Assert(This.JudgeIntersection(That) == true, "intersection should be true"); sb.AppendLine("intersection test 13"); That = new LineSegment(new Point(1, 3), new Point(1, 1)); Debug.Assert(This.JudgeIntersection(That) == true, "intersection should be true"); sb.AppendLine("intersection test 14"); That = new LineSegment(new Point(3, 2), new Point(2, 2)); Debug.Assert(This.JudgeIntersection(That) == false, "intersection should be false"); sb.AppendLine("intersection test 15"); That = new LineSegment(new Point(1, 2), new Point(1, 2)); Debug.Assert(This.JudgeIntersection(That) == false, "intersection should be false"); //test GetIntersectionPoint sb.AppendLine("----------------------------"); This = new LineSegment(new Point(3, 2), new Point(1, 2)); sb.AppendLine("intersection point test 1"); That = new LineSegment(new Point(3, 1), new Point(1, 1)); Debug.Assert(This.GetIntersectionPoint(That) == null, "intersection point should be null"); sb.AppendLine("intersection point test 2"); That = new LineSegment(new Point(4, 1), new Point(3, 2)); Debug.Assert(This.GetIntersectionPoint(That) == new Point(3, 2), "intersection point should be (3, 2)"); sb.AppendLine("intersection point test 3"); That = new LineSegment(new Point(3, 1), new Point(3, 2)); Debug.Assert(This.GetIntersectionPoint(That) == new Point(3, 2), "intersection point should be (3, 2)"); sb.AppendLine("intersection point test 4"); That = new LineSegment(new Point(2, 1), new Point(3, 2)); Debug.Assert(This.GetIntersectionPoint(That) == new Point(3, 2), "intersection point should be (3, 2)"); sb.AppendLine("intersection point test 5"); That = new LineSegment(new Point(3, 2), new Point(2, 3)); Debug.Assert(This.GetIntersectionPoint(That) == new Point(3, 2), "intersection point should be (3, 2)"); sb.AppendLine("intersection point test 6"); That = new LineSegment(new Point(3, 2), new Point(4, 3)); Debug.Assert(This.GetIntersectionPoint(That) == new Point(3, 2), "intersection point should be (3, 2)"); sb.AppendLine("intersection point test 7"); That = new LineSegment(new Point(2, 1), new Point(2, 3)); Debug.Assert(This.GetIntersectionPoint(That) == new Point(2, 2), "intersection point should be (2, 2)"); sb.AppendLine("intersection point test 8"); That = new LineSegment(new Point(1, 1), new Point(3, 3)); Debug.Assert(This.GetIntersectionPoint(That) == new Point(2, 2), "intersection point should be (2, 2)"); sb.AppendLine("intersection point test 9"); That = new LineSegment(new Point(3, 3), new Point(1, 1)); Debug.Assert(This.GetIntersectionPoint(That) == new Point(2, 2), "intersection point should be (2, 2)"); sb.AppendLine("intersection point test 10"); That = new LineSegment(new Point(3, 1), new Point(1, 3)); Debug.Assert(This.GetIntersectionPoint(That) == new Point(2, 2), "intersection point should be (2, 2)"); sb.AppendLine("intersection point test 11"); That = new LineSegment(new Point(1, 3), new Point(3, 1)); Debug.Assert(This.GetIntersectionPoint(That) == new Point(2, 2), "intersection point should be (2, 2)"); sb.AppendLine("intersection point test 12"); That = new LineSegment(new Point(2, 3), new Point(0, 1)); Debug.Assert(This.GetIntersectionPoint(That) == new Point(1, 2), "intersection point should be (1, 2)"); sb.AppendLine("intersection point test 13"); That = new LineSegment(new Point(2, 2), new Point(2, 2)); Debug.Assert(This.GetIntersectionPoint(That) == null, "intersection point should be null"); test_log[time] = sb.ToString(); return test_log[time]; }