Пример #1
0
 public Point GetPoint(double angle, double offset)
 {
     var v0 = new Vector(this.Radius + offset, 0);
     var rotate = v0.Rotate(angle);
     var p = this.Centre + rotate;
     return p;
 }
Пример #2
0
 public void FirstIntersectionWithCircleFromOutsideRoundtrips(string cs)
 {
     var circle = Circle.Parse(cs);
     var xv = new Vector(1, 0);
     for (var i = -180; i < 180; i++)
     {
         var direction = xv.Rotate(i);
         var pointOnCircumference = circle.PointOnCircumference(direction);
         var ray = new Ray(pointOnCircumference + direction, direction.Negated());
         var actual = ray.FirstIntersectionWith(circle);
         Assert.Equal(pointOnCircumference, actual, NullablePointComparer.Default);
     }
 }
Пример #3
0
 public void FirstIntersectionWithEllipseFromInsideRoundtrips(string es)
 {
     var ellipse = Ellipse.Parse(es);
     var xv = new Vector(1, 0);
     for (var i = -180; i < 180; i++)
     {
         var direction = xv.Rotate(i);
         var expected = ellipse.PointOnCircumference(direction);
         var ray = new Ray(ellipse.CenterPoint, direction);
         var actual = ray.FirstIntersectionWith(ellipse);
         Assert.Equal(expected, actual, NullablePointComparer.Default);
     }
 }
Пример #4
0
        public void FirstIntersectionWithRectFromOutsideRoundtrips(string rs)
        {
            var rect = Rect.Parse(rs);
            var xAxis = new Vector(1, 0);
            for (var i = -180; i < 180; i++)
            {
                var direction = xAxis.Rotate(i);
                var fromCenter = new Ray(rect.CenterPoint(), direction);
                var pointOnRect = fromCenter.FirstIntersectionWith(rect).GetValueOrDefault();
                var ray = new Ray(pointOnRect + direction, direction.Negated());
                var actual = ray.FirstIntersectionWith(rect);
                Assert.Equal(pointOnRect, actual, NullablePointComparer.Default);

                if (rect.ClosestCornerPoint(pointOnRect)
                        .DistanceTo(pointOnRect) < 0.01)
                {
                    continue;
                }

                Vector wallNormal;

                if (Math.Abs(pointOnRect.X - rect.Left) < Constants.Tolerance)
                {
                    wallNormal = new Vector(-1, 0);
                }
                else if(Math.Abs(pointOnRect.X - rect.Right) < Constants.Tolerance)
                {
                    wallNormal = new Vector(1, 0);
                }
                else if (Math.Abs(pointOnRect.Y - rect.Bottom) < Constants.Tolerance)
                {
                    wallNormal = new Vector(0, 1);
                }
                else // if (pointOnRect.Y == rect.Top)
                {
                    wallNormal = new Vector(0, -1);
                }

                for (var j = -89; j < 89; j++)
                {
                    var rayDirection = wallNormal.Rotate(j);
                    ray = new Ray(pointOnRect + rayDirection, rayDirection.Negated());
                    actual = ray.FirstIntersectionWith(rect);
                    if (!NullablePointComparer.Default.Equals(pointOnRect, actual))
                    {
                        Debugger.Break();
                    }

                    Assert.Equal(pointOnRect, actual, NullablePointComparer.Default);
                }
            }
        }
