天天看点

python如何画贝塞尔曲线_如何使用Python的PIL绘制贝塞尔曲线?

python如何画贝塞尔曲线_如何使用Python的PIL绘制贝塞尔曲线?

I'm using Python's Imaging Library and I would like to draw some bezier curves.

I guess I could calculate pixel by pixel but I'm hoping there is something simpler.

解决方案

A bezier curve isn't that hard to draw yourself. Given three points A, B, C you require three linear interpolations in order to draw the curve. We use the scalar t as the parameter for the linear interpolation:

P0 = A * t + (1 - t) * B

P1 = B * t + (1 - t) * C

This interpolates between two edges we've created, edge AB and edge BC. The only thing we now have to do to calculate the point we have to draw is interpolate between P0 and P1 using the same t like so:

Pfinal = P0 * t + (1 - t) * P1

There are a couple of things that need to be done before we actually draw the curve. First off we have will walk some dt (delta t) and we need to be aware that 0 <= t <= 1. As you might be able to imagine, this will not give us a smooth curve, instead it yields only a discrete set of positions at which to plot. The easiest way to solve this is to simply draw a line between the current point and the previous point.