Given 2 points how do I draw a line at a right angle to the line formed by the two points? - geometry

Idealy I want to supply a sequence of points and have a line drawn at a right angle at every point (starting at the second point).
The direction of each line would alternate, so if I happened to draw a curve cosisting of 6 points, a line of a given lenth would be drawn for each point starting with the second point, i.e 5 additional lines on alternating sides of the curve, a bit like a caterpillar with alternating legs.
(I understand that the lines won't be entirely at right angles to the curve but rather at right angle to the line formed by any two points on the curve).

It's a question of vector mathematics. You can calculate the directing vector between two points A and B by subtracting A from B. In 2D and only in 2D the vector right angled to this vector can be obtained by reversing x and y component and taking one component negative. If you negate the new x component you'll make a left turn, by negating y you'll make a right turn. You can then reduce the directing vector to unit size (= of length 1) by dividing each component by the length of the vector (sqrt(xx + yy)). Finally you can stretch the unit vector again by your desired length and have one of the size you want. If you add this vector to either A or B you'll get a point to which you want to draw your line.
Here's a little math help:
These are points A and B expressed as vector.
The directing vector is calculated by a simple subtraction.
The normal vector is given by flipping the directing vector, that is to reverse the components and make one component negative. nl = normal, flipped to the left, nr = normal, flipped to the right
The unit vector of the normal vector is given by dividing each component by the length of the vector.
Calculates the length of a vector
If you want to draw a line from B to the left (when coming from A) you calculate the point P to draw the line to as
So you want to alternate that one time you draw to the left and one time to the right when iterating over the points.
If you have points lying outside your canvas, then you length is probably too large. You can of course calculate the point at which the vector to P would cross the boundary by calculating the intersection point of the vector BP and the border.

Related

Given two curves(both formed of certain points),firgure out point count of one curve over another

As the pic show, both curves are of spline line, and have limited points. I want to figure out the count of green points. Is there any idea?
I assume the black curve is x-monotone (otherwise the "one above the other" term can be ambiguous).
A simple approach is to consider the black curve as a polygonal line and for each point p on the red curve find the point q on the polygonal line with the same x-coordinate. Then the green points are those p that have a larger y-coordinate than their corresponding q.
Finding the point q corresponding to a given p amounts to going over the segments of the polygonal line and identifying segments that have one endpoint with smaller x-coordinate and the other with larger. Once you have such a segment the y-value of q is just a linear interpolation.
Since the polygonal line is x-monotone, the x-coordinates of the points are sorted. Therefore, the search for the corresponding segments can be done efficiently using logarithmic binary-search.

What does the mathematical notation at SVG arc implementation notes F.6.5.4 --> F.6.5.6 mean?

https://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes
I'm thinking they're a cute notation for arctan2, but I'm not sure. But then reading the notes below the equations just makes me more confused.
F.6.5.6 is using a triple bar and not an equal sign. The first part of the equation is Theta 1 and the second part is Theta 2, and then the arc that sweeps between them is the delta Theta (I suppose that just how you do it with a matrix, comma, parenthesis, angle sign, triple bar notation -- but I couldn't find other examples).
Theta1 (F.6.5.5) and Theta2 (F.6.5.6 left hand side) are calculated with atan2.
In the notation used in F.6.5.4
the half arrows (some people use full arrows here) over u and v indicate that these are vectors, ie directional quantities. These are usually expressed as a list of real numbers indicating a movement along each of the principle directions, for example in the 2-dimensional cartesian plane, we may have a vector (1,1) which indicates movement up by one and right by one (movement at a 45 degree angle).
On the left of the equation, the strange little symbol indicates an angle, so that left hand side reads the angle between the vectors u and v.
On the right hand side, the dot indicates a dot product (the sum of the products of corresponding entries in the vectors), and the double-bar notation indicates the length of the vector (the square root of the dot product of the vector with itself - in 2 dimensions, this should remind you of the Pythagorean Theorem).
This is a standard formula intimately familiar to anybody who has studied linear algebra. Here is a wikihow article demonstrating the formula where you will see the same notation in use.
F.6.5.5 and F.6.5.6
are applying this formula using the vectors created from the given variables at the beginning of the section. The first formula defines θ1 as the angle between the first chosen vector and the right x-axis (the vector representing a movement of one unit to the right and no vertical movement) and the stated vector. The second formula finds the angle Δθ in a similar manner.
The triple bar is a congruence relation (read x ≡ y mod 3 as x congruent to y mod 3). When two things are congruent mod something it indicates that the remainders of the two are the same when divided by the other number. Thus 13 ≡ 6 mod 7 because both 6 and 13 leave a remainder of 6 when divided by 7.
The formula for Δθ is saying that this angle is equal to the value computed on the right when both are interpreted as an angle between 0 and 360 degrees.
This is not the same as using the atan2 function. atan2 is useful for computing the angle a single vector forms with the x-axis, but not when we need the angle between two vectors (without transforming the coordinate system first).

fast calculation of the intersection area of a triangle and the unit square

In my current project I need to calculate the intersection area of triangles and the unit squares in an infinite grid.
For every triangle (given by three pairs of floating point numbers) I need to know the area (in the interval (0,1]) it has in common with every square it intersects.
Right now I convert both (the triangle and the square) to polygons and use Sutherland-Hodgman polygon clipping to calculate the intersection polygon, which I then use to calculate its area.
This approach now shows to be a performance bottleneck in my application. I guess a more specialized (analytical) algorithm would be much faster. Is there a standard solution for this problem, or do you have any idea? I only need the areas, not the shape of the intersections.
Your polygon are convex. There are some algorithms for convex polygons faster than general ones. I've used O'Rourke algorithm with success (code from his book here, I believe that good description exists). Note that some values may be precomputed for your squares.
If your polygons not always intersect, then you may at first check the fact of intersection with separating axes method.
Another option to try- Liang-Barski algorithm for clipping every triangle edge by square.
Edit: You can quickly find all intersections of triangle edges with grid using algorthm of Amanatides and Woo (example in grid traversal section here)
To process this task with hi performance , i suggest some modifications of
Vatti line sweep clipping.
http://en.wikipedia.org/wiki/Vatti_clipping_algorithm
Stepping from minimal Y vertex of your Triangle make such steps:
sort vertexes by Y coordinate
step Y higher to MIN(nextVertex.Y, nextGridBottom)
Calculate points of intersection of grid with edges.
Collect current trapezoid
repeat from step2 until vertex with highest Y coordinate.
Split trapezoids by X coordinate if required.
here is example of Trapezoidalization in X direction
http://www.personal.kent.edu/~rmuhamma/Compgeometry/MyCG/PolyPart/polyPartition.htm
It illustrate main idea of line sweep algorithm. Good luck.
You are not mentioning what precision you are looking for. In case you are looking for a analytical method, disregard this answer, but if you just want to do antialiasing I suggest a scanline edge-flag algorithm by Kiia Kallio. I have used it a few times and it is quite fast and can be set up for very high precision. I have a java implementation if you are interested.
You can take advantage of the regular pattern of squares.
I'm assuming the reason this is a bottleneck is because you have to wait while your algorithm finds all squares intersecting any of the triangles and computes all the areas of intersection. So we'll compute all the areas, but in batches for each triangle in order to get the most information from the fewest calculations.
First, as explained by others, for each edge of the triangle, you can find the sequence of squares that edge passes through, as well as the points at which it crosses each vertical or horizontal edge of a square.
Do this for all three sides, keeping a list of all the squares you encounter, but keep only one copy of each square. It may be useful to store the squares in multiple lists, so that all squares on a given row are all kept in the same list.
When you've found all squares the triangle's edges pass through, if two of those squares were on the same row, any squares between those two that are not in the list are completely inside the triangle, so 100% of each of those squares is covered.
For the other squares, the calculation of area can depend on how many vertices of the triangle are in the square (0, 1, 2, or 3) and where the edges of the triangle intersect the sides of the square. You can summarize all the cases in a few pencil-and-paper drawings, and come up with calculations for each one. For example, when an edge of triangle crosses two sides of the square, with one corner of the square on the "outside" side of the edge, that corner is one angle of a small triangle "cut off" by that edge of the larger triangle; use the points of intersection on the square's sides to compute the area of the small triangle and deduct it from the area of the square. If two points instead of one are "outside", you have a trapezoid whose two base lengths are found from the points of intersection, and whose height is the width of the square; deduct its area from the square. If three points are outside, deduct the entire area of the square and then add the area of the small triangle.
One vertex of the large triangle inside the square, three corners of the square outside that angle: draw a line from the remaining corner to the triangle's vertex, so you have two small triangles, deduct the entire square and add those triangles' areas. Two corners of the square outside the angle, draw lines to the vertex to get three small triangles, etc.
I'm phrasing this so that you always assume you start with the entire area of the square and reduce the area by some amount depending on how the edge of the triangle intersects the square. That way, in the case where the edges of the triangle intersect the square more than twice--such as one edge cuts across one corner of the square and another edge cuts across a different corner, you can just deduct the area cut off by the first edge, then deduct the area cut off by the second edge.
This will be a considerable number of special cases, though you can take advantage of symmetry; for example, you don't have to write the complete calculation for "cut off a triangle in one corner" four times.
You'll write a lot more code than if you just took someone's convex-polygon library off the shelf, and you will want to test the living daylights out of it to make sure you didn't forget to code any cases, but once you get it working, it shouldn't take much more effort to make it reasonably fast.

largest empty sphere or rectangle

In N (~ 500) dimensions, I wish to find out the largest sphere or rectangle such that the sphere/rectangle does not contain already existing points. The entire set of points is bounded in an axis-aligned rectangular box (lower and upper bounds on the values).
Is there any known polynomial time method/code that I can use to solve my problem?
The two well known algorithms: i) the largest empty rectangle within a rectangle (http://www.cs.princeton.edu/~chazelle/pubs/ComputLargestEmptyRectangle.pdf) and, ii) finding largest empty circle within location constraints (http://www.cs.dartmouth.edu/reports/TR86-130.pdf) do not work.
Although the complexity of above algorithms is N log N or N^2 log N, where N is the number of already existing points, the complexity is also a linear function of the number of vertices of the convex hull or the bounding polygon. A rectangle in 500 dimensions will have 2^500 corners which makes the above techniques infeasible.
Ideally, I am looking for a method (it does not have to be exact) that can determine the largest cirle/rectangle in polynomial time in N (number of points) and D (the dimension).
Thank you.
One possible heuristic solution is to restrict the large rectangle so that it's axis-aligned. In this case, a rectangle can be bounded by at most 2 * d points, where each point represents a bounding min or max to one or more dimensions. It can be then be determined if a point is inside that rectangle in only O(d).
A heuristic method to make use of this is to pick 2 points, and use those to form an initial bounding box, then randomly add points. If the point is inside the box, shrink or split the box. If the point is outside of the box, try to make the box bigger. A single pass should take O(d * N) if the box is shrunk instead of split.
The quality perhaps can be improved a bit by ensuring that no point is within the bounding box formed by the two points. It might be ideal to find all pairs of points such that no point is within the bounding box, as when converted to a graph, they should help with expanding the solution in a less random way. Dynamic programming likely leads to an algorithm that is better than O(d*N^3) perhaps O(d*N^2) or better.

How to find line segments intersecting a 2D viewport

In an infinite 2D space there are a set of lines, each line having a start and end point, and a time of creation: Line(p0, p1, t).
I want to find the lines that should be rendered in a top-down view of this 2D space (higher values of t show up closer to the viewport, not that it should be relevant.)
The intuitive answer is "check if either point is within the viewport coordinates," but this falls down when the points are further apart than the viewport area covers.
The other idea I had was using something like geohash, this would limit precision i.e. maximum zoom level of the viewport. The idea is enumerating the hashes of the cells intersected and storing them. This way querying is a matter of asking the right question.
Are there any ideal solutions? Has this been solved before?
I think you need to check two conditions: one that the rectangle of viewport overlaps the rectangle with corners (p0,p1) and the second that some corners of viewport rectangle are on the different sides of the whole line which contains line segment (p0,p1).
The task of finding rectangle overlap can be solved very effectively for very large number of rectangles using R-trees (http://en.wikipedia.org/wiki/R-tree).
The second task can be reduced to checking signs of the cross product of (p1-p0) x (corner_coordinate-p0)
(all three quantities taken as 3-d vectors with third coordinate equal to zero, the result will be vector along the perpendicular direction). There should be corners with the opposite sign of this cross product.

Resources