Last active
January 4, 2024 10:16
-
-
Save pranithan-kang/c1158d762f45a8ded60ff1ace306e0d4 to your computer and use it in GitHub Desktop.
วาดรูปวงกลม วงรี และรูปไข่
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# %% | |
from sympy import plot_implicit, symbols, Eq | |
from matplotlib import pyplot as plt | |
import numpy as np | |
x, y = symbols("x y") | |
def plot(equation, x_range, y_range, width=5, height=5, show=True): | |
p = plot_implicit( | |
equation, | |
x_var=(x, -x_range, x_range), | |
y_var=(y, -y_range, y_range), | |
size=(width, height), | |
show=show, | |
) | |
return p | |
# %% [markdown] | |
# | |
# สมการวงกลม | |
# $$ (x-a)^2 + (y-b)^2 = r $$ | |
# | |
# โดยที่: | |
# - จุดศูนย์กลางของวงกลมคือ $(a, b)$ | |
# - r คือความยาวรัศมี | |
# %% | |
def circle_equation(x, y, a, b, r): | |
return Eq((x - a) ** 2 + (y - b) ** 2, r) | |
a = 2 | |
b = 4 | |
r = 3 | |
plot(circle_equation(x, y, a, b, r), x_range=10, y_range=10) | |
# %% [markdown] | |
# | |
# สมการวงรี | |
# $$ (\frac{x-h}{a})^2 + (\frac{y-k}{b})^2 = s $$ | |
# | |
# โดยที่: | |
# - จุด (h, k) คือจุดศูนย์กลางของวงรี | |
# - จุด $(h-a, k)$ และ จุด $(a+h, k)$ คือ จุดปลายของวงรีตามแนวนอนทางซ้ายและขวาตามลำดับ | |
# - จุด $(k-b, h)$ และ จุด $(b+k, h)$ คือ จุดปลายของวงรีตามแนวตั้งด้านล่างและด้านบนตามลำดับ | |
# %% | |
def ellipse_equation(x, y, h, k, a, b, s=1): | |
return Eq(((x - h) / a) ** 2 + ((y - k) / b) ** 2, s) | |
h = 2 | |
k = 5 | |
a = 3 | |
b = 2 | |
s = 1 | |
p = plot(ellipse_equation(x, y, h, k, a, b, s), x_range=10, y_range=10, show=False) | |
p.markers = [{"args": [(h - a, a + h, h, h), (k, k, k - b, b + k), "r*"]}] | |
p.markers += [{"args": [h, k, "g+"]}] | |
p.show() | |
# %% [markdown] | |
# | |
# สมการรูปไข่ | |
# $$ (\frac{x-h}{a})^2 + ((\frac{y-k}{b})^2 \times t(x)) = s $$ | |
# | |
# โดยที่: | |
# - จุด (h, k) คือจุดศูนย์กลางของรูปไข่ | |
# - จุด $(h-a, k)$ และ จุด $(a+h, k)$ คือ จุดปลายของรูปไข่ตามแนวนอนทางซ้ายและขวาตามลำดับ | |
# - $t(x)$ คือฟังก์ชั่นที่คูณกับพจน์ y เพื่อบิดให้กลายเป็นรูปไข่ ตัวอย่างเช่น $t(x) = 1 + 0.2(x)$ | |
# %% | |
def egg_equation(x, y, h, k, a, b, s=1): | |
t_x = (1 + 0.2 * x) | |
return Eq(((x - h) / a) ** 2 + ((y - k) / b) ** 2 * t_x, s) | |
h = 4 | |
k = 5 | |
a = 3 | |
b = 2.8 | |
s = 1 | |
p = plot(egg_equation(x, y, h, k, a, b, s), x_range=10, y_range=10, show=False) | |
p.markers = [{"args": [(h - a, a + h), (k, k), "r*"]}] | |
p.markers += [{"args": [h, k, "g+"]}] | |
p.show() | |
# %% | |
p = plot(egg_equation(y, x, h, k, a, b, s), x_range=10, y_range=10, show=False) | |
p.markers = [{"args": [(k, k), (h - a, a + h), "r*"]}] | |
p.markers += [{"args": [k, h, "g+"]}] | |
p.show() | |
# %% [markdown] | |
# อ้างอิง: | |
# - https://www.mathematische-basteleien.de/eggcurves.htm | |
# - https://openstax.org/books/college-algebra-2e/pages/8-1-the-ellipse | |
# - https://www.geeksforgeeks.org/plot-mathematical-expressions-in-python-using-matplotlib | |
# - https://stackoverflow.com/questions/29582089/how-to-plot-an-ellipse-by-its-equation-on-python | |
# - https://stackoverflow.com/questions/2484527/is-it-possible-to-plot-implicit-equations | |
# - https://stackoverflow.com/questions/33174301/changing-figure-size-in-sympy-mpmath-plot |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
contourpy==1.1.0 | |
cycler==0.11.0 | |
fonttools==4.40.0 | |
kiwisolver==1.4.4 | |
matplotlib==3.7.1 | |
numpy==1.25.0 | |
packaging==23.1 | |
Pillow==9.5.0 | |
pyparsing==3.1.0 | |
python-dateutil==2.8.2 | |
six==1.16.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment