# A bit of a dance to get the last entry in the list, this would be unnecessary with (node, following): normal_of(unit_direction_between(node, next))įor (node, following) in iterate_pairs_from(vertices) Yield node.value, (following or first).valueĭef shape_from_vertices(vertices: DoublyLinkedList) -> Iterator: """ This covers the fact that we need to associate the first and last """ Return (to_vertex - from_vertex).normalisedĭef unit_average_vectors(vec1: Vector2D, vec2: Vector2D) -> Vector2D:ĭef iterate_pairs_from(vertices: DoublyLinkedList) -> Iterator]: Quarter rotation, but which direction this is depends on the VertexInfo:ĭef unit_direction_between(from_vertex: Vector2D, to_vertex: Vector2D) -> Vector2D: `normal_of` will be some matrix multiplication of a vector, to perform the I'm also assuming you're using an implementation of a doubly linked list that Scalar division and has a property `normalised` which returns the result of I'm assuming you have a `Vector2D` class which supports vector addition + ![]() Gone for a functional style for its brevity. If the angles to the right are greater, you are traveling anticlockwise, if it's left, clockwise.įor the purposes of pseudo-code I'm using python for its clarity and I have That you know whether it's clockwise or anticlockwise up front - if not this can be deduced by summing the angles to the left and right as you go round (not forgetting to join the first and last). The points given follow either clockwise or anticlockwise - if they are not in some order this is rather insoluble as you can't know what shape is being described, I have made some assumptions here, including that you're using an OO language, but similar patterns can be employed just as effectively in other paradigms. We can then for each point in our input create a pair of this point, and the sum of the dictionary entries keyed by this and its two neighbours (which is why doubly-linked), which is a vector along the normal, which we then divide by its magnitude to make this a unit vector too. Having vectors immutable allows for good behaviour using their references/pointers as keys allowing for efficient lookup later. This constructor can then hold a dictionary/hashmap of vector pair to vector - this is constructed from our rotated unit tangential vectors keyed by the two vertices which form the described edge. These are standard enough data-structures that I'm not going to provide pseudo-code to implement them. I would be tempted to create another data structure for our shape, constructed with a doubly-linked-list of vectors, (doubly-linked for reasons which should become clear later) which exposes a same-length sequence of pairs of vectors to represent a point and its normal. I would propose that we will obviously want something to represent a 2d vector, and it makes sense for this to be immutable for reasons I'll describe later. Looking at the operations we need to perform leads us towards a data-structure. What we have now is a sequence of unit vectors at a normal to each vertex, which we can add or subtract in order to grow or shrink our shape. If we take these tangential vectors we can rotate them by a quarter-turn in order to get vectors in the normal direction to each edge (whether you go clockwise or anticlockwise depends on which way around the shape your points go), and the sum + normalise each pair, again including the first and last. So now we have two same-length sequences of vectors, one describing nodes, the other the direction along each edge. We're only interested in the direction of this vector rather than the size, so we may as well make these unit vectors by dividing them by their magnitude. Next we calculate the vector between each pair of points, including the first and last. Given a list of numbers (I'm going to presume we're working with doubles or decimals) we need to transform this into a sequence of 2D vectors which describe a path along the perimeter of a polygon either clockwise or anticlockwise (which we can know ahead of time) If I understand the question correctly we have the following:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |