Question: CODE IN GOLANG languague, please do not use any other languague but GOLANG! Modify / combine / debug the supplied code to read in a

CODE IN GOLANG languague, please do not use any other languague but GOLANG!
Modify/combine/debug the supplied code to read in a bitmap, store it as a quadtree, and the
count the number of water pixels adjacent to a user-specified pixel.
Identify neighbors across different levels of the quadtree. One common approach is to use
recursion to traverse the tree and identify neighbors based on their spatial relationship.
High-level approach to handle neighbors at different levels:
Identify Current Node's Region: Given the pixel's coordinates, find the node in the
quadtree that represents the region containing that pixel.
Recursively Traverse to Find Neighbors: Traverse the quadtree recursively, starting from
the root or the current node, to find neighboring nodes at the same or adjacent levels. As
you traverse, you need to keep track of the relationship between nodes and their
positions in the tree.
Check Spatial Relationship: During traversal, check the spatial relationship between
nodes to determine if they are neighbors. You can compare the regions represented by
nodes to see if they are adjacent or overlapping.
Handle Different Levels: If neighbors are at different levels, you may need to adjust your
traversal strategy accordingly. For example, you might traverse down the tree to find
neighbors at lower levels or traverse up the tree to find neighbors at higher levels.
When counting, remember individual leaves represent more than one pixel, so you'll need to
add the appropriate number depending on the depth of the tree at that leaf.
To compare this representation to the original x by Y pixel bitmap, count the number of nodes in
the tree. Is a quadtree representation more space efficient?
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
var (
xmax int
ymax int // size of bitmap
bitmap [][]int
)
func main(){
var filename string
fmt.Print("Enter filename: ")
fmt.Scan(&filename)
inFile, err := os.Open(filename)
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer inFile.Close()
fmt.Println("You opened", filename)
scanner := bufio.NewScanner(inFile)
// Skip first two lines
scanner.Scan()// skip first line
scanner.Scan()// skip second line
// Read xmax and ymax
scanner.Scan()
dimensions := strings.Fields(scanner.Text())
if len(dimensions)!=2{
fmt.Println("Error: Unexpected format for dimensions")
return
}
xmax, err = strconv.Atoi(dimensions[0])
if err != nil {
fmt.Println("Error converting xmax:", err)
return
}
ymax, err = strconv.Atoi(dimensions[1])
if err != nil {
fmt.Println("Error converting ymax:", err)
return
}
bitmap = make([][]int, xmax)
for i := range bitmap {
bitmap[i]= make([]int, ymax)
}
for y :=0; y ymax; y++{
for x :=0; x xmax; x++{
fmt.Fscanf(inFile,"%d", &bitmap[x][y])
}
}
// Display map to demonstrate that it has been read in
// Do not print this out in your final submission.
fmt.Printf("Map read in was %d by %d
", xmax, ymax)
for y :=0; y ymax; y++{
for x :=0; x xmax; x++{
fmt.Print(bitmap[x][y])
}
fmt.Println()
}
// CONVERT THIS BITMAP INTO A QUADTREE
// REPORT THE NUMBER OF NODES AND ITS MAX DEPTH
rootNode := buildQuadtree(bitmap)
xUser, yUser :=5,1// Example user-specified coordinates (modify as needed)
// modify code to meet the instructions / requirements
fmt.Printf("Adjacent water pixels: %d",
}
// This has not been debugged but is offered as a help.
type QuadtreeNode struct {
Value int // Value of the node (0 or 1)
IsLeaf bool
TopLeft *QuadtreeNode
TopRight *QuadtreeNode
BottomLeft *QuadtreeNode
BottomRight *QuadtreeNode
}
func buildQuadtree(bitmap [][]int)*QuadtreeNode {
return buildQuadtreeHelper(bitmap,0,0, len(bitmap), len(bitmap[0]))
}
func buildQuadtreeHelper(bitmap [][]int, x, y, width, height int)*QuadtreeNode {
if width ==1 && height ==1{
return &QuadtreeNode{
Value: bitmap[x][y],
IsLeaf: true,
}
}
// Check if all values in the region are the same
isUniform := true
baseValue := bitmap[x][y]
for i := x; i x+width; i++{
for j := y; j y+height; j++{
if bitmap[i][j]!= baseValue {
isUniform = false
break
}
}
if !isUniform {
break
}
}
if isUniform {
return &QuadtreeNode{
Value: baseValue,
IsLeaf: true,
}
}
// Split the region into four quadrants
midX := x + width/2
midY := y + height/2
return &QuadtreeNode{
IsLeaf: false,
TopLeft: buildQuadtreeHelper(bitmap, x, y, midX-x, midY-y),
TopRight: buildQuadtreeHelper(bitmap, midX, y, x+width-midX, midY-y),
BottomLeft: buildQuadtreeHelper(bitmap, x, midY, midX-x, y+height-midY),
BottomRight: buildQuadtreeHelper(bitmap, midX, midY, x+width-midX, y+height-midY),
}
}
CODE IN GOLANG languague, please do not use any

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!