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.
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 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
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
what does the ' mean in x' y' and what is cos(a) what is the 'a' equal to what is it? How am I to understand what x' and 'a' is if they don't remember to write the equation then put that 'a' is equal to .... whatever? so I can figure it out? how am I to know what to put into the cos(?) and sin(?) if I don't know what 'a' is equal to?
x' = x * (width' / width)
y' = y * (height' / height)
Mapping for rotation is only a little bit harder.
x' = x * cos(a) + y * sin(a)
y' = y * cos(a) - x * sin(a)
I am trying to figure out how to find the destination_x, destination_y, angle_x, angle_y for displaying an image into the desktop and angles and rescale it to show it properly I keep getting the image to show but not where I want it -- center screen -- and it is chopped off at angles into polygons, hexagons mollymoolygons and what not. My fingers are googled out. ~
a is the input angle. sin and cos are trigonometric functions that help you to calculate positions based on this angle.
x' and y' are the output from the functions. You'll still need to use them as (x,y) coordinates, but the prime symbol indicates that the values have changed because of the function. This is particularly important in your second set of equations:
x' = x * cos(a) + y * sin(a)
y' = y * cos(a) - x * sin(a)
The equation to derive y' uses plain x, the old x, not the new one derived in the equation just above it.
The commentators and closers are also correct that this is not a programming question per se. Rather this is part of the math needed in the domains of computer graphics and image processing. Specifically these are equations or functions for coordinate transformation. In programming this is usually done with matrix multiplication.
I've been doing a lot of research on the topic and found a couple of post that where helpful but I just can't get this right.
I am developing a very simple structural analysis app. In this app I need to display a graph showing the internal stress of the beam. The graph is obtained by the formula:
y = (100 * X / 2) * (L - X)
where L is the known length of the beam (lets say its 1 for simplicity). And X is a value between 0 and the Length of be beam. So the final formula would be:
y = (100 * X / 2) * (1 - x) where 0 < X < 1.
Assuming my start and end points are P0 = (0,0) and P2 = (1,0). How can I obtain P2 (control point)?? I have been searching in the Wikipedia page but I am unsure how to obtain the control point from the quadratic bezier curve formula:
B(t) = (1 - t)^2 * P0 + 2*(1 - t)*t * P1 + t^2 * P2
I'm sure it must be such an easy problem to fix… Can anyone help me out?
P.S.: I also found this, How to find the mathematical function defining a bezier curve, which seems to explain how to do the opposite of what I am trying to achieve. I just can't figure out how to turn it around.
We want the quadratic curve defined by y to match the quadratic Bezier curve
defined by B(t).
Among the many points that must match is the peak which occurs at x =
0.5. When x = 0.5,
y = (100 * x / 2) * (1 - x)
100 1 25
y = ---- * --- = ---- = 12.5
4 2 2
Therefore, let's arrange for B(0.5) = (0.5, 12.5):
B(t) = (1-t)^2*(0,0) + 2*(1-t)*t*(Px, Py) + t^2*(1,0)
(0.5, 12.5) = B(0.5) = (0,0) + 2*(0.5)*(0.5)*(Px, Py) + (0.25)*(1,0)
0.5 = 0.5 * Px + 0.25
12.5 = 0.5 * Py
Solving for Px and Py, we get
(Px, Py) = (0.5, 25)
And here is visual confirmation (in Python) that we've found the right point:
# test.py
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 1, 100)
y = (100*x/2)*(1-x)
t = np.linspace(0, 1, 100)
P0 = np.array([0,0])
P1 = np.array([0.5,25])
P2 = np.array([1,0])
B = ((1-t)**2)[:,np.newaxis]*P0 + 2*((1-t)*t)[:,np.newaxis]*P1 + (t**2)[:,np.newaxis]*P2
plt.plot(x, y)
plt.plot(B[:,0], B[:,1])
plt.show()
Running python test.py, we see the two curves overlap:
How did I know to choose t = 0.5 as the parameter value when B(t) reaches its maximum height?
Well, it was mainly based on intuition, but here is a more formal way to prove it:
The y-component of B'(t) equals 0 when B(t) reaches its maximum height. So, taking the derivative of B(t), we see
0 = 2*(1-2t)*Py
t = 0.5 or Py = 0
If Py = 0 then B(t) is a horizontal line from (0,0) to (1,0). Rejecting this degenerate case, we see B(t) reaches its maximum height when t = 0.5.
Your quadratic bezier curve formula has a typo in the middle term. It should be:
B(t) = (1 - t)^2 * P0 + 2 * (1 - t) * t * P1 + t^2 * P2
This means you should take the P1=(1,50) that #unutbu found and divide the coordinates in half to get P1=(.5,25). (This won't matter if you're plotting the parametric equation on your own, but if you want something like LaTeX's \qbezier(0,0)(.5,25)(1,0), then you'll need the corrected point.)
The P1 control point is defined so that the tangent lines at P0 and P2 intersect at P1. Which means that if (P1)x=(P2)x, the graph should be vertical on its righthand side (which you don't want).
In response to your comment, if you have a quadratic y=f(x), then it is symmetrical about its axis (almost tautologically). So the maximum/minimum will occur at the average of the roots (as well as the control point).
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