Find Bezier control points P1 and P2 given P0, P3 and B(t) as knowns - cubic-bezier

I'll be doing this in LUA. 2D cubic bezier curve.
I know the start and end points of the curve and I have an arbitrary point with x,y,t all known.
I have tried to apply de Casteljau to this so:
I know that I want to find P[1][0] and P[2][0] given that I know P[0][0] and P[3][0] and P[0][3].
I understand that P[i][j] = (1-t)P[i][j-1] + (t)P[i+1][j-1]
But I cannot find a way to complete the solution chain with the givens that I have.
I have searched this and other forums for info on this but none I found gives a concrete approach to coding the solution.
Any help would be appreciated, even if it's "it can't be done without making assumptions about ..."

Related

How to find a point on a b-spline that is on the normal plane of a point on another b-spline using goemdl / nurbs

So the first problem will be explaining what I am after clearly.
I have two non-rational 3D b-splines. The first b-spline it the guiding spline. The second b-spline is a reference and it is essentially 'inside' of the first spline. ( the splines were generated in Solidworks )
Imagine a circular playground slide. The first spline is the center line of the slide. The second spline is the inside edge of the slide.
The inside spline will tend to be shorter than the center spline. The inside will also tend to have more curvature at any given point than the center.
The path of the slide is not perfectly circular. But the inside spline is always 'parallel' to the outside. ( very liberal use of the word parallel here )
What I am after:
Given a point along the center curve, I would like to find the point on the inside curve that is on the plane that is defined by the normal to the tangent of the center spline at that point.
Where I am at:
I am using the geomdl library in python to manipulate the splines.
I can choose a distance along the center spline and from geomdl I get the 3D point and the tangent vector (A,B,C) of that point and therefore the plane at that point that is normal to the spline at that point.
What I am doing:
From the tangent vector and the point I compute the equation of the plane in the form of:
Ax + By + Cz = D.
From there I guess at the point at the same distance on the inside spline and plug it into the equation for the plane that I already have. I use the error in D to guess at which way I should bump my guess on where the point on the inside curve might really be.
[ I understand that over the entire length of the two splines there may be more than one solution. i.e. if the curve wraps more than 180° there would be more than one point on the inside curve that lands on the plane defined by the center curve. In the local area that i am interested in this will not be a problem. Any second point would also be a long ways away from the center line. i.e. the correct point will be no more than 25 mm from the center point. A non-local point will be at least 3000 mm away. ]
This mostly works. But from time to time it fails. i.e. if D is very near 0 my guesses will diverge from the answer.
Currently I make 10 guesses, each guess having a smaller delta guess than the last.
I have a great number of these points to evaluate. My solution requires 10 X the number of calculations so it is not terribly efficient.
From my Google searches I believe that using the error in D in the equation of a plane may not be correct. I 'think' that D is the distance of the plane to the origin.(yes /no?) Therefore I am really comparing the distance of the two planes from the origin and not really from each other. If my guess happens to be on the "other side" of the origin then the distance's may be the same but opposite.
My Question:
What is the correct way to go about this?
Is my assumption that D is the distance from the plane to the origin?
Is driving the error in D between the two points valid?
What is the correct way to do this?
Restate my question in different terms
Given a plane (Ax + By + Cz = D) how do I find the point on a given b-spline that pierces ( or is coincident to ) that plane (using geomdl.bcurve)?
( I am very much in over my head here so please forgive if this does not make sense )

Equation for Length of Cubic Spline Between 3 Points

This is my first time posting here, so I hope this is ok. I'm working on a java project but my question is really about the math I'll be using for it...
I have three (different) points at (x1, y1), (x2, y2), and (x3, y3). All I need is a formula for the length of the cubic spline formed between them. For someone good at calculus, this should be pretty easy to derive. I've looked all around online but can't seem to find the solution. Again, I don't even need the equation of the spline - just its length, given the three points. Thanks in advance! If someone can figure this out and share, you'll makey day :)
I have some bad news.
The first is that a cubic b-spline generally takes 4 points to define. It is possible to define one from 3 points, but it usually involves making up another point somehow (for example, using degree elevation). So we'd need to have information about how exactly you're defining the spline - if it's some other kind of spline (catmull-rohm?), or the details of how you're constructing it.
The second is that there's no closed-form equation for the length of a b-spline, or even a Bezier curve. What I typically do is sample the curve at a lot of points, and then compute the length of the polyline.
There are formulas that can tell you what your error bound will be, based on the derivatives of the curve, and there are methods that approximate using arcs rather than line segments, but those are probably more complicated than they're worth.
See the primer on bezier curves for a more info. However, sadly, tfinniga is correct for cubic splines you need to use an approximation.

