Move node along a path segment - inkscape

Is there an easy way to move a node of a path along a straight line that contains a straight path segment? I mean, say, I have a triangle as below. I would like to move the node denoted by "A", along the line that contains segment "AB". Is this doable?

Hold down Ctrl+Alt and move the node.
https://inkscape.org/doc/keys.html#idm1882

Related

How to recover the ParserRuleContext for a specific line and character position with antlr4?

Once I have initialised the parser, lexer and obtained the translationUnit context, how can I jump directly to the (closest) ParserRuleContext that contains a specific line and character position in antlr4 (CPP runtime) ?
Usually I m using the Listener pattern to walk through the translationUnit context. In every visited context, I can obtain the corresponding line and character position of a context using the following code :
antlr4::Token* tokenclass = _tokenstream->get(myContext->getSourceInterval().a); // use ".b" if end of interval is needed
size_t CharPositionStartInLine = tokenclass->getCharPositionInLine();
size_t LineStart = tokenclass->getLine();
I would like to perform the opposite: to obtain a token from a specific line and char position, and then to obtain the (first) parent context. Is it possible ?
I think I can achieve what I want (i.e to find a context based on line and character position) by checking every line and character position of context inside the function enterEveryRule(antlr4::ParserRuleContext* context) but it seems overcomplicated. So is there an easier way to recover the ParserRuleContext for a specific line/character position ?
The approach is pretty simple. A ParserRuleContext contains start and stop tokens with positioning information. Hence it is easy to tell if a rule context includes a specific position. Start with the parse tree root and iterate over its children. Find the one that includes this position (overlap is not possible). Continue with that node and its children until you find a terminal node, which is the one you are looking for. If for a given node no child includes the given position then use that node instead.
In the MySQL Workbench Sources there's a C++ implementation for terminalFromPosition and contextFromPosition. The first function takes a line/column pair and strives to always return a terminal (even if there's none directly at the given position), while the latter uses a character index and implements the approach exactly as I mentioned in the previous paragraph.

Go back one line on a text file C++

my program reads a line from a text file using :
std::ifstream myReadFile("route.txt");
getline(myReadFile, line)
And if it finds something that i'm looking for (tag) it stores that line in a temp String. I wan't to continue this until i find some other tag, if i find an other tag i want to be able to return to the previous line in order for the program to read if again as some that other tag and do something else.
I have been looking at putback() and unget() i'm confuse on how to use them and if they might be the correct answer.
Best would be to consider a one pass algorithm, that stores in memory what it could need at the first tag without going back.
If this is not possible, you can "bookmark" the stream position and retreive it later with tellg() and seekg():
streampos oldpos = myReadFile.tellg(); // stores the position
....
myReadFile.seekg (oldpos); // get back to the position
If you read recursively embedded tags (html for example), you could even use a stack<streampos> to push and pop the positions while reading. However, be aware that performance is slowed down by such forward/backward accesses.
You mention putback() and unget(), but these are limited to one char, and seem not suited to your getline() approach.
The easiest thing by far, if you only ever want to roll back by one line, is always to keep track of the line you're on and the line before.
Maintain a cur variable that stores the current line, and prev that stores the previous one. When you move to the next line, you copy cur into prev, and read the new line into cur.
That way, you always have the previous line available.

Getting all elements in Selenium

I am very new in Selenium. I have came across a line of code which I can't understand.
allElements=sBrowser.find_elements_by_xpath(".//a[#class]")
I tried several places the answer I got is, it is finding all the anchor tag in the browser. but what does the
'.'
means before
//a[#class]
//y
will still find any node, y, located anywhere within the XML tree. But, the XPath:
.//y
will find any node, y, that is a descendant of the node x. In other words, preceding the // expression with a . tells the XML search engine to execute the search relative to the current node reference.
Referance: http://www.bennadel.com/blog/2142-using-and-expressions-in-xpath-xml-search-directives-in-coldfusion.htm

how to find the indexes of all matching substring using suffix tree?

I created a suffix tree from this amazing answer. It works like a charm!
For now, if I look for "cat" in "This cat is a pretty cat", it will return 5 as "cat" first appearance as for starting index 5.
But I can't find a way to keep track of all the suffixes in the algorithm to create. So basically, I can find the index of the first match, but not all the different occurrences.
For now, I have:
class Edge
{
int textIndexFrom;
Node* nodefrom;
Node* nodeTo;
int textIndexTo;
}
class Node
{
std::map<char,Edge*> m_childEdges;
Edge* m_pParentEdge;
Node* m_pLinkedNode;
}
I just put the relevant variables in the code above. To store the different starting positions, I imagine a std::vector is needed in Edge, but I don't see when to add a new index. We might use the active point but with the suffix links, it becomes tricky.
Could someone explain?
I assume you constructed a suffix tree for the string S$ where $ is some special character not present in S. The $ char ensures that each suffix has its own leaf in the tree. The number of occurances of word w in S is the number of leaves in the subtree of w.
I think that storing all starting positions in each edge/node would require quadratic memory. For example if T happens to be perfectly balanced binary tree then on level 0 (root) you have n entries, on level 1 you have 2 * n/2 entries and so on. After summing it gives us n^2. It requires proving so please correct me if I'm wrong.
Instead I think its better to keep all the leaves in a list in order they appear in dfs traversal of the tree (left to right if you draw a picture of the tree). And in every node v keep 2 pointers to the elements of that list. First should point to the first leaf of v's subtree and second to the last leaf of v's subtree. All that can be computed by simple dfs procedure. Now if for example 'cat' happens to be in the middle of some edge then go through that edge to some node v and get leaves from that node. In addition in every leaf you should store the length of the path from root to that leaf. It will help you find the position of that particular 'cat' occurance.
Walk the entire cat subtree. Each leaf in that subtree corresponds to a suffix that begins with cat. If you know the length of the string you've matched so far and the length of the string, each time you encounter a leaf you can do a subtraction to find the index of the corresponding occurrence of cat.

