last day (14 days later) » 

6:04 AM
2
A: Get points from a UIBezierPath

fangIt should be possible to get points along a Bezier path as addQuadCurveToPoint is to add a quadratic Bezier segment into the path. So, the three control points of your quadratic Bezier curve are (refer to the code piece in original post) P(0) = origin P(1) = (midpt1.x, midpt1.y+50) P(2) = (mi...

 
Hey @fang, I am still trying to understand your answer. Let me ask you questions if I get stuck. Just to confirm, this is a way to get the points that lie on the graph, right? I just updated my question to better explain my need.
 
Yes. Evaluating the quadratic Bezier curve is the way to get points on the curve unless addQuadCurveToPoint() does not create quadratic Bezier curve as it claims to be.
 
That is just perfect. Let me do the math and see if it works for me!
I think I get it. Once you've found the t-value, you set Y = (1-t)^2*Y0 + 2*t*(1-t)*Y1 + t^2 *Y2 !
I just computed a bunch of tValues and sometimes the value is not [0, 1]. For example, if X = 358.0, X0 = 0.0, X1 = 179.0, X2 = 364.0, t = 1.88 or -31.71. What should I do in this case? I updated the question.
 
You should at least get a root that is within [0, 1] if X value is within [X0, X2]. In your example, X=358, which is very close to X2 (364), so the t value should be very close to 1. Actual computation shows that the two roots for t are t=0.9837796 and t=-60.650446 and you choose t=0.9837796, which is very close to 1.
 
Thanks, I guess that means my equation that I solved for t is wrong? This is my gist: gist.github.com/woniesong92/c7f79c812f1ae8be21f5 and I am not sure which part is wrong. How did you solve for t? I wrote down my equation for t in my question. Did I make a mistake?
Like you said, I think my equation for t is wrong. I am checking on that.
 
6:04 AM
I think the (2.0*x0-x1) should be 2.0*(x0-x1). Also, the -2.0*x0+x1 should be -2.0*(x0-x1).
 
Hi fang are you able to chat?
These are some of the data:
2014-11-13 01:05:40.154 CalendarPlus[25035:965452] Touch location: {70.5, 522.5}
2014-11-13 01:05:40.154 CalendarPlus[25035:965452] X1: 35.250000
2014-11-13 01:05:40.154 CalendarPlus[25035:965452] X2: 220.250000
2014-11-13 01:05:40.154 CalendarPlus[25035:965452] tVal: 0.490000
2014-11-13 01:05:40.154 CalendarPlus[25035:965452] Y_real: 522.500000
2014-11-13 01:05:40.155 CalendarPlus[25035:965452] Y_predicted: 459.662964
2014-11-13 01:05:40.834 CalendarPlus[25035:965452] Touch location: {117.5, 522.5}
 
Sure
 
So it performs a lot better than before but Y_real is still different from Y_predicted
(thank you for spending your time with me. you're awesome)
Do you have an idea?
So I draw a Bezier path everytime I touch the screen, and I draw it based on the point that is touched
Y_real is the y-coordinate of the maximum of the graph (and this is where the screen is touched)
one thing I noticed was Y_predicted increases as I increase X, but it should really stay the same if Y_real stays the same
 
The math portion in my post is correct. However, I am not sure the three control points are indeed those three points in your object-C program. You have to figure out this. If you can draw the 3 points on the graph, it might help to identify what the problem is.
 
I see. I have one question
Do you think the graph that I draw is actually a combination of two bezier paths?
Because that's precisely what I am doing. In that case, should I change the math formula?
In a better way, what's the formula for C(t) when there's only one control point?
 
6:15 AM
It is possible. There is a G1 discontinuity at where y attain the maximum, which indicates that is the joint between two Bezier curves. Originally I thought your program only show codes for drawing the first Bezier curve.
 
I see!
Let me try to find the t-values separately.
 
addQuadCurveToPoint could be treating the third control points the same as the 2nd one when you only add one point. So, it is likely the 3 control points for your first Bezier segments are origin, (midpt1.x, midpt1.y+50), and (midpt1.x, midpt1.y+50) .
 
This is what I am trying:
float tVal1 = [self getTvalFromBezierPath:0.0 x1Val:ctrlpt1.x x2Val:location.x];
float tVal2 = [self getTvalFromBezierPath:location.x x1Val:ctrlpt2.x x2Val:endpt.x];
NSLog(@"tVal1: %f", tVal1);
NSLog(@"tVal1: %f", tVal2);
float yVal1 = [self getCoordFromBezierPath:tVal1 origin:0.0 p1Val:ctrlpt1.y p2Val:location.y];
float yVal2 = [self getCoordFromBezierPath:tVal2 origin:location.y p1Val:ctrlpt2.y p2Val:endpt.y];
 
I am not very familiar with iOS programming. So, the first thing to make sure is in your codes [path addQuadCurveToPoint:location controlPoint:CGPointMake(midpt1.x, midpt1.y+50)]
Sorry. Havn't finished the typing....
So, my question is in that line of codes how many points are added? 1 point or 2 points?
 
For each addQuadCurveToPoint function
three points are used
start point, control point, end point
 
6:30 AM
OK. In that case, the first Bezier segment is defined by 'origin', 'location' and '(midpt1.x, midpt1.y+50)'. Is this correct?
 
yes
 
Thanks for the pictures. I am pretty familiar with Bezier curve and all the related math. I am just not familiar with the syntax of addQuadCurveToPoint.
 
and the second curve will be defined by location, (midpt2.x, midpt2.y+50), 'endpoint'
:)
so in this case,
 
OK. Then, my original post about the 3 control points are wrong. You should not follow my post about the 3 control points. However, the rest of the math is still correct
 
I see
So for the first Bezier segment
where the three points are defined by 'origin', 'location' and '(midpt1.x, midpt1.y+50)'.
you can find the t-value by doing X = (1-t)^2X0 + 2t(1-t)X1 + t^2X2
in this case, what would be the value of X ?
 
6:34 AM
The value of X should still be the value at which you would like to find Y
 
oh
I see.
then what about X2?
 
X0, X1 and X2 are X coordinate of the 3 control points in their order.
 
But we only have 1 control point for the first Bezier segment
Or do you mean X0 = Current point, X1 = Control point, X2 = endpoint using the labels in the picture above?
So for the first segment, X0 = origin.x, X1 = midpt1.x, X2 = location.x ?
 
A quadratic Bezier segment has 3 control points. Many software will claim they only have 1 control point, which is misleading as they mark the 1st and last control points as start point and end point.
 
I see
 
6:38 AM
So, yes X0 = current point and X2= end points and X1 will be your so-called control point.
 
So for the first segment, X0 = origin.x, X1 = midpt1.x, X2 = location.x, X = location.x ?
and second segment, X0 = location.x, X1= midpt2.x, X2 = endpoint.x, X = location.x ?
 
X should not be location.x. It should be whatever x value at which you would like to find its y value.
 
yes, I would like to find the y-value of the location for now
so that it's easier for me to test whether the predicted value is same as the real value
because I already know the coordinates for "location"
does that make sense?
 
Yes, if you set X=location.x, you should find corresponding Y = location.y. This is a good way to prove the codes is correct.
 
Could you wait a couple of minutes while I try to fix my code? :) are you in a rush?
 