Пример #5
0
 public void FirstIntersectionWithEllipseFromOutsideRoundtrips(string es)
 {
     var ellipse = Ellipse.Parse(es);
     var xv = new Vector(1, 0);
     for (var i = -180; i < 180; i++)
     {
         var fromCenterDirection = xv.Rotate(i);
         var pointOnCircumference = ellipse.PointOnCircumference(fromCenterDirection);
         var ray = new Ray(pointOnCircumference + fromCenterDirection, fromCenterDirection.Negated());
         var actual = ray.FirstIntersectionWith(ellipse);
         Assert.Equal(pointOnCircumference, actual, NullablePointComparer.Default);
         for (var j = -70; j < 70; j++)
         {
             var direction = fromCenterDirection.Rotate(j);
             ray = new Ray(pointOnCircumference + direction, direction.Negated());
             actual = ray.FirstIntersectionWith(ellipse);
             //if (!NullablePointComparer.Default.Equals(pointOnCircumference, actual))
             //{
             //    Debugger.Break();
             //}
             Assert.Equal(pointOnCircumference, actual, NullablePointComparer.Default);
         }
     }
 }
        private static Animation CreateNetworkAnimation(Animation animation, Ani<GraphMessages> state, Lifetime life)
        {
            // --- end points
            // timeline
            animation.LinkMany(
                state.Select(s => s.Graph.EndPoints.Select(
                    p => new LineSegmentDesc(
                             new Point(s.Graph.GetX(p.Skew), s.Graph.GetY(p)).Sweep(new Vector(1000, 0)),
                             Brushes.Black,
                             3))),
                life);
            // timeline label
            animation.LinkMany(
                state.Select(s => s.Graph.EndPoints.Select(
                    p => new TextDesc(
                             text: p.Name,
                             pos: new Point(s.Graph.GetX(p.Skew), s.Graph.GetY(p)),
                             fontWeight: FontWeights.Bold,
                             reference: new Point(1.1, 0.5)))),
                life);
            // tick marks
            animation.LinkMany(
                state.Select(s => s.Graph.EndPoints.SelectMany(
                    p => 10.Range().Select(
                        j => new LineSegmentDesc(
                                 new Point(s.Graph.GetX(p.Skew + j.Seconds()), s.Graph.GetY(p) - 5).Sweep(new Vector(0, 10)),
                                 Brushes.Black,
                                 2)))),
                life);
            // tick labels
            animation.LinkMany(
                state.Select(s => s.Graph.EndPoints.SelectMany(
                    p => 10.Range().Select(
                        j => new TextDesc(
                                 text: (j + "s"),
                                 pos: new Point(s.Graph.GetX(p.Skew + j.Seconds()), s.Graph.GetY(p) - 5) + new Vector(0, -2),
                                 fontSize: 10)))),
                life);

            // --- measurements
            var measurements =
                from s in state
                select from m in s.Measurements
                       let x1 = s.Graph.GetX(m.X1)
                       let y1 = s.Graph.GetY(m.V1)
                       let x2 = s.Graph.GetX(m.X2)
                       let y2 = s.Graph.GetY(m.V2)
                       let y = m.Y ?? ((y1 + y2)/2)
                       select new {s, m, x1, y1, x2, y2, y};
            animation.LinkMany(
                measurements.LiftSelect(
                    e => new LineSegmentDesc(
                             new LineSegment(new Point(e.x1, e.y), new Point(e.x2, e.y)),
                             Brushes.Black,
                             2,
                             1)),
                life);
            animation.LinkMany(
                measurements.LiftSelect(
                    e => new LineSegmentDesc(
                             new LineSegment(new Point(e.x1, e.y1), new Point(e.x1, e.y)),
                             Brushes.Red,
                             dashed: 1)),
                    life);
            animation.LinkMany(
                measurements.LiftSelect(
                    e => new LineSegmentDesc(
                             new LineSegment(new Point(e.x2, e.y2), new Point(e.x2, e.y)),
                             Brushes.Red,
                             dashed: 1)),
                    life);
            var off1 = new Vector(5, -5);
            var off2 = new Vector(5, 12);
            animation.LinkMany(
                measurements.LiftSelect(
                    e => new TextDesc(
                        text: e.m.Text,
                        pos: new Point(Math.Max(e.x1, e.x2), e.y) + off1.Rotate(e.m.Angle),
                        direction: e.m.Angle)),
                    life);
            animation.LinkMany(
                measurements.LiftSelect(
                    e => new TextDesc(
                             text: string.Format("{0:0.00}s", (e.m.X2 - e.m.X1).TotalSeconds),
                             pos: new Point(Math.Max(e.x1, e.x2), e.y) + off2.Rotate(e.m.Angle),
                             direction: e.m.Angle)),
                life);

            // messages
            animation.LinkMany(
                state.Select(e => e.Messages).LiftSelect(
                    e => new LineSegmentDesc(
                             e.Pos,
                             Brushes.Black)),
                life);
            animation.LinkMany(
                state.Select(e => e.Messages.SelectMany(
                    m => new[] {m.PosSourcePoint, m.PosEndPoint}.Select(
                        p => new PointDesc(
                                 p,
                                 Brushes.Transparent,
                                 Brushes.Black,
                                 3,
                                 0)))),
                life);
            animation.LinkMany(
                state.Select(e => e.Messages).LiftSelect(
                    e => new TextDesc(
                             text: "-> " + e.Text + " ->",
                             pos: e.Pos.LerpAcross(0.1),
                             fontSize: 10,
                             direction: Dir.FromVector(e.Pos.Delta.X, e.Pos.Delta.Y),
                             foreground: Brushes.Gray)),
                life);

            return animation;
        }