[PYTHON] Try to draw a Bezier curve

Introduction

I was studying shape processing, but I couldn't get it right just by reading a book, so I decided to try programming.

What is a Bezier curve?

A Bezier curve is a curve shape created in a control polygon formed by connecting control points $ P_i $. One point that composes the Bezier curve is defined by the control points and parameters, and when they are connected, the Bezier curve as shown in the figure below can be drawn. The blue point is the control point. image.png

Bezier curve calculation

This time, we are targeting Bezier curves drawn in two-dimensional space. Therefore, the component of the coordinate value is $ (X_i, Y_i) $.

First, the Bezier curve is defined by the following formula. With this formula, once $ t $ is determined, the point $ P (t) $ on the Bezier curve can be calculated. Calculate the point $ P (t) $ by changing $ t $ in the range of 0 to 1, and connect them to form a Bezier curve.

P(t) = \sum_{i=0}^{N}B_{N,i}(t)Q_i \tag{1}
B_{n,i}(t) = \begin{pmatrix}n\\i\end{pmatrix}t^i(1-t)^{n-i} \tag{2}

There are various symbols such as $ t $ and $ N $, but the meaning of each is as follows.

--Binomial coefficient The coefficient part calculated on the right side of $ B_ {n, i} (t) $ is called the binomial coefficient, and it is calculated by the following formula.

\begin{pmatrix} n\\i \end{pmatrix} = {_n\mathrm{C}_k=\frac{n!}{k!(n-k)!} } \tag{3}

Also, $ (2) $ is called a Bernstein polynomial.

program

The implemented python script is as follows.

--Bezier curve calculation part

#Binomial coefficient calculation
def BiCoe(n, k):
    if n < k :
        return -1
    return math.factorial(n) / (math.factorial(k) * math.factorial((n - k)))

#Bernstein polynomial
def Bernstein(n, i, t):
    return BiCoe(n, i) * np.power((1-t), (n-i)) * np.power(t, i)

#Bezier curve
def BezierCurve(points, t):
    Gt = 0
    n = len(points) - 1
    for k, point in enumerate(points):
        Gt += point * Bernstein(n, k, t)
            
    return Gt

--Bezier curve drawing part

#Draw Bezier curve
def DrawBezierCurve(points):
    x = np.arange(0, 1, 0.01, dtype=np.float32)
    x = np.append(x, 1.0)
    gt = [BezierCurve(points, t) for t in x]
    gt_x = [g[0] for g in gt]
    gt_y = [g[1] for g in gt]
    ct_x = [ct[0] for ct in points]
    ct_y = [ct[1] for ct in points]
    
    plt.plot(ct_x, ct_y, linestyle='dashed', linewidth=1)
    plt.plot(gt_x, gt_y, linewidth = 3)
    plt.scatter(ct_x, ct_y)

in conclusion

When I was reading the book, I thought it was stupid to say, "Can I draw a curve with this formula?" I think it has deepened. As a mathematics enthusiast, it took me a long time to research and understand a little bit. .. .. For example, it took about 30 minutes to know that the coefficient part is a binomial coefficient by looking at the formula of $ (2) $. (LOL) I will continue to study hard.

References

-[Introduction to 3D shape processing](https://www.amazon.co.jp/3%E6%AC%A1%E5%85%83%E5%BD%A2%E7%8A%B6%E5%87 % A6% E7% 90% 86% E5% 85% A5% E9% 96% 80% E2% 80% 953% E6% AC% A1% E5% 85% 83CG% E3% 81% A8CAD% E3% 81% B8 % E3% 81% AE% E5% 9F% BA% E7% A4% 8E-Information-Computing-% E4% BB% 8A% E9% 87% 8E-% E6% 99% 83% E5% B8% 82 / dp / 4781910483)

Recommended Posts

Try to draw a Bezier curve
Try to draw a life curve with python
Try to select a language
How to draw a graph using Matplotlib
Try to make a kernel of Jupyter
Try to calculate a statistical problem in Python
How to draw a 2-axis graph with pyplot
How to draw a 3D graph before optimization
I wanted to play with the Bezier curve
Try to make a "cryptanalysis" cipher with Python
Sample to draw a simple clock using ebiten
Try to create a new command on linux
Try to make a dihedral group with Python
[Python] How to draw a histogram in Matplotlib
Try to write a ping confirmation script appropriately
Try to draw a "weather map-like front" by machine learning based on weather data (5)
Try to draw a "weather map-like front" by machine learning based on weather data (3)
Try to draw a "weather map-like front" by machine learning based on weather data (1)
Try to draw a "weather map-like front" by machine learning based on weather data (4)
Try to draw a "weather map-like front" by machine learning based on weather data (2)
Try to make a Python module in C language
[Python] How to draw a line graph with Matplotlib
Try to make a command standby tool with python
Try to dynamically create a Checkbutton with Python's Tkinter
(Python) Try to develop a web application using Django
I tried to draw a route map with Python
[GCP] Try a sample to authenticate users with Firebase
Try to implement yolact
A sample to try Factorization Machines quickly with fastFM
Machine learning beginners try to make a decision tree
[Python] How to draw a scatter plot with Matplotlib
Derivatives learned using Python-(2) Draw a yield curve (JPYLibor curve)-
I tried to draw a configuration diagram using Diagrams
I want to find the intersection of a Bezier curve and a straight line (Bezier Clipping method)
[Go language] Try to create a uselessly multi-threaded line counter
Try to build a deep learning / neural network with scratch
Try to bring up a subwindow with PyQt5 and Python
Try to model a multimodal distribution using the EM algorithm
[Visualization] I want to draw a beautiful graph with Plotly
[Introduction to Tensorflow] Understand Tensorflow properly and try to make a model
Just try to receive a webhook in ngrok and python
A quick introduction to pytest-mock
Try to factorial with recursion
Draw Bezier curves with Go
Draw a graph with NetworkX
A road to intermediate Python
A super introduction to Linux
Try to analyze Twitter trends
Try programming with a shell!
Try to understand Python self
Add a dictionary to MeCab
How to call a function
Upload a file to Dropbox
Send a signal to subprocess
Try creating a CRUD function
Let's try a shell script
How to hack a terminal
Let's draw a logistic function
Draw a heart in Python
Draw a graph with networkx
[C language] [Linux] Try to create a simple Linux command * Just add! !!