आप किसी क्षेत्र को भरने के लिए Flood fill का उपयोग कर सकते हैं। यह एक प्रारंभिक बिंदु (या बीज बिंदु) इनपुट के रूप में लेता है और अपने खाली पड़ोसियों को भरने के प्रयास से क्षेत्र को फिर से भरता है।
जावास्क्रिप्ट में एक साधारण ढेर आधारित कार्यान्वयन:
// 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)
समारोह का परीक्षण करती है उस बिंदु (x, y)
(, हल्के हरे रंग की इस मामले में) नहीं सीमा रंग से भर जाता है कि क्या है या है।
setPixel(point)
(x, y)
को अग्रभूमि रंग (गहरा हरा, अपने मामले में) के साथ बिंदु भरता है।
implementation of these functions मामूली है, और मैं इसे आपके पास छोड़ देता हूं।
उपरोक्त कार्यान्वयन 4-connected पड़ोस का उपयोग करता है। लेकिन इसे आसानी से 6 या 8-जुड़े पड़ोस तक बढ़ाया जा सकता है।
स्रोत
2015-12-15 03:17:58
इसलिए यदि आप "संपूर्ण रूपरेखा भरना" चाहते थे, तो क्या यह स्कैनिंग गति में ऐसा करता है? लाइन से लाइन की तरह? यदि हां, तो शुरुआती बिंदु कैसे निर्धारित किया जाता है? यह अवतल आकार कैसे संभालता है? – thedarklord47
संभावित डुप्लिकेट [मैं एचटीएमएल कैनवास के साथ बाढ़ भर कैसे कर सकता हूं?] (Http://stackoverflow.com/questions/2106995/how-can-i-perform-flood-fill-with-html-canvas) – iamnotmaynard