본문 바로가기

python/Matplotlib

두 함수의 교차점

import matplotlib.pyplot as plt
import numpy as np

m1, b1 = 1.0, 0.0 # slope & intercept (line 1)
m2, b2 = -1.0, 8.0 # slope & intercept (line 2)

x = np.linspace(0,10,100)

plt.plot(x,x*m1+b1)
plt.plot(x,x*m2+b2)
plt.xlim(0,8)
plt.ylim(0,8)


# 접점
x0 = (b2-b1) / (m1-m2)
y0 = m1 * x0 + b1
plt.scatter(x0,y0, color='black' )

 

plt.show()

 

import matplotlib.pyplot as plt
import numpy as np

m1, b1 = 1.0, 0.0 # slope & intercept (line 1)
m2, b2 = -1.0, 8.0 # slope & intercept (line 2)

#m1, b1 = 0.1, 2.0 # slope & intercept (line 1)
#m2, b2 = 2.0, -3.0 # slope & intercept (line 2)
x = np.linspace(0,10,500)

plt.plot(x,x*m1+b1)
plt.plot(x,x*m2+b2)
plt.xlim(0,8)
plt.ylim(0,8)

# 접점
x0 = (b2-b1) / (m1-m2)
y0 = m1 * x0 + b1
plt.scatter(x0,y0, color='black' )

#원
theta = np.linspace(0, 2*np.pi, 100)

r = np.sqrt(4.0) # circle radius

x1 = r * np.cos(theta) + x0
x2 = r * np.sin(theta) + y0
plt.plot(x1, x2, color='gray')

#접점

x_list = []
y_list = []

def line_and_circle_intersection_points(m,b,x0,y0,r):

    c1 = 1 + m ** 2
    c2 = - 2.0 * x0 + 2 * m * ( b - y0 )
    c3 = x0 ** 2 + ( b - y0 ) ** 2 - r ** 2

    # solve the quadratic equation:

    delta = c2 ** 2 - 4.0 * c1 * c3

    x1 = ( - c2 + np.sqrt(delta) ) / ( 2.0 * c1 )
    x2 = ( - c2 - np.sqrt(delta) ) / ( 2.0 * c1 )

    x_list.append(x1)
    x_list.append(x2)

    y1 = m * x1 + b
    y2 = m * x2 + b

    y_list.append(y1)
    y_list.append(y2)

    return None

line_and_circle_intersection_points(m1,b1,x0,y0,r)

plt.scatter( x_list[0], y_list[0], color='black' )
plt.scatter( x_list[1], y_list[1], color='black' )

plt.text( x_list[0], y_list[0], 'P1', color='black' )
plt.text( x_list[1], y_list[1], 'P2', color='black' )

line_and_circle_intersection_points(m2,b2,x0,y0,r)

plt.scatter( x_list[2], y_list[2], color='black' )
plt.scatter( x_list[3], y_list[3], color='black' )

plt.text( x_list[2], y_list[2], 'P3', color='black' )
plt.text( x_list[3], y_list[3], 'P4', color='black' )
plt.show()

 

 

'python > Matplotlib' 카테고리의 다른 글

exp 그래프  (0) 2021.07.25
접점  (0) 2021.07.25
1차함수  (0) 2021.07.25
Plotting with categorical variables  (0) 2021.07.19
Plotting with keyword strings  (0) 2021.07.19