Discover that a filled-in grid has a circle; fill in the area - loops

Hi, I'm currently in the middle of a project where a new grid is added on to a chain of blocks on the grid every timestep. How would I be able to detect that a circle has been made in the grid? Given that all I have are the coordinates (x,y) and the color of each cell. By "circle" I mean an area that is sealed off, as shown in the picture.
Thanks in advance! By the way, I'm not asking how to click on a cell and apply the flood-fill algorithm.
The aftermath of the algorithm should produce this:

You need to split all of your white (unfilled) squares into sets of squares adjacent to each other. Start with any white square, add all of its unfilled adjacent squares to the set, and keep doing it until you've included all of the squares.
Once you have those sets, you will have a "circle" (as you named it) if there are non-empty sets that do not contain any border squares. Then to fill these sets you just change the color of each member to blue.
If you have the sets from the previous step, when you add another brick you just need to consider the set that included the affected square to see if it has been split into two sets and whether either of these new sets may be a "circle".

Related

Graphs the same size in gnuplot multiplot when each take up most of its canvas and one has labels

I've reedited this question a few times: I've made some good progress!
So, as I understand it, multiplot splits the whole canvas up into equal sized parts as needed. This is a little weird when your different plots have different dimensions, as in my case, but it works. The problem might come in when the graph are supposed to be very close together (e.g. each takes up most of its canvas), but one of them has labels. In that case, it seems the plot with labels must resize to be smaller so everything can fit. That's where I am now.
I see a few options.
make all the plots farther apart-- but I don't want to do that.
somehow make the label not part of the multiplot-- I would totally do this, but I don't know how. It's possible even just the axis tics themselves would be too big, but I can probably deal with that or compromise just that amount on the spacing.
So my question is, how can I put words in a gnuplot graph, completely separately from a plot?
(The picture is also giant, which is unfortunate, it was the only way I could make the formatting work)
Two things:
Multiplot has a convenience mode layout <rows>, <columns> that, as you say, splits the page into equal rectangles. But you do not have to use this convenience mode; you can assign each sub-plot to any arbitrary rectangle on the page, even one that overlaps or is interior to another rectangle. Here is an example from the online demo set that is close to what you show:
Demo of multiple plots with explicit alignment of borders
Placing text anywhere on the page: The set label command allows you to position the text using screen coordinates rather than plot coordinates. For example, to place a single large label centered at the top of a page that contains multiple plots:
set label 1 "This label is positioned independent of all plots"
set label 1 at screen 0.5, screen 0.95 center
set label 1 font "Times,20"

algorithm to find how many circles there are in 2d array

