Ejemplo n.º 1
0
        public Vector3?Intersect(LinePathSegment other)  //xy: point, z: distance along line
        {
            if (this.Length == 0 || other.Length == 0)
            {
                return(null);
            }
            var epsilon = 0.0001f;

            Vector2 a = Direction;
            Vector2 b = other.Direction;
            Vector2 c = Start - other.Start;

            //percent of line 1 where we intersect with line 2
            float ip = 1 / (-b.X * a.Y + a.X * b.Y); //projection
            float t  = (b.X * c.Y - b.Y * c.X) * ip;

            //percent of line 2 where we intersect line 1
            float ip2 = 1 / (-a.X * b.Y + b.X * a.Y);
            float s   = (a.X * (-c.Y) - a.Y * (-c.X)) * ip2;

            if (float.IsNaN(t) || t < -epsilon || t > 1 + epsilon || float.IsNaN(s) || s < -epsilon || s > 1 + epsilon)
            {
                return(null);
            }
            return(new Vector3(Direction * t + Start, t * Length));
        }
Ejemplo n.º 2
0
 public LinePath(List <Vector2> line)
 {
     for (int i = 0; i < line.Count - 1; i++)
     {
         var seg = new LinePathSegment(line[i], line[i + 1]);
         Length += seg.Length;
         Segments.Add(seg);
     }
 }
Ejemplo n.º 3
0
        public void PrepareJoins()
        {
            LinePathSegment last = null;

            foreach (var line in Segments)
            {
                if (last != null)
                {
                    last.EndNormal = line.StartNormal = Vector2.Normalize(last.EndNormal + line.StartNormal);
                }
                last = line;
            }
        }
Ejemplo n.º 4
0
        public List <LinePath> Split(float dist, float gap)
        {
            var result   = new List <LinePath>();
            var startGap = dist - gap / 2;
            var endGap   = dist + gap / 2;

            bool     before  = 0 < startGap;
            LinePath current = new LinePath();

            if (before)
            {
                current.SharpStart  = SharpStart;
                current.SharpEnd    = true;
                current.StartOffset = StartOffset;
            }
            else
            {
                current.SharpStart  = true;
                current.SharpEnd    = SharpEnd;
                current.StartOffset = StartOffset + endGap;
            }
            current.TemplateNum = TemplateNum;

            float soFar = 0;

            foreach (var segment in Segments)
            {
                if (before)
                {
                    if (soFar + segment.Length <= startGap)
                    {
                        //add this segment
                        current.Segments.Add(segment);
                    }
                    else
                    {
                        //this segment extends over the gap.
                        //an additional segment must be added to reach the start gap
                        if (soFar != startGap && segment.Length != 0)
                        {
                            var bridge = new LinePathSegment(segment.Start, Vector2.Lerp(segment.Start, segment.End, (startGap - soFar) / segment.Length));
                            bridge.StartNormal = segment.StartNormal;
                            current.Segments.Add(bridge);
                        }

                        current.Length = current.Segments.Sum(x => x.Length);
                        result.Add(current);
                        current             = new LinePath();
                        current.SharpStart  = true;
                        current.SharpEnd    = SharpEnd;
                        current.StartOffset = StartOffset + endGap;
                        current.TemplateNum = TemplateNum;
                        before = false;
                    }
                }
                if (!before)
                {
                    if (current.Segments.Count == 0)
                    {
                        //waiting to get to a segment that ends after the gap.
                        if (soFar + segment.Length > endGap)
                        {
                            var bridge = new LinePathSegment(Vector2.Lerp(segment.Start, segment.End, (endGap - soFar) / segment.Length), segment.End);
                            bridge.EndNormal = segment.EndNormal;
                            current.Segments.Add(bridge);
                        }
                    }
                    else
                    {
                        //add this segment
                        current.Segments.Add(segment);
                    }
                }

                soFar += segment.Length;
            }
            current.Length = current.Segments.Sum(x => x.Length);
            result.Add(current);
            return(result);
        }