예제 #1
0
        static bool Snap(List <Snapline> input, List <Snapline> map, double accuracy,
                         out List <Snapline> drawLines, out double delta)
        {
            delta     = double.MaxValue;
            drawLines = null;

            foreach (var inputLine in input)
            {
                foreach (var mapLine in map)
                {
                    if (Math.Abs(mapLine.Offset - inputLine.Offset) <= accuracy)
                    {
                        if (!inputLine.RequireOverlap && !mapLine.RequireOverlap ||
                            Math.Max(inputLine.Start, mapLine.Start) < Math.Min(inputLine.End, mapLine.End))
                        {
                            if (mapLine.Group == inputLine.Group)
                            {
                                delta = mapLine.Offset - inputLine.Offset;
                            }
                        }
                    }
                }
            }

            if (delta == double.MaxValue)
            {
                return(false);
            }
            var offsetDict = new Dictionary <double, Snapline>();

            foreach (var inputLine in input)
            {
                inputLine.Offset += delta;
                foreach (var mapLine in map)
                {
                    if (inputLine.Offset == mapLine.Offset)
                    {
                        var      offset = mapLine.Offset;
                        Snapline drawLine;
                        if (!offsetDict.TryGetValue(offset, out drawLine))
                        {
                            drawLine            = new Snapline();
                            drawLine.Start      = double.MaxValue;
                            drawLine.End        = double.MinValue;
                            drawLine.DrawOffset = mapLine.DrawOffset;
                            offsetDict[offset]  = drawLine;
                        }
                        drawLine.Offset = offset;
                        drawLine.Start  = Math.Min(drawLine.Start, Math.Min(inputLine.Start, mapLine.Start));
                        drawLine.End    = Math.Max(drawLine.End, Math.Max(inputLine.End, mapLine.End));
                    }
                }
            }

            drawLines = offsetDict.Values.ToList();
            return(true);
        }
		static bool Snap(List<Snapline> input, List<Snapline> map, double accuracy,
		                 out List<Snapline> drawLines, out double delta)
		{
			delta = double.MaxValue;
			drawLines = null;

			foreach (var inputLine in input) {
				foreach (var mapLine in map) {
					if (Math.Abs(mapLine.Offset - inputLine.Offset) <= accuracy) {
						if (!inputLine.RequireOverlap && !mapLine.RequireOverlap ||
						    Math.Max(inputLine.Start, mapLine.Start) < Math.Min(inputLine.End, mapLine.End))
						{
							if (mapLine.Group == inputLine.Group)
								delta = mapLine.Offset - inputLine.Offset;
						}
					}
				}
			}

			if (delta == double.MaxValue) return false;
			var offsetDict = new Dictionary<double, Snapline>();

			foreach (var inputLine in input) {
				inputLine.Offset += delta;
				foreach (var mapLine in map) {
					if (inputLine.Offset == mapLine.Offset) {
						var offset = mapLine.Offset;
						Snapline drawLine;
						if (!offsetDict.TryGetValue(offset, out drawLine)) {
							drawLine = new Snapline();
							drawLine.Start = double.MaxValue;
							drawLine.End = double.MinValue;
							offsetDict[offset] = drawLine;
						}
						drawLine.Offset = offset;
						drawLine.Start = Math.Min(drawLine.Start, Math.Min(inputLine.Start, mapLine.Start));
						drawLine.End = Math.Max(drawLine.End, Math.Max(inputLine.End, mapLine.End));
					}
				}
			}

			drawLines = offsetDict.Values.ToList();
			return true;
		}