Project Euler 363

Project Euler 363

题目

Bézier Curves

A cubic Bézier curve is defined by four points: P0,P1,P2, and P3.

The curve is constructed as follows:

On the segments P0P1, P1P2, and P2P3 the points Q0,Q1, and Q2 are drawn such that P0Q0P0P1=P1Q1P1P2=P2Q2P2P3=t, with t in [0,1].

On the segments Q0Q1 and Q1Q2 the points R0 and R1 are drawn such that

Q0R0Q0Q1=Q1R1Q1Q2=t for the same value of t.

On the segment R0R1 the point B is drawn such that R0BR0R1=t for the same value of t.

The Bézier curve defined by the points P0,P1,P2,P3 is the locus of B as Q0 takes all possible positions on the segment P0P1. (Please note that for all points the value of t is the same.)

From the construction it is clear that the Bézier curve will be tangent to the segments P0P1 in P0 and P2P3 in P3.

A cubic Bézier curve with P0=(1,0),P1=(1,v),P2=(v,1), and P3=(0,1) is used to approximate a quarter circle.

The value v>0 is chosen such that the area enclosed by the lines OP0,OP3 and the curve is equal to π4 (the area of the quarter circle).

By how many percent does the length of the curve differ from the length of the quarter circle?

That is, if L is the length of the curve, calculate 100×Lπ2π2

Give your answer rounded to 10 digits behind the decimal point.

解决方案

这个页面给出了这条三次贝塞尔曲线的参数方程:

B(t)=P0(1t)3+3P1t(1t)2+3P2t2(1t)+P3t3,t[0,1]

那么化简后,得到关于 x,y 的参数方程:

x(t)=(1t)3+3t(1t)2+3t2(1t)vy(t)=3t(1t)2v+3t2(1t)+t3

第一个问题:如何求贝塞尔曲线 B 和线段 C:P3O,D:OP0) 共同围绕而成的封闭曲线的面积?

使用补线法进行计算,可以列出公式为:

π4=S=12B+C+Dxdyydx

注意线段 B 和线段 C 的积分值都为 0,那么只需要计算 12Bxdyydx 即可。

12(1+6v53v210)=π4

解这个一元二次方程,得到 v=2±13(225π)

由于按照题目的需要,它是一个 14 圆的近似,因此取 v=213(225π)

第二个问题:如何求曲线的长度?

dP0P3=01x2(t)+y2(t)dt

通过 Mathematica 直接计算出具体值即可。

代码

1
2
3
4
5
x[t_] = (1 - t)^3 + 3 t (1 - t)^2 + 3 t^2 (1 - t) v
y[t_] = 3 t (1 - t)^2 v + 3 t^2 (1 - t) + t^3
sol = First[Solve[1/2 (Integrate[x[t] y'[t] - y[t] x'[t], {t, 0, 1}]) == Pi/4, v]]
L = NIntegrate[Sqrt[x'[t]^2 + y'[t]^2 /. sol], {t, 0, 1}]
ans = 100*(L - Pi/2)/(Pi/2)
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
Ujimatsu Chiya 微信 微信
Ujimatsu Chiya 支付宝 支付宝