My trigonometry needs a little help.
How would I go about calculating the point of the nearest possible intersection with a line along a rounded corner?
Take this image:
What I would like to know is, given that I know point a, and the dimensions of the rectangle, how would I find point b when the edges of the rectangle are curved?
So far, as you can see, I've only managed to calculate the nearest edge of the rectangle as if it had right-angled corners.
If it matters, I'm doing this in ActionScript 3. But example sudo-code will suffice.
Calculate the vector from the midpoint M of the corner to A:
v_x = a_x - m_x
v_y = a_y - m_y
then go radius of the corner r times towards A to get to the intersection point I
i_x = m_x + r*v_x
i_y = m_y + r*v_y
This obviously only works if the nearest intersection is on the rounded corner. Just calculate the other intersections with the edges, too, and then check which has the nearest distance to A.
You need to know the radius R of the circle that generates the round corner and the coordinates (Xr,Yr) of the point where the two sides of a non rounded rectangle cross each other.
Then the coordinates for the center of the circle that generates the round corner are (Xc, Yc) = (Xr-R, Yr-R)
From here, it's a matter of solving the equation of the cross point between the segment line defined by point A=(Xa, Ya) and point (Xc, Yc), whose parametric equation is:
x = Xa + p*(Xc-Xa)
y = Ya + p*(Yc-Ya)
and the circle whose equation is
(x-Xc)^2 + (y-Yc)^2 = R^2
Substitute values for x and y from the parametric euation of the line in the equation of the circle, and you will have an equation with only one unkown: p. Solve the equation, and if there are more than one solution, choose the one that is in the range [0,1]. Substitute the found value of p in the parametric equation of the line to get the point of intersection.
Graphically:
If you know the radius and center of the corner as R and C=(Xc, Yc), then the nearest point on the corner to the given point A=(Xa, Ya) is the intersection point of the corner and the line defined by the given point and the center. This point can be directly expressed as
X = Xc + R*(Xa-Xc)/|AC|
Y = Yc + R*(Ya-Yc)/|AC|
where |AC| = Sqrt((Xa-Xc)^2 + (Ya-Yc)^2)
Related
I have :
Arc : defined by p1x, p1y - p2x,p2y , radius, center, initial - final ang.
Line : defined by ax,ay - bx,by.
As you can see at the image I want to figure out an arc tangent to arc and linea and passing by the end point of first arc .
I think there is a unique solution. (or maybe two, R + and R - )
I'm trying so see how to implement an algorithm, without results...
Any idea would be appreciated...
What you are trying to do is to find a circle tangent to an infinite line "L", and tangent to a circle at a particular point on the circle. The key observation is that, since the tangent vector to any circle at a given angle is perpendicular to the radius vector for that angle, what we need to find are the point "TC" ("Tangent Center") and distance "d" at which a line offset from the given line by the distance d intersects a normal drawn outward from the circle for the same distance (forgive my bad art):
The easiest way to solve for "d" is as follows:
Construct the normalized normal vector "R" at point "P" by taking P-C and normalizing. (This constructs the outer tangent to the circle. If you want an inner tangent, you can flip.)
Construct the normalized perpendicular vector "N" to the line "L". I'm not really sure what your variables "ax,ay - bx,by" mean, so let's define the line by a start point "A = (ax, ay)" and a direction vector "DA = (dax, day)". In that case the normal is +/- (-day, dax)/sqrt(day*day+dax*dax). (The normalized cross with the (0,0,1) vector in 3d.)
Choose the sign of "N" so that it points away from P, i.e. if the dot product of (P-A) and N is positive, flip N. if the dot product is (nearly) zero, then the tangent circle would have radius (nearly) zero, and so is not defined.
Now consider a point TC defined by P2 = P + d*(R + N) for some d. If P2 lies on the line L, then d is the radius of the tangent circle we seek! P2 lies on the line if and only if the dot product of (P2 - A) and N is zero. This defines a linear equation in one variable -- d -- so you can solve for it. Note that if R + N is (nearly) of length zero, then P is 180 degrees away from the closest point between the circle and the line; you will need to check for this explicitly and handle it.
Once you have d, you can get the center of the circle TC=P + d*R.
This method should have good numerical stability when the tangent to the circle at P is parallel or nearly parallel to the line.
You didn't specify a language, but hopefully this can get you started. My primary language is c#.
If i have a point (x,y,z) how to project it on to a sphere(x0,y0,z0,radius) (on its surface).
My input will be the coordinates of point and sphere.
The output should be the coordinates of the projected point on sphere.
Just convert from cartesian to spherical coordinates?
For the simplest projection (along the line connecting the point to the center of the sphere):
Write the point in a coordinate system centered at the center of the sphere (x0,y0,z0):
P = (x',y',z') = (x - x0, y - y0, z - z0)
Compute the length of this vector:
|P| = sqrt(x'^2 + y'^2 + z'^2)
Scale the vector so that it has length equal to the radius of the sphere:
Q = (radius/|P|)*P
And change back to your original coordinate system to get the projection:
R = Q + (x0,y0,z0)
Basically you want to construct a line going through the spheres centre and the point. Then you intersect this line with the sphere and you have your projection point.
In greater detail:
Let p be the point, s the sphere's centre and r the radius then x = s + r*(p-s)/(norm(p-s)) where x is the point you are looking for. The implementation is left to you.
I agree that the spherical coordinate approach will work as well but is computationally more demanding. In the above formula the only non-trivial operation is the square root for the norm.
It works if you set the coordinates of the center of the sphere as origin of the system (x0, y0, z0). So you will have the coordinates of the point referred to that origin (Xp', Yp', Zp'), and converting the coordinates to polar, you discard the radius (distance between the center of the sphere and the point) and the angles will define the projection.
Imagine a circle. Imagine a pie. Imagine trying to return a bool that determines whether the provided parameters of X, Y are contained within one of those pie pieces.
What I know about the arc:
I have the CenterX, CenterY, Radius, StartingAngle, EndingAngle, StartingPoint (point on circumference), EndingPoint (point on circumference).
Given a coordinate of X,Y, I'd like to determine if this coordinate is contained anywhere within the pie slide.
Check:
The angle from the centerX,centerY through X,Y should be between start&endangle.
The distance from centerX,centerY to X,Y should be less then the Radius
And you'll have your answer.
I know this question is old but none of the answers consider the placement of the arc on the circle.
This algorithm considers that all angles are between 0 and 360, and the arcs are drawn in positive mathematical direction (counter-clockwise)
First you can transform to polar coordinates: radius (R) and angle (A). Note: use Atan2 function if available. wiki
R = sqrt ((X - CenterX)^2 + (Y - CenterY)^2)
A = atan2 (Y - CenterY, X - CenterX)
Now if R < Radius the point is inside the circle.
To check if the angle is between StartingAngle (S) and EndingAngle (E) you need to consider two possibilities:
1) if S < E then if S < A < E the point lies inside the slice
2) if S > E then there are 2 possible scenarios
if A > S
then the point lies inside the slice
if A < E
then the point lies inside the slice
In all other cases the point lies outside the slice.
Convert X,Y to polar coordinates using this:
Angle = arctan(y/x);
Radius = sqrt(x * x + y * y);
Then Angle must be between StartingAngle and EndingAngle, and Radius between 0 and your Radius.
You have to convert atan2() to into 0-360 before making comparisons with starting and ending angles.
(A > 0 ? A : (2PI + A)) * 360 / (2PI)
I am trying to figure out the correct trig. eq./function to determine the following:
The Angle-change (in DEGREES) between two DIRECTION VECTORS(already determined), that represent two line-segment.
This is used in the context of SHAPE RECOGTNITION (hand drawn by user on screen).
SO basically,
a) if the user draws a (rough) shape, such as a circle, or oval, or rectangle etc - the lines that makes up that shape are broken down in to say.. 20 points(x-y pairs).
b) I have the DirectionVector for each of these LINE SEGMENTS.
c) So the BEGINNING of a LINE SEGMENT(x0,y0), will the END points of the previous line(so as to form a closed shape like a rectangle, let's say).
SO, my question is , given the context(i.e. determinign the type of a polygon), how does one find the angle-change between two DIRECTION VECTORS(available as two floating point values for x and y) ???
I have seen so many different trig. equations and I'm seeking clarity on this.
Thanks so much in advance folks!
If (x1,y1) is the first direction vector and (x2,y2) is the second one, it holds:
cos( alpha ) = (x1 * x2 + y1 * y2) / ( sqrt(x1*x1 + y1*y1) * sqrt(x2*x2 + y2*y2) )
sqrt means the square root.
Look up http://en.wikipedia.org/wiki/Dot_product
Especially the section "Geometric Representation".
You could try atan2:
float angle = atan2(previousY-currentY, previousX-currentY);
but also, as the previous answers mentioned, the
angle between two verctors = acos(first.dotProduct(second))
I guess you have the vector as three points (x_1, y_1), (x_2, y_2) and (x_3, y_3).
Then you can move the points so that (x_1, y_1) == (0, 0) by
(x_1, y_1) = (x_2, y_2) - (x_1, y_1)
(x_2, y_2) = (x_3, y_3) - (x_1, y_1)
Now you have this situation:
Think of this triangle as two right-angled triangles. The first one has the angle alpha and a part of beta, the second right-angled triangle has the other part of beta.
Then you can apply:
You can calculate alpha like this:
If I understand you correctly, you may just evaluate the dot product between two vectors and take the appropriate arccos to retrieve the angle between these vectors.
Using the following start and end point coordinate values of a baseline:
X1 = 5296823.36
Y1 = 2542131.23
X2 = 5311334.21
Y2 = 2548768.66
I would like to calculate the start and end coordinates of a pendicular line that intersects the baseline at the mid-point. This intersecting, perpendicular line should extend at a given distance either side of the baseline (e.g. Dist=100).
I would be very grateful if anyone could provide some guidance using simple formulas that can be tranferred to Excel or VB.
Many thanks in advance.
Steps to do:
Find the midpoint of the two coordinates (xmid, ymid)
Find the gradient of the line segment joining the two coordinates (call it m).
The gradient of a line perpendicular to this line is -1/m.
Use this new gradient and the coordinates of the midpoint (xmid, ymid) to find the equation of the perpendicular line (substitute xmid, ymid and -1/m into the equation of a line), call it y = -1x/m + k
Imagine a right angled triangle from xmid, ymid to your target point (r units along the perpendicular line is the hypotenuse). The x component will be X units across, the y component will be (-1X/m + k) units up.
Solve
r^2 = X^2 + (-1X/m + k)^2
to find X. Where you have already found r, m and k in the previous steps.
Substitute the +ve and -ve values of this into y = -1x/m + k to get the y coordinates of your endpoints, and Bob's your Uncle.
It should be relatively straight forward to translate this into any given programming language in a very short space of time but you may need to understand the underlying maths to do so, and as a Maths teacher I'm not going to do your Homework for you.