An "elbow connector" in MS-Word is a 3-segment line with a control point in the middle as shown
where if I move the yellow control point sideways, then the length of the two lines on either side change accordingly while the end points remain the same. (Please ignore the "2" in the picture)
I am trying to understand how this works so that I can re-create this. Is there a "line equation" for such a line? I have some points (x,y) that are already in the shape of this elbow connector but I would like to incorporate the functionality of changing the lines on either side by controlling the control point. How would I go about re-creating this?
By dissecting the lines like:
For moving the center(M) only sideways, length of lines 2 and 3 remains the same so the problem becomes how to calculate length (and direction) of the lines 1 and 4.
That can be calculated like:
line1_length = B.x - M.x;
line4_length = M.x - A.x;
For directions a comparison should first be made like:
if(B.x > M.x)...
.
.
.
if(M.x > A.x)...
.
.
.
Beginning points is already known as the position of A and the position of B. By knowing the lengths and directions of lines 1 and 4, the end points can be determined.
Good luck!
Related
One can enter the matrix from any cell, and each cell contains some clue regarding the position of the cell needed to be found like
Total 8 types of clues can be present:
Exactly Above it
Exactly Below it
Exactly To the Right
Exactly To the Left
In the Top Right Region
In the Bottom Right Region
In the Top Left Region
In the Bottom Left Region
In each step he can move to one adjacent cell as per the above eight movements.
My question is minimum no. of steps he needs to take to reach that cell in the worst case.
Test Case :
2 2 : 1
3 3 : 1
My solution looked like this :
I thought If I start from the middle region of the matrix , then I would have to take the minimum no. of steps.
cin >> n >> m ;
ans = max ( n/2 , m/2 ) ;
But obviously it was wrong.
The correct solution looked like this
cin >> n >> m ;
ans = log( max(n,m) , 2) ; //base 2
Can anyone explain this ?
The question can be found here : http://www.codechef.com/TRNT2014/problems/TR001
These two items:
In each step he can move to one adjacent cell as per the above eight movements.
The correct solution looked like this:
ans = log( max(n,m) , 2) ; //base 2
are in contradiction. If the first is true, then your solution is correct. If the second is true, then the first must be false, and he can jump to any cell (or at least a cell (n/4,m/4) away).
Contrary to the description above, the linked description does not seem to limit which box you can check at any time.
As such, the correct approach seems to be basically a 2D binary search. Start at the middle box. If (for example) the clue says the correct box is up and to the right, move half the distance toward that corner. At that point, it might say (for example) that the correct box is up and to the left. If so, you move half the distance that direction.
In short, it's a binary search, except that the result at each point is a vector--a direction and a distance--rather than just a distance as you'd have in a typical 1D binary search.
hell-o guys!
well, I'm playing with random walks. Midpoint displacement gives some nice results, but I would like a random walk without walk loops, like the ones (in yellow) on this screen-hot :
My first idea to deal with that problem is to check for each segment if there is an intersection with all others segments, then to delete the walk loop between the both segments and bind at the interesection point. But for some walks, it would give a strange result, like that one :
where the yellow part is a loop, and we can see that a big part of the walk would be deleted if I do what I said.
Maybe another method would be to check, when the displacement of the midpoint is made, if the segments are interesecting. In case of there is an intersection, get another displacment. But it looks to become very time consuming quickly when the number of subdivisions rises...
So I would like to know if there is a way to avoid these loops
so... it's seems playing with the amplitudes of the random numbers is a good way to avoid overlaps :
the path without displacement is drawn in cyan. I didn't get overlaps with these displacments :
do{
dx = (D>0)? 0.5*sqrt((double)(rand()%D)) - sqrt((double)D)/2. : 0 ;
dz = (D>0)? 0.5*sqrt((double)(rand()%D)) - sqrt((double)D)/2. : 0 ;
}while(dx*dx+dz*dz>D);
where D is the squared distance between the two neibourers of the point we want to displace. The (D>0)? is needed to avoid some Floating Point Exception.
Ok so I have a 2d vector of chars that I call a grid. Lets just say its 70 x 30. When the grid is created, it automatically fills each position with 'x'
I have a function that displays the grid. So I call this function and a 70x30 grid of x's is displayed to the console.
I have another function that I want to call to essentially replace the char at certain x,y coordinates of the grid with a different char. The points aren't exactly random/scattered. I'm basically starting from a point on the edge of the grid, and drawing zigzagged lines to another edge. All points are predetermined. Theres a lot of points to plot, so manually doing it seems inefficient.
Here's how I was thinking to do it:
Create a double for loop, width and height, calling them i and j
If i = (a || b || c || d...) && j = (e || f || g..)
And essentially do that tedious process for each possible scenario..
Surely there is a much easier and simpler way lol. Any suggestions will be greatly appreciated. Thanks!
If the points can be pre-determined by having a map (as in for a level editor or otherwised fixed pattern), then make a dictionary of x/y co-ordinates to what the tile becomes. Iterate over the dictionary and do each replacement.
If the points aren't pre-determined but follow a pattern, such as lines or blobs, then write a method that draws the line/blob/whatever and call it over and over. The method decides which tiles to replace and replaces those.
Btw, there's a trick when doing 2D checking and processing like this which is called having a 'delta', for instance xdelta=-1, ydelta=0 is west and xdelta=1, ydelta=1 is northeast. By having a delta you can run a function two, four or eight times with different deltas and it'll move in different directions by just using the delta's directions instead of needing to try all eight directions on its own - the delta can also be used to drive the bounds checking if you want, as you can't go out of bounds in a direction you're not proceeding in for example. Then you can go further and have an enumeration of all directions, functions that invert a direction, that turn a direction 90/45 degrees (if it's enumerated it's very simple, you just add 2 or 1 to the enumeration and return the new direction), etc, making processing very easy now.
So I might have something like
function drawLine(int xstart, int ystart, int xdelta, intydelta)
that starts at xstart,ystart, replaces the tile with O, adds xdelta to x, adds ydelta to y, replaces the tile with O, etc until it falls off the edge.
I'm having trouble with my homework assignment(C++). I'm not asking for the complete solution, but tilt in the right direction could be helpful. :)
I have a NxN board (max N = 100) and a 1x2 figure (cube) on that board. The cube is painted red on the one side and blue on the other. Default position for the cube is left upper angle of the board, blue side up:
B B . .
. . . .
. . . .
. . . .
(4x4 example, B stands for blue)
There could be stones (obstacles) on the blackboard.
Moves I can make with my figure:
rotating it for 90/180/270 degrees clockwise
you can flip the cube around its right/left/upper/lower edge, changing its ''upward color''
For an example, using flip right on the default position:
. . R R
. . . .
. . . .
. . . .
and then using rotate 90:
. . R .
. . R .
. . . .
. . . .
and then using flip left:
. B . .
. B . .
. . . .
. . . .
Of course, when rotating or flipping, you cannot land on the stone.
So, the problem is - for any given configuration of the board (figure position and stone positions) write a program that will ''bring the cube home'' in the default position (blue side upwards!) using minimal number of moves and return 1 if that's possible or return 0 if that's impossible.
I find this problem interesting, but I have to admit I'm slightly confused with it. Especially the blue side/red side part. I can't really figure out how to ''translate'' those moves I can use in the language of usual shortest path algorithms (and I never used any of these).
So, I would be grateful for every piece of advice you can give! :)
First, since you are asked to find the exact optimal path, I would go with Dijksta's algorithm.
For this algorithm, you'll need:
A function which gives the next
possible move.
A function which tell you if a position was already
visited.
A function which tell you the total cost of each path.
And of course a function which tell you when you've reached the final
position
Given an initial position, your cube can reach exactly 7 new positions. It's easy to pick which ones are possible.
G is simply the number of moves you've made so far + 1 for the next move :)
I would use a hashtable to keep track of the visited position. (This is probably the most difficult function to write), but you don't need to over think it right now. A simple vector and a term-by-term comparison would do. You can optimize this once your code is running.
And finally, you need to check if the cube is in its initial position blue side upwards.
You can interpret each possible 1x2 block and a color (red or blue) combination as a vertex and moves as edges. If it is possible to reach a particular 1x2 block and color combination (vertex) from some other combination in one move then there is a connection (edge) between those two combinations. Then you have to find shortest path between the given configuration and "home" configuration in the resulting graph (probably Breadth-first search since cost of the move is the same no matter what move you perform).
And if want to go further you can use advanced graph search algorithms that use heuristics during the graph traversal (heuristic being the minimum amount of moves needed to reach destination assuming there are no obstacles on the blackboard). For example you can use A* algorithm.
When dealing with this kind of problems, the first thing to do is to find a representation of the state of your problem.
In this case, you need:
Two integers which represents the top-left position of your figure
One boolean which represents the color of the figure (red/blue)
One boolean which represents the orientation of the figure (horizontal/vertical)
If you are familiar with bitmasks, you should use just a 32 bit integer to do this (8 bits for x position, 8 bits for y position, 2 bits for the rest).
In this way you don't need to implement a comparison operator.
OR
You define a simple struct (call it state) with these 3 information and a strict-ordering comparison on this (this is only needed to put state in std::set.
After this, you can solve this problem using a BFS.
To do that, you need:
An std::map<state,state> to store the position you already visited in the key, and the position you came from in the value (replace map with unordered_map if you can use c++11 and you use a bitmask to store your state)
A std::queue<state> in which push and pop up the states to be processed.
Some code to determine every possible state reachable from a given one (i.e. implements all the possible moves, taking care of the board dimension)
Pseudo code:
map<state,state> visited;
queue<state> to_be_processed;
visited.insert( initial_state,initial_state); //you are not coming from anywhere
to_be_processed.push ( initial_state);
while(!to_be_processed.empty()) {
state cur = to_be_processed.pop();
if ( cur == end_state) //you are done
{
//to get the path from initial_state to end_state you have just to walk visited in the inverse order.
return 1;
}
for ( i = every possible state reachable from cur) {
if (visited.count(i) != 0) continue; //already visited
to_be_processed.push(i);
visited.insert(i,cur); //i has been visited, and you reached i from cur
}
}
return 0; //if you get here, no way
The presence of obstacles make just the problem more difficult to code, but is no conceptually different.
Note that in this case BFS works because the cost you pay for going from one state to another is always the same.
Firstly I would like to accept that it is a homework question , but then I know how to code AB-pruning from the algorithm of it . The problem is how to apply it on a grid of numbers where the game can go on in any direction (right , left , up and down ) , thus how will be the tree formed .
Sorry for being a bit vague here , if more info is required then do inquire , I will provide it .
You question is very vague so I can only guess what you are asking:
Are you talking about a game where the player can only move in one of those 4 directions on each turn? If that is the case, your Node will be an (x, y) position of your player on the grid, and each node will branch 4 times (once for each direction) plus maybe 1 if you can stay still.
You say you already know how to code AB-pruning, so is that enough?