Does anyone know what kind of overhead CGRectContainsRect and CGRectIntersectsRect have?
I am trying to track and test a user gesture and am finding I am probably going to have to use a combination of both but not sure of the impact.
Imagine a grid of 5 squares and you want to test if the user drew a line from square 1 to 2.
Easy, CGRectContainsRect lets me know if the did draw only in square 1 and 2.
If they drew in say squares 1, 2 and 3 the same test would return false as CGRectContainsRect would say NO.
OK no lets say you want to test for drawing in squares 1,2 and 3 and the user only draws in square 1 and 2, CGRectContainsRect will return YES because rightly so the users stroke is within squares 1,2 and 3........but not actually in square 3 so a false reading is obtained!
You could iterate thru each of the 5 squares, checking CGRectIntersectsRect on each. For a larger array of squares this may become highly inefficient, but off the top of my head thats the best way I can think of.
Related
I'm making a game, where levels are grid based.
At first, I have rectangle MxM cells. Then within this rectangle I have to put rooms. Room is another rectangle which is AxB cells, where 2 <= A, B <= 4. Besides, It's necessary to put N rooms in each row and N ones in each column. They should fill all the space, there can't be empty space between them. In other words, I have to feel rectangle with other rectangles the way that there will no be empty space between them and they will form a grid with N rows and N columns.
What I did:
I store information about rooms in form of their left-top corner, calculate it and then put rooms based on their and neighbor's corners. To do that:
Divide grid on rooms 3x3
In each of 3x3 rooms define area which is obligatory floor (2x2 square, let's call it red area)
In loop for each room count it's neighbor x and y corner position the way that it doesn't cross none of the obligatory floor ares. For that:
a. Get red area of current room and it's neighbors. Set corner somewhere between them, making sure the dimensions of the room are within range above.
b. Resolve collisions, when it's not possible to set random corner. For instance, if x position of room above isn't equal to our room, then we can't put horizontal wall between to rooms righter them in random y position, because in that case these rooms will overlap each other.
Some other stuff with converting information about corners to rooms themselves
So, what's the problem? My code with a lot of if-statements and crutches became so unreadable and huge that it almost impossible to test and find bugs. Approach I used seems to work but it's impossible to control the way it's working or not working.
Another issue is that I want to have more control on how it looks like. Grid must be interesting, which means that neighbor rooms are preferably not of the same size. There's an example (grid) of such a grid (with red areas that are gray there), which is not bad.
Is there some alternative to solve this? In other questions I saw a lot of similar solutions, but all of them doesn't assume that there's fixed amount of rows and columns.
Recommend me some articles I haven't managed to find, probably, literature devoted to this topic, or point the direction where to move and find a working solution.
A traditional method of generating grids containing rooms is to use Binary-Space-Partition trees.
One thing about that method is that it often produces grids that are less densely populated than your example. You might be able to modify some BSP example code and make the map more dense though.
Another possible approach would be to generate the rectangles first, (perhaps with a border along two edges for the gap) then try to pack them using a rectangle packing algorithm. This previous answer has several potential packing algorithms.
I am currently in High School, and I am in an APCSP (AP Computer Science Principles) class, which in my case is learning in Scratch programming. I am confused and have practically no idea what I'm doing. Scratch is very confusing and I feel like it's pointless to learn.
My question is this: Can anyone help me on how to make a Maze Generator on Scratch, as this is my project and it's giving me struggles.
Thank you.
It's actually possible to build with scratch but depends on what you are looking for. I assume you want to generate a simple maze like in old fashioned 8-bit games like boulder dash.
First decide on the size of your maze: for example 5 x 5 blocks.
If you want to create a maze, imagine drawing it on a grid on paper. Blocks are either "empty" or filled in. Our maze can be represented by numbers. The empty blocks are represented by a 0 and the filled blocks with a 1.
You could visualize that matrix like this if all blocks are empty:
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0
Adding a border wall while keeping the inside empty would look like:
1,1,1,1,1,
1,0,0,0,1,
1,0,0,0,1,
1,0,0,0,1,
1,1,1,1,1
Using a "list" variable to store this information would fit best within the possibilities of MIT Scratch.
In this case, you need to understand that each block in our maze is represented by a position in above matrix. You could draw numbers on a piece of paper in the shape and size of your grid / matrix as a reference to remember the position of each block if that makes it easier.
We also need to look at how our maze will relate to the Stage size. The width and height in pixels of a default scratch project is 480x360.
A 5 x 5 maze is divided in blocks of 480 / 5 = 96 width and 360 / 5 = 72 height. In other words, a block needs to be 96x72 pixels, based on a full screen maze.
Next step, is creating a sprite representing the visualization of the blocks of the maze. I would keep the first "costume" of our block sprite empty, and create a fully filled block to represent the walls of the maze.
After that, we need to programmatically create our maze. I made an example you can explore of random drawing of the blocks of a maze:
https://scratch.mit.edu/projects/278731659/
(You can change the rows & columns value to see it scale up, but remember the limit to the amount of clones the block sprite can have is 300)
This is just to get you started and by no means a complete solution. I just hope this helps you think in the right direction.
You can make this more advanced, by adding a function to explore and correct our randomly drawn grid to generate a walkable path from position x to position y. A rule you can program is for example: Every empty position in the grid should have at least two other empty positions in the spaces above, below, left and right of it.
There are many different ways to do this; whether this is with sprites and stamp or 2D lists and pen. Either way, the main component is the algorithm. This wikipedia page gives details on how maze generation works and few different algorithms. There is also a video series by The Coding Train here where he creates a maze generator with the 2D list method from above (this method is a bit harder on scratch, however). Either way, the best thing to do is to look at examples others have made, figure out how they work, and try to recreate them or make them better. Here's a good place to get started.
Scratch IS truly pointless! A simple maze generator would have you use the pen to draw predefined shapes (Such as a long hallway or intersection). You should also make (invisible) squares to separate everything and have the program draw in the squares.
I will put a link later that leads to a sample project that has the code.
Check out this video by griffpatch
https://www.youtube.com/watch?v=22Dpi5e9uz8
This was one of my projects, and the instructor provided this video for everyone to follow and expand from.
i am trying to erode a small shape, since it's a region on an MR images i can't find the suitble struct element disk doesn't work since the object is very small square doesn't give a good result,
the image :
thanks in advance
It's really difficult when object are really small. At this scale, you don't have many possibilities:
square 3x3 (8-neighborhood, the biggest)
disk 3x3 which is then just a simple cross (4-neighborhood)
segments 3x3 with for possible orientations, but then you impose an orientation and you will get artifacts.
There is a last possibility that is more complicated to develop: an hexagon. You have to use tow different masks for the implementations, according to the row/line you process. It will give you a result between the cross and the square.
Btw, why do you want to achieve, maybe there an other solution than the erosion.
I have a binary image, I want to detect discontinuous lines and linking them.
I don't know anything (coordinates, angle etc).
Can any one guide me how should I start? Suppose I have this image:
I want to join discontinuous lines. And I want to store information of lines joining (in an array) to use afterward.
I found your problem interesting and I will try to give you just some ideas but unfortunately not a complete algorithm (you know, it takes time...). I will also leave you with some unanswered questions.
I consider the image you posted as a binary image, that is the black pixels have value of zero and the white pixels have value of one. I ignore the red pixels because I think you drew them in order to highlight where you would like to connect the broken lines; ignoring the red pixels means I will set their value to zero.
First of all we need some definitions.
A non-border pixel has 8 neighbor (north-west, north, north-east, east, south-east, south, south-west, west) pixels:
abc
h*d
gfe
in the above diagram the pixel is indicated by * and its 8 neighbor by a,b,c,d,e,f,g and h.
I define an endpoint pixel as a pixel with value of one and just one neighbor with value of one, the remaining neighbor have a value of zero, so for example this diagram shows an endpoint pixel
000
011
000
because d=1 and all the remaining neighbors are zero.
The following diagram shows instead a pixel which is not and endpoint pixel because it has two neighbors equal to one (a=1 and e=1)
100
010
001
Now we can start to describe a part of a simple algorithm.
In the first step find all the endpoint pixels and put them in a vector: in the following image I marked the endpoints from 1 to 15 (note that the endpoint 15 was not highlighted in the image you posted).
In the second step, for each endpoint, find its closest endpoint: for example consider the endpoint 4, its closest endpoint is 5. Now, if you follow the simple rule of connecting one endpoint with its closest endpoint you will have segments connecting 4-5, 10-11, 13-14, which are all fine. But consider 1: its closest endpoint is 2 or maybe it is 3, but I would like that the algorithm just connected 2 and 3 while connecting 1 to the leftmost vertical line. I would also like the same behavior for 6, 9 and 12.
Now a different situation: what about 6, 7 and 8? Ignore for a moment 8, the closest endpoint of 6 is then 7 but they are already connected, how we can manage this case?
And, last, consider 15: why did not highlight it in the image you posted? Maybe it should be ignored?
May be this would help.
Increase the thickness of the lines to at-least 2.
Find those runs of consecutive foreground pixel in vertical direction in the image whose previous or next column has all background pixel within the run-length. This would give the location of the points, which needs to be processed.
For each point find the nearest steep point. This would be either a branch point or a point where an abrupt change in angle has occured. (Example point 15 in the image of previous answer). If there is no such point there is obviously another end-point. Call these as reference point. The vector between an endpoint and reference point would give the direction of extension.
Now there can be various ways to decide which point to join with. You can take the nearest foreground point in that direction. You can also pick some features depending on the angle and distance between an endpoint and a point of extension to use in a KNN classifier.
Hope it helps.
I have some data points which I have devided into them into some clusters with some clustering algorithms as the picture below:(it might takes some time for the image to appear)
alt text http://www.freeimagehosting.net/uploads/05a807bc42.png
Each color represents different cluster. I have to draw polygons around each cluster. I use convhull for this reason. But as you can see the polygon for the red cluster is very big and covers a lot of areas, which is not the one I am looking for. I need to draw lines(ploygons) exactly around my data sets. For example in the picture above I want a polygon that is drawn exactly the same(and around) as the red cluster with the 3 branches. In other words, in this case I need a polygon with 3 branches to cover my red clusters not that big polygon that covers the whole area. Can anyone help me with this?
Please Note that the solution should be general, because the clusters will change in each run of the algorithm, so it needs to be in a way that is general.
I am not sure this is a fully specified question. I see this variants on this question come up quite often.
Why this can not really be answered here: Imagine six points, three in an equilateral triangle with another three in an equilateral triangle inside it in the same orientation.
What is the correct hull around this? Is it just the convex hull? Is it the inner triangle with three line spurs coming out from it? Does it matter what the relative sizes of the triangles are? Should you have to specify that parameter then?
If your clusters are very compact, you could try the following:
Create a grid, say with a spacing of 0.1.
Set every pixel in the grid to 1 if there's at least one data point covering it, set the pixel to 0 if there is no data point covering the pixel.
You may need to run imclose on your mask in order to fill little holes inside that have not been colored due to sheer bad luck.
Extract the border pixels using, e.g. bwperim. This is the outline of the polygon you're looking for.