Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed last year.
Improve this question
So I have a line, connecting two points, and I want to give this line a visual name in my canvas. I want this name to be positioned right at the middle of the line but with an offset so that the text doesn't go through the line. Determining the middle of the line isn't that hard you can just find it out like that:
And the line equation of the normal to the line can be determined pretty easy too:
Where you can find out n just by inserting P into the equation.
Now to the problem: For a given distance to the line, I want to find a point P(xn, yn) that is on that normal to the line through the middle of the line. After inserting the linear function into the distance formula I end up with this:
Not only am I not able to transform this equation to get a valid value for xn, I believe there has to be an mathematically easier way to achieve this.
Line middle is
mx = (x1 + x2)/2
my = (y1 + y2)/2
Line length is
len = sqrt((x1 - x2)^2 + (y1 - y2)^2)
Normalized direction vector of the line is
dx = (x2-x1) / len
dy = (y2-y1) / len
Perpendicular vector is
nx = -dy
ny = dx
Point at offset d from middle is
px = mx + d * nx
py = mx + d * ny
or for other side
px = mx - d * nx
py = mx - d * ny
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
Update
I do not think this question is off topic.
The solution provided is what I was looking for and it is a programming solution.
================
I want to know how can I find the coordinates of equal chords from the same point on the circle.
As shown in the image below, I will like to choose a random point on a circle and a random chord angle (in the example its 110 degrees).
I will know the radius (r) of the circle and one randomly selected point (A) on a circle.
Based on this data, I would like to know how can I draw two equal chords from this point (AB and AC) where AB = AC.
Let you have circle center xc, yc, radius R.
At first choose random angle in range 0..2*Pi
aangle = random(2*Pi)
Then A coordinates are
ax = xc + R * Cos(aangle)
ay = yc + R * Sin(aangle)
Now choose random (or you need specific value?) chord angle in needed range and get B, C coordinates
changle = random(3 * Pi / 4)
bx = xc + R * Cos(aangle + changle)
cx = xc + R * Cos(aangle - changle) // note subtraction
and similar for Y-coordinates
If you have A coordinates, you can also rotate them around center
bx = xc + (ax - xc) * Cos(changle) - (ay - yc) * Sin(changle)
and so on
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I need to calculate circumcenter coordinates (or at least I hope they're called that) at point C for an isosceles triangle (the circle must be such, that created triangle is). I know the point O (origin), two vectors p and q (length may differ) originating in that point (leading to points P and Q). I also know the radius r of this to be circumscribed circle. When the circle's center is known it should create said green highlighted isosceles triangle. Here is drawing for better understanding:
Update (solution):
Calculates the length of p and q vectors
Normalize them both, and add them together
Normalize this to be OC vector again
Finally extend OC vector from point of origin O to length equivalent to radius r
Thinking geometrically:
normalise vectors p and q, i.e. p = p / |p|, q = q / |q|
add them together
normalise the result
multiply that by r - this is the vector OC
add to O
Steps 1 - 3 simply produce the bisection of the vectors p and q
EDIT this is simplified somewhat compared to my original answer.
The first equation of your system is:
(x_c-x_o)^2 + (y_c-y_o)^2 = r^2
The second one is more convoluted. You must intersect the circumference
(x-x_c)^2+(y-y_c)^2 = r^2
with your two vectors, that have equation rispectively
y = (Q_y/Q_x)*x and y = (P_y/P_x)*x
this gives you the two points of intersection p and q in function of x_c and y_c. Now force hte distance OP and OQ to be equal (you want an isoscele triangle), and you have your second equation.
Solve hte two equation system and you have the formula for x_c and y_c.
Assuming i did the math right, the solution is:
x_c = ((a+b)^2 * r^2) / ((a+b)^2+4)
y_c = (-2*(a+b) * r^2) / ((a+b)^2+4)
where
a = p_y / p_x
b = q_y / q_x
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 1 year ago.
Improve this question
I have a cubic bezier with 2 control points. Starting point and control points are known. Need to get all the points of the curve, given the control, starting and ending points.
What I wanna to achieve is ..given a value i from 1 to length of curve.. get the X and Y and alpha (angle) of each point in that position.
I cannot find a good reference or working code for that.
I'm using javascript.
If I understand correctly, you are trying to determine the position and slope (tangent to the curve) of the Bezier, at every point.
Let's assume that your start point is (ax, ay), the end point is (dx, dy) and your control points are (bx, by) and (cx, cy).
Position is easy. First, compute the blending functions. These control the "effect" of your control points on the curve.
B0_t = (1-t)^3
B1_t = 3 * t * (1-t)^2
B2_t = 3 * t^2 * (1-t)
B3_t = t^3
Notice how B0_t is 1 when t is 0 (and everything else is zero). Also, B3_t is 1 when t is 1 (and everything else is zero). So the curve starts at (ax, ay), and ends at (dx, dy).
Any intermediate point (px_t, py_t) will be given by the following (vary t from 0 to 1, in small increments inside a loop):
px_t = (B0_t * ax) + (B1_t * bx) + (B2_t * cx) + (B3_t * dx)
py_t = (B0_t * ay) + (B1_t * by) + (B2_t * cy) + (B3_t * dy)
Slope is also easy to do. Using the method given in https://stackoverflow.com/a/4091430/1384030
B0_dt = -3(1-t)^2
B1_dt = 3(1-t)^2 -6t(1-t)
B2_dt = - 3t^2 + 6t(1-t)
B3_dt = 3t^2
So, the rate of change of x and y are:
px_dt = (B0_dt * ax) + (B1_dt * bx) + (B2_dt * cx) + (B3_dt * dx)
py_dt = (B0_dt * ay) + (B1_dt * by) + (B2_dt * cy) + (B3_dt * dy)
And then use Math.atan2(py_dt,px_dt) to get the angle (in radians).
De Casteljau algorithm is more numerically stable. Here it has additional advantage that it calculates the tangent line (and thus, the tangent angle) as the step immediately prior to calculating the point.
But, it works according to a parameter value, not length. It is preferable to calculate points by parameter, not value, as part of rendering the curve. Parameter's range will be [0 ... 1], 0 corresponding to the starting, and 1 the ending point of the curve.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I have drawn a rectangle. I know its (x1,y1) Top Left and (x2,y2) Bottom Right coordinates.. I also have the height h and width w of drawn rectangle.. How can I find the center coordinates (x,y) ?
I am currently using the following formula.
(x,y) = (x2 + x1)/2, (y2+y1)/2
It gives the correct y coordinate but no luck in x.
The center of rectangle is the midpoint of the diagonal end points of rectangle.
Here the midpoint is ( (x1 + x2) / 2, (y1 + y2) / 2 ).
That means:
xCenter = (x1 + x2) / 2
yCenter = (y1 + y2) / 2
Let me know your code.
Center x = x + 1/2 of width
Center y = y + 1/2 of height
If you know the width and height already then you only need one set of coordinates.
We can calculate using mid point of line formula,
centre (x,y) = new Point((boundRect.tl().x+boundRect.br().x)/2,(boundRect.tl().y+boundRect.br().y)/2)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed last year.
The community reviewed whether to reopen this question 3 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
Suppose I have a line segment going from (x1,y1) to (x2,y2). How do I calculate the normal vector perpendicular to the line?
I can find lots of stuff about doing this for planes in 3D, but no 2D stuff.
Please go easy on the maths (links to worked examples, diagrams or algorithms are welcome), I'm a programmer more than I'm a mathematician ;)
If we define dx = x2 - x1 and dy = y2 - y1, then the normals are (-dy, dx) and (dy, -dx).
Note that no division is required, and so you're not risking dividing by zero.
Another way to think of it is to calculate the unit vector for a given direction and then apply a 90 degree counterclockwise rotation to get the normal vector.
The matrix representation of the general 2D transformation looks like this:
x' = x cos(t) - y sin(t)
y' = x sin(t) + y cos(t)
where (x,y) are the components of the original vector and (x', y') are the transformed components.
If t = 90 degrees, then cos(90) = 0 and sin(90) = 1. Substituting and multiplying it out gives:
x' = -y
y' = +x
Same result as given earlier, but with a little more explanation as to where it comes from.
We know that: if two vectors are perpendicular, their dot product equals zero.
The normal vector (x',y') is perpendicular to the line connecting (x1,y1) and (x2,y2). This line has direction (x2-x1,y2-y1), or (dx,dy).
So,
(x',y').(dx,dy) = 0
x'.dx + y'.dy = 0
The are plenty of pairs (x',y') that satisfy the above equation. But the best pair that ALWAYS satisfies is either (dy,-dx) or (-dy,dx)
m1 = (y2 - y1) / (x2 - x1)
if perpendicular two lines:
m1*m2 = -1
then
m2 = -1 / m1 //if (m1 == 0, then your line should have an equation like x = b)
y = m2*x + b //b is offset of new perpendicular line..
b is something if you want to pass it from a point you defined