import numpy as np
import matplotlib.pyplot as plt
import random
# --- Random slopes ---
m1 = random.randint(1, 15) # positive slope
m2 = random.randint(1, 15) # negative slope
print(f"Line A (through 0,0) slope: {m1}")
print(f"Line B (through 1,0) slope: {m2}")
# --- Define the two lines ---
def lineA(x):
return m1 * x # passes through (0,0)
def lineB(x):
return m2 * (x - 1) # passes through (1,0)
# --- Intersection with x-axis ---
xA = 0
xB = 1
# --- Intersection between A and B ---
xI = -m2 / (m1 - m2)
yI = lineA(xI)
# --- LERP curve on x ∈ [0,1] ---
t = np.linspace(0, 1, 400)
# linear interpolation between lineA(t) and lineB(t)
curve = (1 - t) * lineA(t) + t * lineB(t)
# --- Plotting window ---
xmin = min(xA, xB, xI) - 1
xmax = max(xA, xB, xI) + 1
x_plot = np.linspace(xmin, xmax, 400)
plt.figure(figsize=(9,7))
# Lines
plt.plot(x_plot, lineA(x_plot), label=f"Line A: y = {m1}x")
plt.plot(x_plot, lineB(x_plot), label=f"Line B: y = {m2}(x - 1)")
# LERP curve
plt.plot(t, curve, color='purple', linewidth=3,
label="LERP between lineA and lineB on [0,1]")
# Triangle
plt.fill([xA, xB, xI], [0, 0, yI], alpha=0.2, color='orange', label="Triangle")
plt.scatter([xA, xB, xI], [0, 0, yI], color='red')
plt.axhline(0, color='black', linewidth=0.8)
plt.axvline(0, color='black', linewidth=0.8)
plt.xlabel("x")
plt.ylabel("y")
plt.title("Two Random Lines + LERP Between Lines on [0,1]")
plt.legend()
plt.grid(True)
plt.show()