Not a knapsack or bin algorithm

I need to find a combination of rectangles that will maximize the use of the area of a circle. The difference between my situation and the classic problems is I have a set of rectangles I CAN use, and a subset of those rectangles I MUST use.
By way of an analogy: Think of an end of a log and list of board sizes. I can cut 2x4s, 2x6s and 2x8s and 2x10 from a log but I must cut at least two 2x4s and one 2x8.
As I understand it, my particular variation is mildly different than other packing optimizations. Thanks in advance for any insight on how I might adapt existing algorithms to solve this problem.
NCDiesel
This is actually a pretty hard problem, even with squares instead of rectangles.
Here's an idea. Approach it as an knapsack-Integer-Program, which can give you some insights into the solution. (By definition it won't give you the optimal solution.)
IP Formulation Heuristic
Say you have a total of n rectangles, r1, r2, r3, ..., rn
Let the area of each rectangle be a1, a2, a3, ..., an
Let the area of the large circle you are given be *A*
Decision Variable
Xi = 1 if rectangle i is selected. 0 otherwise.
Objective
Minimize [A - Sum_over_i (ai * Xi)]
Subject to:
Sum_over_i (ai x Xi) <= A # Area_limit constraint
Xk = 1 for each rectangle k that has to be selected
You can solve this using any solver.
Now, the reason this is a heuristic is that this solution totally ignores the arrangement of the rectangles inside the circle. It also ends up "cutting" rectangles into smaller pieces to fit inside the circle. (That is why the Area_limit constraint is a weak bound.)
Relevant Reference
This Math SE question addresses the "classic" version of it.
And you can look at the link provided as comments in there, for several clever solutions involving squares of the same size packed inside a circle.

Physics - Projectile get launch angle to hit desired location?

I am programming a simple ball projectile in a game.
the update pretty much looks like:
velocity += gravity;
velocity *=0.9;
pos += vel;
Is there a way to set the angle and power of the launch in order to hit a point that is specified with the mouse?
like peggle, http://youtu.be/4KbNiWsgJck?t=45s
I know there is a solution that I have used several years ago, but I can't find it.
I believed it turned my update into a quadratic formula, or derived it or something.
It had two solutions that was solved with the quadratic equation.
ps- hopefully this could be in 3D, but I could use a 2D solution too because my curve would be 2D
any help?
thanks,
Dan
Yes, you can do this. If you can change the angle and speed, you have more variability than you need, so you have to find a reasonable set of parameters that will work, which isn't hard. The basic equations are:
x = x0 + t*v0x
y = y0 + v0yt + (1/2)ayt2
Here, x and y will be the points you want to hit, and t will be the time that you hit them. t won't show up in the final solution, but you'll use it as in intermediary to calculate the values you want.
Basically, then, pick a reasonable value for v0x. Using the x-equation, find what t will be when the target is hit. Then plug this value into the y-equation, and solve for v0y. This then will give you a pair of values of v0x and v0y that will work to hit the target.

Determine the point of intersection of a line in the xy- plane

This is a linear algebra question which i am expected to understand before i can start tackling 2D and 3D programming. I am a business application programmer but i am exploring an interest in game programming. I realise that this maybe a simple question to some, so please bear with me.
The line L passes through the points P1 (3, -1, 2) and P2 (1, -2, -1). Determine the point of intersection of L in the xy- plane.
Okay using those two points you can find the equation of a line (google finding the equation of a line in 3d) from that point on you can equate the equation of a line and the equation of the xy-plane to figure out their intersection (google finding intersection of two planes in 3D).
You can use the z-coordinate of the line as the independent variable, and use the two points to get the formula for the x- and y- coordinates in terms of z.
First, we define the slopes:
x_slope=(x2-x1)/(z2-z1);
y_slope=(y2-y1)/(z2-z1);
Then we have that:
x-x1=x_slope*(z-z1)
and
y-y1=y_slope*(z-z1)
Setting z to 0 and solving for x and y, we get
x_plane_coord=x1-(x_slope*z1);
y_plane_coord=y1-(y_slope*z1);