I'm given a 2d binary array. Some of the dots are on, some are off (1 for on, 0 for off).
I know that the "on" dots were created before by putting circles on the 2d array.
The circles are of the same radius, and each time a circle was put, the dots inside it changed to 1 instead of 0.
All the circles are within the edges of the array and dot touching the edge of the circle is lit.
An illustration can be seen below. The circles are ordered randomly and may touch.
Notice that the dots inside the circles are 1 and all other are 0.
Can you find how many circles were there just by looking at the 2d array without the circles after I had put them? Is this problem solvable?
My attempt at solving this problem was:
First, I assumed that my circles can contain dots as in the figure (radius big enough to contain 4 to 7 dots.
Then I tried to categorize what possible orientation can the circles have, however there are just a lot.
I would like to find these two circles. Notice that they can cannot overlap but can be just one near the other.
If your circles don't overlap, you can use connected component labeling algorithm and get number of circles:
NCircles = (NComponents - 1) / 2
(if inner empty regions of circles and outer empty place form separate components)
Edit: with these dots it is worth to select only connected conponents with size in some range to exclude dots and other false regions.
Simple kind of CCL suitable for this picture:
scan image until black pixel is met
do flood fill while possible, keep bounding box of scanned black pixels
if box corresponds to circle size, count it
scan further from any unmarked pixel
One more possible approach: you can try Hough algorithm for circles of predefined radius.
For example, OpenCV library contains labeling function that works with images and arrays (and Hough transform too)
Why not just generate randomly generate circles and count them?
When you insert a new circle, just check if they do not overlap.
And stop inserting new circles after you tried a certain times and failed to insert a new circle. With this last value you probably need to play a bit.
You can probably repeat this a couple of times and average the result like that.

Separating a 2D Array into Regions bounded by a Continuous Line

I am a beginner hobbyist programmer in my first year of college. Recently I've been obsessed with the puzzle game "The Witness", for its minimalist yet surprisingly difficult puzzles. As a passion project I'm attempting to recreate just the Puzzle element of the game for others to enjoy.
THE GAME
This is How the Game Looks So Far
Essentially, you have a white path that is controlled by the user, and you must navigate that path through the grid, splitting the grid into region consisting of black and white tiles. Each region must have only white or only black tiles.
I've posted a picture of how the project looks so far, with a solved puzzle.
THE PROBLEM
I cannot for the life of me figure out a function to split the grid into regions as shown in the image. The Path is a 1D array of x and y coordinates of each point in the path. when its done, it should be at the top right corner of the grid at (cols,rows). This is assuming the lower left corner is (0,0).
Path = [[x1,y1],[x2,y2],...,[cols,rows]]
each Puzzle has n rows and n columns, so id like a function getRegions(path, cols, rows) that takes in the path, and the rows and columns, and outputs an array like this
arrayWithRegions=
[[2,3,3,2],
[2,2,2,2],
[1,1,1,2],
[1,1,1,2]]
where each square is marked as being in a distinct region based on the boundaries set by the path and the outer border. The example is how the array would look for the puzzle in the image provided. (disregard the black and white blocks, they don't matter right now)
I'd appreciate any sort of help or even a nudge in the right direction. Thanks!
You can just execute Flood fill algorithm using path line and field edges as borders.
Choose any unmarked cell (for example - left bottom one), start flood fill with region mark 1, traverse all possible cells. Then find another unmarked cell, start fill with region mark 2 and so on.
The simplest recursive implementation of FF algo and sequential search of unmarked cells should work nicely for small size of your field.

TableLayout heightPercentage: percentage of what?

I need to create a Container with a layout of two rows, the second one twice the height of the first, and put the container in the south position of a BorderLayout. I tried to do this using a TableLayout with two rows and one column, setting the height percentages to 33 and 67, respectively. However the TableLayout filled the entire screen, not just the "South" area. The javadocs indicate that the height percentage refers to the "parent", which I assumed would be the container with the TableLayout. But, apparently, the parent in question will be the display.
My next attempt was to create a TableLayout with three rows and one column, giving the lower content a vertical span of 2. But the bottom rows just disappeared.
Is there a solution I overlooked?
There are two separate stages in layout, requesting the preferred size and dealing with assigned size.
During the request stage we don't have the final size of everything so this is "guesstimated" (which amazingly my spell checker didn't flag?).
As we do the layout we already have an assigned size based on that preferred size and then the percentage is calculated based on that assigned size.
If you place a table layout container in the center of a border layout it will get all available space and so the preferred size aspect of the calculation phase will become meaningless and then percentage will be of the space allocated to the component which is "all available space". Naturally scrolling brings this into a completely different dimension.

Complex depth sorting issue based on MC location

I need help rescuing a project concerning some extreme depth management. We're out of ideas!
The project concerns stacking boxes of various sizes, each box is isometric - so thats where the depth comes in to play (we need to weave this box in front of that box, but on top of two boxes and below another, etc - anything goes!)
Here's the project as it stands:
http://clearlytrained.com/depth/
You can drag the boxes then let go over the grid if the grid space is green - if you were to take the two boxes that are one grid space high, place then next to eachother, then take a wider box (2x2) and place it over both (so the gap in the two lower boxes is in the middle of the box you placed on top) you'll see that the far right lower box's depth is now higher and above the box on top. I completley understand why this happens, but no matter how we sweep through the grid in order, this will happen to some extent the way it currently stands.
The way we're currently sorting depth, is sweeping through the grid array, which stores the name of every box on screen, based on the direction we sweep through the rows and columns we get an order to which we then set each box's depth. The problem is that even though a box might take up 6 grid spaces, we don't set the depth 6 times - we only set it the first time we come to its instance name in the array - so if we're sweeping through the grid bottom to top, left to right, a box that's physically under another box, yet further to the right will always have a higher depth. not good!
The only way i can see to fix this is to figure out some sort of box to box comparison of first X then Y values, setting up some sort of double for loop/if statemenet conditions, and more or less, programatically hand place each boxe's depth every time we move or place a new box. So the complexity goes from setting depth by order of box instance name, to comparing this box to every other box, then every other box to every other box, and coming up with the real order we need to stack their depth. In some cases we might go left to right, then bottom to top, then back to left to right - there's no smooth pattern.
Please play around with the link provided, let me know if you have any other questions or need more info, I'm desperate!
I would add all boxes to an array and sort that by both X and Y value (tile X and Y). It's not a large set, so you don't really have to worry about performance.
Sort by X, then if X is the same, sort by Y. That shoooould solve it, unless I missed something obvious.

Resources