{
"cells": [
{
"cell_type": "markdown",
"id": "81d76ce2",
"metadata": {},
"source": [
"# Flower\n",
"\n",
"This example uses the `rotate()` and `morph()` animations to animate a `Circle`, `fill` and `plot` plot objects.\n",
"\n",
"First, we create the pistil of the flower using a `Circle`:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "9e6d4f88",
"metadata": {},
"outputs": [],
"source": [
"import diplotocus as dpl\n",
"import numpy as np\n",
"\n",
"pistil = dpl.Circle((0,0),.5,fc=\"#ffd97d\",zorder=3)"
]
},
{
"cell_type": "markdown",
"id": "9ddbc5bb",
"metadata": {},
"source": [
"Then, we create the petals:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "cc41fbb1",
"metadata": {},
"outputs": [],
"source": [
"theta = np.linspace(0,2*np.pi,360)\n",
"r1 = 2-0.6*np.sin(-5*theta)\n",
"x1 = r1*np.cos(theta)\n",
"y1 = r1*np.sin(theta)\n",
"r2 = 2-1*np.sin(-5*theta)\n",
"x2 = r2*np.cos(theta)\n",
"y2 = r2*np.sin(theta)\n",
"\n",
"petals = dpl.fill(x1,y1,fc=\"#ff9b85\",zorder=2)"
]
},
{
"cell_type": "markdown",
"id": "954a8a2b",
"metadata": {},
"source": [
"Next, we create the stem:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "1f6c353c",
"metadata": {},
"outputs": [],
"source": [
"y = np.linspace(-5,0,100)\n",
"theta = (1+y/5)*15/180*np.pi\n",
"d = 5 + y\n",
"x3 = np.arctan( theta)*d\n",
"x4 = np.arctan(-theta)*d\n",
"stem = dpl.plot(x3,y,c='#60d394',lw=10,zorder=1)"
]
},
{
"cell_type": "markdown",
"id": "f2e8f031",
"metadata": {},
"source": [
"And finally, we create our timeline, animate our plot objects and render the video:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3cec2712",
"metadata": {},
"outputs": [],
"source": [
"tl = dpl.Timeline(xlim=(-5,5),ylim=(-5,5),noaxis=True,figsize=(7,7))\n",
"e = dpl.easeInOutQuad()\n",
"\n",
"petals.rotate(start_angle=0,end_angle=360,center=(0,0),duration=120)\n",
"petals.morph(new_x=x2,new_y=y2,duration=60)\n",
"petals.morph(new_x=x1,new_y=y1,duration=60,delay=60)\n",
"\n",
"stem.morph(new_x=x4,new_y=y,duration=60,easing=e)\n",
"stem.morph(new_x=x3,new_y=y,duration=60,easing=e,delay=60)\n",
"\n",
"pistil.rotate(start_angle=-15,end_angle=15,center=(0,-5),duration=60,easing=e)\n",
"petals.rotate(start_angle=-15,end_angle=15,center=(0,-5),duration=60,easing=e)\n",
"pistil.rotate(start_angle=0 ,end_angle=-30,center=(0,-5),duration=60,easing=e,delay=60)\n",
"petals.rotate(start_angle=0 ,end_angle=-30,center=(0,-5),duration=60,easing=e,delay=60)\n",
"\n",
"tl.animate((pistil,petals,stem))\n",
"tl.save_video('../../_static/examples/flower.mp4')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "9fd99211",
"metadata": {
"tags": [
"remove-input"
]
},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from IPython.display import HTML, display\n",
"display(HTML(\"\"\"\n",
" \n",
"\"\"\"))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}