algorithm - How to compute the intersection between a quadratic bezier curve and a horizontal line? -
what trying x coordinates @ bezier curve crosses horizontal line (a y coordinate). moment, have code:
function self.getx(y) if y > maxy or y < miny return end local = y1 - y if == 0 return end local b = 2*(y2 - y1) local c = (y3 - 2*y2 + y1) local discriminant = (b^2 - 4*a*c ) if discriminant < 0 return else local abytwo = 2*a if discriminant == 0 local index1 = -b/abytwo if 0 < index1 , index1 < 1 return (1-index1)^2*x1+2*(1-index1)*index1*x2+index1^2*x3 end else local thesqrt = math.sqrt(discriminant) local index1, index2 = (-b -thesqrt)/abytwo, (-b +thesqrt)/abytwo if 0 < index1 , index1 < 1 if 0 < index2 , index2 < 1 return (1-index1)^2*x1+2*(1-index1)*index1*x2+index1^2*x3, (1-index2)^2*x1+2*(1-index2)*index2*x2+index2^2*x3 else return (1-index1)^2*x1+2*(1-index1)*index1*x2+index1^2*x3 end elseif 0 < index2 , index2 < 1 return (1-index2)^2*x1+2*(1-index2)*index2*x2+index2^2*x3 end end end end
a few specifications:
- this lua code.
- local means variable local chunk of code, not affect code's functionality.
- y1, y2, , y3 y coordinate of 3 points. same applies x1, x2, x3.
- y y coordinate of horizontal line computing.
- maxy biggest of 3 y's.
- miny smallest.
for moment code gives me this:
- there 8 bezier curves
- the green ones generated using normal method:
(1-t)^2*x1+2*(1-t)*t*x2+t^2*x3
- the red dots control points.
- the white lines generated using method described code above.
- the straight lines lines, ignore them.
- there should 8 curves, 4 rendered.
thanks in advance,
creator!
the bézier curve has
y(t)=(1-t)^2*y1+2(1-t)*t*y2+t^2*y
which expands to
(y1-2*y2+y3)*t^2+2(y2-y1)*t+y1
you have swapped a
, c
in quadratic equation a*t^2+b*t+c=0
required solving y(t)=y
.
Comments
Post a Comment