Flipping QuadTree in recursive abstract code - shapes

I'm trying to flip a quad tree about the vertical axis recursively, but without using a particular PL. In which case I've written the following, but I'm 100% it's not actually good, and I can't quite sure I understand it as well.
flip(quadtree) {
if (singleNode)
return quadtree
else return formQuadTree(flip(NW(quadtree)), flip(NE(quadtree)), flip(SW(quadtree)), flip(SE(quadtree)))
Any suggestions?

Replace:
formQuadTree(flip(NW(quadtree)),flip(NE(quadtree)),flip(SW(quadtree)),flip(SE(quadtree))).
With:
formQuadTree(flip(NE(quadtree)),flip(NW(quadtree)),flip(SE(quadtree)),flip(SW(quadtree))).
What exactly don't you understand?
formQuadTree( topLeft, topRight, bottomLeft, bottomRight ) - forms your quad tree. Every time you enter recursively into flip( quadTree ) you go deeper and deeper inside the inductive step and return the base case, flipping each node that has 4 children quadTrees.
So once all your recursive calls get to return quadtree, all your inductive nodes have been arranged correspondingly.

Related

What's the fastest way to find if a point is in one of many rectangles?

So basically im doing this for my minecraft spigot plugin (java). I know there are already some land claim plugins but i would like to make my own.
For this claim plugin i'd like to know how to get if a point (minecraft block) is inside a region (rectangle). i know how to check if a point is inside a rectangle, the main problem is how to check as quickly as possible when there are like lets say 10.000 rectangles.
What would be the most efficient way to check 10.000 or even 100.000 without having to manually loop through all of them and check every single rectangle?
Is there a way to add a logical test when the rectangles get generated in a way that checks if they hold that point? In that case you could set a boolean to true if they contain that point when generated, and then when checking for that minecraft block the region (rectangle) replies with true or false.
This way you run the loops or checks when generating the rectangles, but when running the game the replies should happen very fast, just check if true or false for bool ContainsPoint.
If your rectangles are uniformly placed neighbors of each other in a big rectangle, then finding which rectangle contains point is easy:
width = (maxX-minX)/num_rectangles_x;
height = same but for y
idx = floor( (x - minX)/width );
idy = floor( (y - minY)/height );
id_square = idx + idy*num_rectangles_x;
If your rectangles are randomly placed, then you should use a spatial acceleration structure like octree. Then check if point is in root, then check if point is in one of its nodes, repeat until you find a leaf that includes the point. 10000 tests per 10milliseconds should be reachable on cpu. 1 million tests per 10ms should be ok for a gpu. But you may need to implement a sparse version of the octree and a space filling curve order for leaf nodes to have better caching, to reach those performance levels.

Constraining movement of a node to a single axis

Using Cytoscape.js, how can I constrain movement of a node to a single (i.e. either the x or y) axis? I'd like to be able to make it so a node can only be dragged vertically or horizontally, but not both. In other words, I'd like to lock a node, but only on a single axis. I'm not sure if this is possible, and wasn't able to find anything in the documentation that mentioned this specifically, so I figured I'd ask.
Thanks in advance!
Use the automove extension, which lets you set whatever restrictions on node positioning that you like. Constraining the x value is as easy as passing a (x, y) => { return { xConst, y }; } function to the extension.
I ended up coming up with a way to approximate the result that I wanted by listening for each node's free event, and setting its position to what I needed it to be. This doesn't restrict dragging of nodes to a single axis, but it does restrict dropping of nodes to a single axis, if that makes sense.
Here is the Cytoscape.js description of the free event:
free : when an element is freed (i.e. let go from being grabbed)
from: http://js.cytoscape.org/#events/collection-events

Check if point is within a polygon

Given a GEO-JSON polygon, such as the below:
[
[15.520376, 38.231155],
[15.160243, 37.444046],
[15.309898, 37.134219],
[15.099988, 36.619987],
[14.335229, 36.996631],
[13.826733, 37.104531],
[12.431004, 37.61295],
[12.570944, 38.126381],
[13.741156, 38.034966],
[14.761249, 38.143874],
[15.520376, 38.231155]
]
How can I check if a GPS location is within the polygon region?
For example, if the user is at Lat 37.387617, Long 14.458008, how would I go about searching the array?
I don't need someone to necessarily write the code for me, I just don't understand the logic of how I can check. If you have any example (any language) please point me.
This task is called point in polygon test.
Gerve has explained the algorithm that is widley used for this task. But this will not help you in implementing it. There are foot traps, like parallel lines.
One of that algorithms is called Crossings Multiply test, which is an optimized variant.
Source code: CrossingsMultiplyTest (last function in the file)
An Overview is given in "Point in Polygon Strategies"
Use longitude for the x coordinate, and latitude for the y coordinate.
I've found an article about the Ray-casting algorithm. It's explained pretty well here, the jist of it is (in pseudo code):
count ← 0
foreach side in polygon:
if ray_intersects_segment(P,side) then
count ← count + 1
if is_odd(count) then
return inside
else
return outside

creating table border in unity3d

I am working on a pool game of sorts. To create a table, I have used cubes as sides. I want to use the inbuilt physics engine to get those sides interact with the balls. Sadly I am unable to get it working.
Here is what I have done. I created cube as side, and a sphere for ball. To move the sphere, I am using rigidbody.MovePosition function. both the cube and sphere have colliders and rigidbody attached, and gravity turned off.
Sphere movement is fine, but when it collides with cube, it makes the cube fly. Since the cube is supposed to be an immovable wall, I constrained all axes rotation and movement. But, using constraints cause physics engine to go bonkers. Instead of sphere coming to stop or moving in opposite direction, it simply passes through the cube. Clearly, something is wrong, and I need help figuring out the same.
Thanks in advance.
Here is the code used to move the sphere.
public float movePower = 10.0f;
// Update is called once per frame
void Update ()
{
if(Input.GetKey(KeyCode.LeftArrow))
{
rigidbody.MovePosition(transform.position + Vector3.left* movePower * Time.deltaTime);
}
if(Input.GetKey(KeyCode.RightArrow))
{
rigidbody.MovePosition(transform.position + Vector3.right* movePower * Time.deltaTime);
}
if(Input.GetKey(KeyCode.DownArrow))
{
rigidbody.MovePosition(transform.position + Vector3.down* movePower * Time.deltaTime);
}
if(Input.GetKey(KeyCode.UpArrow))
{
rigidbody.MovePosition(transform.position + Vector3.up* movePower * Time.deltaTime);
}
}
The simplest way is to remove the Rigidbody from all cubes as they are supposed to be fixed. Another way is to mark the cubes' Rigidbody components as Kinematic, but this meant to be used for moving objects like player characters that should participate in physics but should not be moved by the engine.
I recommend reading the Unity Physics man page.
Update:
More things to consider:
Don't check Is Trigger
Check that your Layer Collision Matrix is set up right (menu Edit/Project Settings/Physics)
If the velocity is pretty high, physics engine might get confused and collision are not detected
Ensure that the models are not scaled down or up extremely (best is to have scale = 1)
The best mass for Rigidbody.mass is 1
Be careful when playing with PhysicsManager settings like Min Penetration For Penalty or Solver Iteration Count
Use gravity if possible
Never move by manipulating Transform directly. Use Rigidbody methods instead
Avoid calling rigidbody.MovePosition on every update if it's a constant linear motion. Do it once and leave it untouched
Remember to use FixedUpdate for calling rigidbody.MovePosition etc.

How to track the depth in this object graph depth-first search algorithm?

I have this code which iterates over a tree, doing a depth-first search. Every element is tackled exactly once. Very good.
-(void)iterateOverTree:(TreeNode *)node
{
NSMutableArray * elements = [NSMutableArray array];
[elements addObject:node];
while([elements count])
{
TreeNode * current = [elements objectAtIndex:0];
[self doStuffWithNode:current];
for(TreeNode * child in current.children)
{
[elements addObject:child];
}
[elements removeLastObject];
}
}
BUT: How can I keep track of the current depth in the graph? I need to know the level of depth. So for example I have these nodes:
A has childs B, J.
B has child C.
C has child D.
D has childs E, F, I.
When A is at depth level 1, then B is 2 and C is 3.
With recursion it was very easy to keep track of the current depth level. Just inrement a variable before calling itself and decrement it after calling itself.
But here with this fancy while loop that is not possible. There is no box in the box in the box happening like with recursion.
I don't really want to have to add properties (or instance variables) to the TreeNode object as this should be re-usable in a generic way for any kind of object graph.
Does anyone have an idea how to do this? Must I introduce another array to keep track of visited nodes?
I think you do need to stack the depths as well. This is what you would actually have done anyway, if you had a recursive version. It's just that the storage would be “invisible”, since you would have used the call stack instead of an explicit stack like you are doing now.
If it helps you, you could easily convert the depth-first search to a breadth-first search, by using the array as a queue instead of a stack. (Just do removeFirstObject instead of removeLastObject.) Then you would know that you always look at the nodes in order of non-decreasing depth. However, if you need exact depths, I think you still need to add some storage for keeping track of when you have to increment the current depth.
Update:
You should be able to do a DFS without the stack altogether, if instead you follow parent pointers of the node to back up the tree. That would make maintaining the depth simple. But you would have to be careful not to break linear-time worst-case complexity by rescanning children to find out where you were.
If you don't have parent pointers, it should be possible to stack just enough information to keep track of the parents. But this would mean that you put some more information on the stack than you are currently doing, so it would not be much of an improvement over stacking the depths directly.
By the way, looking carefully on your algorithm, aren't you looking at the wrong side of the array when you get the next current node? It should work like this:
push root
while stack not empty:
current = pop
push all children of current
Im not understanding your notation, but if I read correctly you process a node and add all children to your list of work to do.
If you could change that part to using a recursion, you could track the tree-depth since it would be the recursion depth.
So instead of adding the child node, recurse for each child node.
hth
Mario
I believe what you are doing actually is BFS. You are working with lists. For doing DFS, you should use a stack;
This might be useful for the depth track, you might look into the vector p (of parents)
Supposing you are doing BFS, the easiest thing to do is to introduce another queue for depth that mirrors your nodes queue. Initialize the depth to zero. Each time you push to the node queue, push the current depth + 1 to the depth queue.