A few points are given on X axis and a few on Y axis , Find number of right triangles using the given points as vertices.
Your approach is nice, just avoid extensive checking.
Put coordinates in (hash)map to provide almost O(1) access.
For every a^2 factorize it and check for divisors (and complementary ones) in the map.
(there is no more than 2*a divisors for a^2)
Related
I'm looking for help with a workflow that, given an arbitrary number of points in a (x, y) plane, can find all line segments that connect 2 (and only 2) points. My first thought is to use scipy.spatial.distance.pdist() to get all unique pairwise distances, then for each distance between two points, test whether any of the other points are collinear. I can imagine that approach becoming inefficient with large numbers of points, so I want to know if anyone knows of a more efficient way to solve this problem.
The question is unwieldy, so I'll provide an example. I have a list of 3 (x, y) coordinate points coords = [(1,1), (2,1), (3,1)] that happen to form a straight line, and want to find the lengths of all line segments that connect 2 (and only 2) points. For this example, I only have two line segments of length 1 that meet my criteria (from (1,1) to (2,1), and (2,1) to (3,1)). I can get all the pairwise distances between points using scipy.spatial.distance.pdist(), but that function's result includes the distance between (1,1) and (3,1), which I don't want.
My preferred language is Python, but I'm open to answers in other coding languages.
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).
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.
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.
Summed area table is a pre-filtering technique to avoid aliasing in texture map. I would like to know how it works and what are the advantages and disadvantages over Mipmap. Thanks
A summed area table replaces the X*Y texture, T, with an X*Y set of values, S, where entry x,y in S is the sum of all pixels of T contained in the axis-aligned rectangle from [0,0] to [x, y] inclusive.
Given that you want to compute the average of an arbitrary axis-aligned rectangle of pixels of T, you just need to sample the corner locations in S and do some trivial additions and subtractions. (Hmm. It seems that wikipedia sums (pun intended) this up quite succinctly)
The advantages of the SAT is that it will quickly give you the correct results for any arbitrary axis-aligned rectangle (rather than being limited to power-of-2 squares as in MIP mapping) but
You won't get automatic hardware support as you do with MIP maps
the SAT can be huge because, assuming you start summing from the top left texel, the bottom right needs to be able to equal the sum of all the texels. For a 1K*1K texture, you'd need 11 extra bits of precision per channel!