6:42 AM
No. I can wait.
 
I have a question
t = (sqrt((-2.0 * x * x1) + (x * x0) + (x * x2) + pow(x1, 2) - (x0 * x2)) + x0 - x1) / (x0 - (2.0 * x1) + x2)
x: 224.000000, x0: 0.000000, x1: 112.000000, x2: 224.000000
then the denominator evaluates to 0
because (2*x1) = x2
Do you see the problem?
And it will always be that way because X1 is just the midpoint between X0 and X2
so I am confused again :(
 
Did you still use your original formula for solving t? Your original formula is not correct. Please see my comment in the post.
 
oh, hold on
I used a corrected version of formula, but I think the problem still holds?
 
If you happen to get x0 - 2*x1+x2 = 0, that means you get a linear equation or t. Then, you should not use the quadratic formula.
 
hm....
But the graph is clearly not linear though
 
6:55 AM
It does not matter. We are solving for t only. Y value is still quadratic.
 
I see
For me, (x0 - (2.0 * x1) + x2) always evaluates to 0
How should I change the equation?
(always evals to 0 because x1 is the midpoint between x0 and x2)
tVal = (sqrt((-2.0 * x * x1) + (x * x0) + (x * x2) + pow(x1, 2) - (x0 * x2)) + x0 - x1) / (x0 - (2.0 * x1) + x2);
Also I appreciate your help so much.. thanks for being patient with me.
 
In general the quadratic equation to solve for t is (x0-2*x1+x2)*t^2 + 2*(x1-x0)*t + (x0-x) = 0. If the coefficient for t^2 is not zero, then you can use the quadratic formula. But if the coefficient for t^2 is zero, then t is simply (x-x0)/(2*(x1-x0)).
 
wait.. omg I think it worked.
I see. Given ax^2 + bx + c = 0
a = (2.0 * x1) + x2) for us, and since it always evals to 0
We can just solve for bx + c = 0
 
Yes
 
Wow!
Thanks so much!
Where does the equation C(t) = (1-t)^2*P(0) + 2*t*(1-t)*P(1) + t^2 *P(2) come from?
 
7:03 AM
OK. I am going to end the chat and go back to revise my post about those 3 control points.
That is just the definitions of quadratic Bezier curve.
 
Okay. Thank you @fang
 
Bye
 
Thank you!!!!!
 

last day (14 days later) »