Algorithm to print to screen path(s) through a text maze

For my C++ assignment, I'm basically trying to search through a chunk of text in a text file (that's streamed to my vector vec) beginning at the second top character on the left. It's for a text maze, where my program in the end is supposed to print out the characters for a path through it.
An example of a maze would be like:
###############
Sbcde####efebyj
####hijk#m#####
#######lmi#####
###############
###############
###############
###############
###############
###############
###############
###############
###############
###############
###############
Where '#' is an unwalkable wall and you always begin on the left at the second top character. Alphabetical characters represent walkable squares. Exit(s) are ALWAYS on the right. The maze is always a 15x15 size in a maze.text file. Alphabetical characters repeat within the same maze, but not directly beside each other.
What I'm trying to do here is: if a square next to the current one has an alphabetical character, add it to the vector vec, and repeat this process until I get to the end of the maze. Eventually I am supposed to make this more complicated by printing to the screen multiple paths that exist in some mazes.
So far I have this for the algorithm itself, which I know is wrong:
void pathcheck()
{
if (isalpha(vec.at(x)) && !(find(visited.begin(), visited.end(), (vec.at(x))) != visited.end()) )
{
path.push_back(vec.at(x));
visited.push_back(vec.at(x));
pathcheck(vec.at(x++));
pathcheck(vec.at(x--));
pathcheck(vec.at(x + 16));
pathcheck(vec.at(x - 16));
}
}
visited is my vector keeping track of the visited squares.
How would I update this so it actually works, and eventually so I can manage more than one path (i.e. if there were 2 paths, the program would print to the screen both of them)? I recall being told that I may need another vector/array that keeps track of squares that I've already visited/checked, but then how would I implement that here exactly?
You're on the right track. When it comes to mazes, the typical method of solving is through either a depth-first search (the most efficient solution for finding some path) or breadth-first search (less efficient, but is guarenteed to find the optimal path). Since you seem to want to do an exhaustive search, these choices are basically interchangeable. I suggest you read up on them:
http://en.wikipedia.org/wiki/Depth-first_search
http://en.wikipedia.org/wiki/Breadth-first_search
Basically, you will need to parse your maze and represent it as a graph (where each non "#" is a node and each link is a walkable path). Then, you keep a list of partial paths (i.e. a list of nodes, in the order you visited them, for example, [S, b, c] is the partial path starting from S and ending at c). The main idea of DFS and BFS is that you have a list of partial paths, and one by one you remove items from the list, generate all possible partial paths leading from that partial path, then place them in the list and repeat. The main difference between DFS and BFS is that DFS implements this list as a stack (i.e. new items have greatest priority) and BFS uses a queue (i.e. new items have lowest priority).
So, for your maze using DFS it would work like this:
Initial node is S, so your initial path is just [S]. Push [S] into your stack ([ [S] ]).
Pop the first item (in this case, [S]).
Make a list of all possible nodes you can reach in 1 move from the current node (in your case, just b).
For each node from step 3, remove any nodes that are part of your current partial path. This will prevent loops. (i.e. for partial path [S, b], from b we can travel to c and to S, but S is already part of our partial path so returning is pointless)
If one of the nodes from step 4 is the goal node, add it to your partial path to create a completed path. Print the path.
For each node from step 4 that IS NOT the goal node, generate a new partial path and push it into the stack (i.e. for [S], we generate [S, b] and push it into the stack, which now should look like [ [S, b] ])
Repeat steps 2 through 6 until the stack is empty, meaning you have traversed every possible path from the starting node.
NOTE: in your example there are duplicate letters (for example, three "e"s). For your case, maybe make a simple "Node" class that includes a variable to hold the letter. That way each "e" will have it's own instance and the pointers will be different values letting you easily tell them apart. I don't know C++ exactly, but in pseudo code:
class Node:
method Constructor(label):
myLabel = label
links = list()
method addLink(node):
links.add(node)
You could read every character in the file and if it is not "#", create a new instance of Node for that character and add all the adjacent nodes.
EDIT: I've spent the last 3 years as a Python developer and I've gotten a bit spoiled. Look at the following code.
s = "foo"
s == "foo"
In Python, that assertion is true. "==" in Python compares the string's content. What I forgot from my days as a Java developer is that in many languages "==" compares the string's pointers. That's why in many languages like Java and C++ the assertion is false because the strings point to different parts of memory.
My point is that because this assertion is not true, you COULD forgo making a Node class and just compare the characters (using ==, NOT using strcmp()!) BUT this code could be a bit confusing to read and must be documented.
Overall, I'd use some sort of Node class just because it's fairly simple to implement and results in more readable code AND only requires parsing your maze once!
Good Luck