如何使用Python的PIL绘制贝塞尔曲线? [英] How can I draw a bezier curve using Python's PIL?

查看:824
本文介绍了如何使用Python的PIL绘制贝塞尔曲线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python的图像库,我想绘制一些贝塞尔曲线. 我想我可以逐像素计算,但我希望有一些更简单的方法.

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.

推荐答案

绘制贝塞尔曲线并不难.给定三个点ABC,您需要三个线性插值才能绘制曲线.我们将标量t用作线性插值的参数:

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

这将在我们创建的两个边(AB边和BC边)之间进行插值.现在我们唯一需要计算的点就是使用相同的t在P0和P1之间进行插值,如下所示:

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

在实际绘制曲线之前,需要完成几件事.首先,我们要走一些dt(增量t),我们需要知道0 <= t <= 1.如您所料,这不会给我们平滑的曲线,而是仅产生一组离散的绘图位置.解决此问题的最简单方法是在当前点和上一个点之间画一条线.

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.

这篇关于如何使用Python的PIL绘制贝塞尔曲线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