/// <summary> /// 判断给定点是否在散射体内部 /// 采用射线法:从给定点作一条射线,若与多边形有偶数个交点 /// 则在多边形外,若有奇数个交点则在内部 /// </summary> /// <param name="pt"></param> /// <returns></returns> public bool IsPointInside(Point pt) { if(this.BorderRect.IntersectsWith(new Rectangle(pt, new Size(1, 1)))) // 先判断点是否在边界矩形内 { int l = (int)(Math.Pow(this.BorderRect.Width, 2) + Math.Pow(this.BorderRect.Height, 2)) + 2; LineSegment seg_l = new LineSegment(pt, new Point(pt.X + l, pt.Y)); int pt_num = 0; foreach (LineSegment seg in this.BorderSegments) if (seg_l.JudgeIntersection(seg)) pt_num++; return pt_num % 2 == 1; } return false; }
/// <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]; }