Sie können Flood fill verwenden, um eine Region zu füllen. Es nimmt einen Startpunkt (oder den Startpunkt) als eine Eingabe und füllt die Region rekursiv, indem es versucht, seine leeren Nachbarn zu füllen.
Eine einfache stapelbasierten Implementierung in JavaScript:
// Takes the seed point as input
var floodfill = function(point) {
var stack = Array();
stack.push(point); // Push the seed
while(stack.length > 0) {
var currPoint = stack.pop();
if(isEmpty(currPoint)) { // Check if the point is not filled
setPixel(currPoint); // Fill the point with the foreground
stack.push(currPoint.x + 1, currPoint.y); // Fill the east neighbour
stack.push(currPoint.x, currPoint.y + 1); // Fill the south neighbour
stack.push(currPoint.x - 1, currPoint.y); // Fill the west neighbour
stack.push(currPoint.x, currPoint.y - 1); // Fill the north neighbour
}
}
};
isEmpty(point)
ist die Funktion, die (in diesem Fall Licht grün) mit der Randfarbe wird der Punkt (x, y)
gefüllt oder nicht, überprüft, ob.
setPixel(point)
füllt den Punkt (x, y)
mit der Vordergrundfarbe (dunkelgrün, in Ihrem Fall).
Die implementation of these functions ist trivial, und ich überlasse es Ihnen. Die folgende Implementierung verwendet eine 4-connected Nachbarschaft. Aber es kann leicht zu 6 oder 8 verbundenen Nachbarschaften erweitert werden.
Wenn Sie also "den gesamten Umriss ausfüllen" würden, ist dies bei einer Scan-Bewegung der Fall? wie Zeile für Zeile? Wenn ja, wie ist der Ausgangspunkt festgelegt? Wie geht es mit konkaven Formen um? – thedarklord47
Mögliches Duplikat von [Wie kann ich eine Flutfüllung mit HTML Canvas durchführen?] (Http://stackoverflow.com/questions/2106995/how-can-i-perform-flood-fill-with-html-canvas) – iamnotmaynard