private LineSegment GetInterpolatedSegment(LineSegment segmentA, LineSegment segmentB)
		{
			double colorAStartThreshold = thersholdPerPixel[((int)segmentA.start.x) + ((int)segmentA.start.y) * imageToMarch.Width];
			double colorAEndThreshold = thersholdPerPixel[((int)segmentA.end.x) + ((int)segmentA.end.y) * imageToMarch.Width];

			Vector2 directionA = segmentA.end - segmentA.start;
			double offsetA = 1 - (colorAEndThreshold + colorAStartThreshold) / 2.0;
			directionA *= offsetA;

			double colorBStartThreshold = thersholdPerPixel[((int)segmentB.start.x) + ((int)segmentB.start.y) * imageToMarch.Width];
			double colorBEndThreshold = thersholdPerPixel[((int)segmentB.end.x) + ((int)segmentB.end.y) * imageToMarch.Width];

			Vector2 directionB = segmentB.end - segmentB.start;
			double ratioB = 1 - (colorBEndThreshold + colorBStartThreshold) / 2.0;
			directionB *= ratioB;

			double offsetToPixelCenter = .5;
			LineSegment segment = new LineSegment(
				(segmentA.start.x + directionA.x) + offsetToPixelCenter,
				(segmentA.start.y + directionA.y) + offsetToPixelCenter,
				(segmentB.start.x + directionB.x) + offsetToPixelCenter,
				(segmentB.start.y + directionB.y) + offsetToPixelCenter,
				segmentA.color);

			return segment;
		}
Esempio n. 2
0
        public Polygons CreateLineLoops(int pixelsToIntPointsScale, int maxLineLoopsToAdd = int.MaxValue)
        {
            Polygons LineLoops = new Polygons();

            bool[] hasBeenAddedList = new bool[LineSegments.Count];

            for (int segmentToAddIndex = 0; segmentToAddIndex < LineSegments.Count; segmentToAddIndex++)
            {
                if (!hasBeenAddedList[segmentToAddIndex])
                {
                    // now find all the connected segments until we get back to this one
                    Polygon loopToAdd = new Polygon();

                    // walk the loop
                    int         currentSegmentIndex = segmentToAddIndex;
                    LineSegment currentSegment      = LineSegments[currentSegmentIndex];
                    Vector2     connectionVertex    = currentSegment.end;
                    loopToAdd.Add(new IntPoint((long)(connectionVertex.x * pixelsToIntPointsScale), (long)(connectionVertex.y * pixelsToIntPointsScale)));
                    hasBeenAddedList[currentSegmentIndex] = true;
                    bool addedToLoop = false;
                    do
                    {
                        bool foundNextSegment = false;
                        addedToLoop = false;
                        for (int segmentToCheckIndex = 0; segmentToCheckIndex < LineSegments.Count; segmentToCheckIndex++)
                        {
                            LineSegment segmentToCheck = LineSegments[segmentToCheckIndex];
                            if (!hasBeenAddedList[segmentToCheckIndex])
                            {
                                if (connectionVertex == segmentToCheck.start)
                                {
                                    connectionVertex = segmentToCheck.end;
                                    foundNextSegment = true;
                                }
                                else if (connectionVertex == segmentToCheck.end)
                                {
                                    connectionVertex = segmentToCheck.start;
                                    foundNextSegment = true;
                                }
                                else
                                {
                                    // int i = 0;
                                }

                                if (foundNextSegment)
                                {
                                    hasBeenAddedList[segmentToCheckIndex] = true;
                                    currentSegmentIndex = segmentToCheckIndex;
                                    currentSegment      = segmentToCheck;
                                    loopToAdd.Add(new IntPoint((long)(connectionVertex.x * pixelsToIntPointsScale), (long)(connectionVertex.y * pixelsToIntPointsScale)));
                                    addedToLoop = true;
                                    break;
                                }
                            }
                        }
                    } while (addedToLoop);

                    LineLoops.Add(loopToAdd);
                    if (LineLoops.Count > maxLineLoopsToAdd)
                    {
                        break;
                    }
                }
            }

            return(LineLoops);
        }