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!
Modifycombinedebug the supplied code to read in a bitmap, store it as a quadtree, and the
count the number of water pixels adjacent to a userspecified 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.
Highlevel 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 by 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
fmtPrintEnter filename:
fmtScan&filename
inFile, err : osOpenfilename
if err nil
fmtPrintlnError opening file:", err
return
defer inFile.Close
fmtPrintlnYou opened", filename
scanner : bufio.NewScannerinFile
Skip first two lines
scanner.Scan skip first line
scanner.Scan skip second line
Read xmax and ymax
scanner.Scan
dimensions : strings.FieldsscannerText
if lendimensions
fmtPrintlnError: Unexpected format for dimensions"
return
xmax, err strconv.Atoidimensions
if err nil
fmtPrintlnError converting xmax:", err
return
ymax, err strconv.Atoidimensions
if err nil
fmtPrintlnError converting ymax:", err
return
bitmap makeint xmax
for i : range bitmap
bitmapi makeint ymax
for y :; y ymax; y
for x :; x xmax; x
fmtFscanfinFiled &bitmapxy
Display map to demonstrate that it has been read in
Do not print this out in your final submission.
fmtPrintfMap read in was d by d
xmax, ymax
for y :; y ymax; y
for x :; x xmax; x
fmtPrintbitmapxy
fmtPrintln
CONVERT THIS BITMAP INTO A QUADTREE
REPORT THE NUMBER OF NODES AND ITS MAX DEPTH
rootNode : buildQuadtreebitmap
xUser, yUser : Example userspecified coordinates modify as needed
modify code to meet the instructions requirements
fmtPrintfAdjacent water pixels: d
This has not been debugged but is offered as a help.
type QuadtreeNode struct
Value int Value of the node or
IsLeaf bool
TopLeft QuadtreeNode
TopRight QuadtreeNode
BottomLeft QuadtreeNode
BottomRight QuadtreeNode
func buildQuadtreebitmap intQuadtreeNode
return buildQuadtreeHelperbitmap lenbitmap lenbitmap
func buildQuadtreeHelperbitmap int x y width, height intQuadtreeNode
if width && height
return &QuadtreeNode
Value: bitmapxy
IsLeaf: true,
Check if all values in the region are the same
isUniform : true
baseValue : bitmapxy
for i : x; i xwidth; i
for j : y; j yheight; j
if bitmapij baseValue
isUniform false
break
if isUniform
break
if isUniform
return &QuadtreeNode
Value: baseValue,
IsLeaf: true,
Split the region into four quadrants
midX : x width
midY : y height
return &QuadtreeNode
IsLeaf: false,
TopLeft: buildQuadtreeHelperbitmap x y midXx midYy
TopRight: buildQuadtreeHelperbitmap midX, y xwidthmidX, midYy
BottomLeft: buildQuadtreeHelperbitmap x midY, midXx yheightmidY
BottomRight: buildQuadtreeHelperbitmap midX, midY, xwidthmidX, yheightmidY
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
