I have been very confused about this, so the basic idea of Hough's line discovery is that any line can be represented by a unique r and θ.
r = x.cos(θ) + y.sin(θ)
And further, every pixel on a given line will transform to the exact same r and θ for that line
but this assumption fails for the simplest of lines.
In the given plot, two points are on the same line, but their r is different
explanation??
It is tempting to say that for the points (1, 1) and (3, 3), θ is π / 4 and r is the square root of 2 and 3 respectively.
These would be the polar coordinates of the points. But you can't expect two distinct points to have the same r and θ... How would you draw them?
r and θ are not the polar coordinates of the points here.
r is the distance from the origin to the line and θ is an angle such that the vector (cos θ, sin θ) is orthogonal to the line.
(See http://en.wikipedia.org/wiki/Hough_transform for a figure my reputation does not allow me to post here).
With r and θ fixed in this way, all the points M=(x, y) on the line verify the equation: r = x.cos(θ) + y.sin(θ).
The right side can be seen as the dot product between the vector OM (where O is the origin) and the vector (cos θ, sin θ).
In your example, r would be 0 and θ would be 3π / 4.
For lines that do not go through the origin, (r, θ) are the polar coordinates of one point only on the line, which is the point closest to the origin.
Hope this helps.
Related
I'm trying to make a basic sphere animation.
So far, I have some points lying on a sphere, which can be rotated around its origin by using the rotation matrices found on Wikipedia:
I now want to rotate the sphere so that a specific point is at the front (at [0, 0, 1]). I think that I might have to find the angle between the two vectors in each plane and rotate by this amount, but I'm not sure if this is correct, or how it can be achieved.
So, how would I find the angles required to do this?
In spherical coordinates, your point is at (sin θ cos φ, sin θ sin φ, cos θ) where θ = arccos(z) and φ = atan2(y, x).
Beware of conventions: here, θ is the polar angle or inclination and φ is the azimuthal angle or azimuth.
Since you want to move your point to (0, 0, 1), you can first set φ to zero with a rotation around the z axis, and then set θ to zero with a rotation around the y axis.
The first rotation is Rz(-φ):
cos φ sin φ 0
-sin φ cos φ 0
0 0 1
The second rotation is Ry(-θ):
cos θ 0 -sin θ
0 1 0
sin θ 0 cos θ
The composition is Ry(-θ) * Rz(-φ):
cos θ cos φ cos θ sin φ -sin θ
-sin φ cos φ 0
sin θ cos φ sin θ sin φ cos θ
Note that the last row is (x, y, z), which confirms that this point will move to (0, 0, 1).
Another way to construct a rotation matrix that takes (x, y, z) to (0, 0, 1), is to construct the inverse (that takes (0, 0, 1) to (x, y, z)) and then transpose it. You need three basis vectors that are perpendicular to each other, one for each column of the matrix. However, since we will transpose the result at the end, we can just consider these vectors to be the rows of the final matrix.
The first vector is V3 = (x, y, z), and this goes into the third row (since we want to move it to the z unit vector). The two other vectors can be computed using the cross-product with some other arbitrary vector. Many games and 3D engines use a "look-at" function that takes an "up" vector because the world usually has a sense of up and down.
Let's take UP = (0, 1, 0) as our "up" vector. You can compute V1 = norm(cross(UP, V3)) and V2 = cross(V3, V1). Depending on the order of arguments, you can flip the sphere (you can also multiply one of the vectors by -1). We don't need to normalize V2 since V1 and `V3 are both already unit vectors.
So the vectors are:
V3 = (x, y, z)
V1 = norm(cross(UP, V3)) = (z/sqrt(xx+zz), 0, -x/sqrt(xx+zz))
V2 = cross(V3, V1) = (-xy/sqrt(xx+zz), sqrt(xx+zz), -yz/sqrt(xx+zz))
And the final rotation matrix, with S = sqrt(xx+zz), is:
z/S 0 -x/S
-xy/S S -yz/S
x y z
Note that it's different from the one we obtained from Ry(-θ) * Rz(-φ). There are an infinite number of rotation matrices that move a point to another point, because rotations have three degrees of freedom and moving on a surface only has two if you don't consider the final orientation. You can get other results by changing the "up" vector.
You have given N points in 2D plane ,i need to find out the smallest radius of a circle that contain at least M points .
Approach I am using :-
I will do binary search on radius of circle .
Pick an arbitrary point P from the given set. We rotate a circle C with radius R using P as the "axis of rotation" (by convention, in the counter-clockwise direction), i.e. we keep C to touch P any time during the rotation. While C is being rotated, we maintain a counter to count the number of points C encloses.
Note that this counter only changes when some point Q enters (or leaves) the region of the circle C. Our goal is to come up with an algorithm that will increment (or decrement) this counter, whenever some other point Q ≠ P enters (or leaves) the region of C.
The state of the (rotating) circle C can be described by a single parameter θθ, where (r,θ) are the polar coordinates of the center of the circle C, if we pick P as the fixed point of the polar coordinate system. With this system, rotating C means increasing θ.
For each other point Q (≠ P), we can actually compute the range of θ for which C covers Q. Put more formally, C encloses Q whenever (iff) θ∈[α,β].
So, up to this point, the original problem has been reduced to:
What is an optimal value of θ that lies in the most number of [α,β] intervals?
The reduced problem can be solved with a pretty standard O(NlogN) algorithm.[3] This reduced problem has to be solved N times (one for each point P), hence the time complexity O(N2logN).
I am able to get the how to do this step :
For each other point Q (≠ P), we can actually compute the range of θ for which C covers Q. Put more formally, C encloses Q whenever (iff) θ∈[α,β].
So, up to this point, the original problem has been reduced to:
What is an optimal value of θ that lies in the most number of [α,β]intervals?
can you please suggest how to implement that part .
When Q enters or leaves the circle C (with radius R):
The distance between P and C's center is R (because it always is); and
The distance between Q and C's center is also R
So, if you draw a circle of radius R around Q, and a circle of radius R around P. The two points at which they intersect are the centers of C when Q enters or leaves.
Let ±θ be the angles between those centers of C and line PQ. If you draw it out you can easily see that |PQ|/2R = cos(θ), which makes it pretty easy to find the angles you're looking for.
I have a set of (2-dimensional) line segments. I want to fit a curve of second degree which is parallel to the linesegments.
I would like to do this by using an implicit function like this: f(x,y) = a x^2 + b xy + c y^2 + d x + e y + f = 0.
I also have a couple of points where the curve should start with, so it is possible to determine f.
Here is what I tried so far:
I computed lines which are perpendicular to my line segments. Then I would like to set the gradient at the intersection point of the curve perpendicular to the lines gradient (and so equals the line segment). Unfortunately there are two problems: 1) I might have 2 solutions (intersections) 2) I do not know the intersecting points as long I don't know the coefficient of f. Eventually I get non linear equation systems which I do not know how to solve.
So far I used the singular value decomposition to solve my linear equation systems.
I have encountered the following interesting problem while preparing for a
contest.
You have a triangle with sides of length a, b, c and a rope of length L. You need to find
the surfaced enclosed by the rope that has the maximum surface area and it has to be entirely inside the triangle.
So, if L = a + b + c, then it's the area of the triangle.
Else, we know that the circle has the biggest surface to perimeter area, so if L is smaller or equal to the perimeter of the inscribed circle of the triangle, then the area will be the area of the circle of perimeter L.
So, the remaining case is alfa < L < a + b + c, where alfa is the perimeter of the inscribed circle .
Any ideas would be great!
EDIT: I would like to know if I should focus on some kind of algorithm for solving this
or trying to figure it out a mathematical formula. The contest contains somehow a combination of both. The edges can be as long as 100 and the precision of a,b,c,L is of 4 digits after the decimal point .
After reading the answers to this question: https://math.stackexchange.com/questions/4808/why-circle-encloses-largest-area, I agree with n.m., and think the optimal curve verifies:
Curvature is either constant, or flat when it touches the triangle, meaning it is composed of segments lying on the triangle sides, and circle arcs, all sharing the same radius.
There are no angles, meaning the arcs are tangent to the triangle sides.
With these conditions, the solution is obtained by three circles of same radius R, each tangent to two sides of the triangle (see below). When R varies between 0 and the radius of the inscribed circle, we start from the triangle itself, and end to the inscribed circle, where all three circles coincide. The length of the curve is the perimeter of the circle of radius R + the perimeter (p) of the smaller triangle: L = 2*PiR + p. The area is the area (a) of the smaller triangle + one disc of radius R + the remaining rectangles: A = PiR^2 + p*R + a.
Since a circle has the largest Area/Perimeter, start with the inscribed circle. If L is less than that circumference, then shrink appropriately. If L is longer, grow whichever of the 3 arcs maximizes dA/dL. I don't know if there's a closed form, but the largest arc will be in the 3rd of the triangle with the sides most approaching parallel.
It should be trivial to solve this algorithmically. With 4 decimals of precision, increment by 0.0001 checking each arc to see which has the greatest dA/dL for that single increment.
I worked up a drawing of the geometry overnight:
The inscribed circle is constructed by bisecting each of the angles and finding the intersections of the bisectors. I've labeled the half-angle "a1" (and all related variables have '1'). The area of the non-circular portion is two trapezoids (one denoted with the red outline). We can calculate the area for a single trapezoid as L1 * (m1 + R)/2 (note that when L1, L2, L3 are all zero, these trapezoids are all zero, and we just get the inscribed circle area). The circular cap has a radius of m1 to remain tangent with the side of the triangle. For a given choice of L1, m1 = R(x1-L1)/x1.
From here you can easily calculate the perimeter and area of each of the three sectors and solve numerically.
I cannot prove that this is the largest area, just that this is how to calculate the area and perimeter of this construction.
..answering my own comment/question, it can be proved that the radii must be equal,
Here is a useful formula:
the gray area A is
A = r^2 ( alpha - Pi + 2/tan(alpha/2) ) /2
but even more useful..the arc length is simply:
s = 2 ( b - A/r )
from here it is straightforward to show the three radii must be equal to each other:
writing the rope length and enclosed area:
ropelength = trianglelength - 2 Sum[r[i] a[i] ]
ropearea = trianglearea - Sum[r[i]^2 a[i] /2 ]
where
a[i]=( alpha[i] - Pi + 2/tan(alpha[i]/2) )
after a bit of manipulation maximizing the area leads to all r[i] equal. Note the three a[i], ropelength,trainglearea,trianglelength are all constants that you do not need to work out. Pedantically solve for r[l] = f( constants, r[2],r[3] ) sub into the second expression and solve for d ropearea /d r[2] = 0 and d /d r[3] = 0 with the result:
r =(1/2) (triangle_length - rope_length) /(Sum(1/tan(alpha[i]/2)) - Pi)
(the messy expression for a[i] is substituted only at the last step ) finally..
ropearea = trianglearea - (trianglelength-ropelength)^2/(8 Sum[a[i])
= trianglearea - (1/2)(trianglelength-ropelength) r
edit -- a useful identity ..with a,b,c, the lengths of the sides.
Sum(1/tan(alpha[i]/2)) = Sqrt( S^3 / ((S-a)(S-b)(S-c)) )
S = 1/2 (a+b+c) ! S is semiperimeter not to be confused with arc length s
the above expressions then can be used to reproduce the formula for an inscribed circle,
rinscribed = Sqrt( ((S-a)(S-b)(S-c)) / S )
If the perimeter of the rope is too small or too large, the answers are trivial. The interesting case is a shape with 6 vertices that goes line-arc-line-arc-line-arc. The arc are all tangent to their neighbouring lines and their radii are equal. I don't have a rigorous proof, but imagine a 2D balloon filled with air and squeezed between the sides of the triangle.
It is easy to express the overall shape and thus the perimeter given the radius; the opposite direction (perimeter to radius) is then easily found numerically.
I have a circle with two points inside it that make up a line segment. How can I calculate the distance from one endpoint to the edge of the circle where the line would intersect it?
I think the easist way is to figure out where the intersection of the line and the circle is, then just calculate the distance from the line segment point with the intersection point.
So, say your circle is described by the equation
x^2 + y^2 = 5
and your line segment is points like
(1,3), (2,4)
First, you figure out the equation for the line that is directly over the segment, which, in this case, would be
y = x + 2
You then substitute this equation into the first equation, and you get
x^2 + (x+2)^2 = 5
Simplify this into
2x^2 + 4x - 1 = 0
and solve via the quadradic formula.
You now have the x coordinates of the two intersection points. From there, plug into the line equation to get the y coordinates. Then you can just do normal point distance calculation ala Pythagoras.
sqrt ( (x1 - x2)^2 + (y1 - y2)^2 )
Use the center of the circle as a point of reference. Get the distance from the center point to your two points, then the radius of the circle. You can now draw a triangle between any three of those points (center, segment point, and circle edge.) Pythagoras can handle the rest.
Two points define a line L. Solve for an equation Cx + L = 0 where C is the equation of the circle. If I remember correctly :P Some more information here.