import numpy as np
import matplotlib.pyplot as plt
import random

def vector_3d(ax, s_p, vec, color = "red", length = 1, arrow_length_ratio=0.05):
    ax.quiver(s_p[0], s_p[1], s_p[2], vec[0], vec[1], vec[2],
              color = color, length = length,
              arrow_length_ratio=arrow_length_ratio)

def descartes_3d(ax, ran_x, ran_y, ran_z, ax_title, x_label = "x", y_label = "y", z_label="z"):
    ax.set_xlabel(x_label, fontsize = 12)
    ax.set_ylabel(y_label, fontsize = 12)
    ax.set_zlabel(z_label, fontsize = 12)
    ax.set_xlim(ran_x[0], ran_x[1])
    ax.set_ylim(ran_y[0], ran_y[1])
    ax.set_zlim(ran_z[0], ran_z[1])
    ax.set_title(ax_title, fontsize = 16)
    ax.grid()

fig = plt.figure(figsize = (10, 10))
ax = fig.add_subplot(111, projection='3d')
title1 = "Kyoto University 2024 Math qu.3 NO1_2"

descartes_3d(ax, [-1, 3], [-1, 3], [-1, 3], title1)

point_O = np.array([0, 0, 0])
point_A = np.array([2, 0, 0])
point_B = np.array([2,2,2])
point_C = np.array([0,2,0])
point_P = np.array([1,0,0])
point_Q = np.array([2,1,1])

ax.text(0,0,0,"O",fontsize="xx-large")
ax.text(2,0,0,"A",fontsize="xx-large")
ax.text(2,2,2,"B",fontsize="xx-large")
ax.text(0,2,0,"C",fontsize="xx-large")
ax.text(1,0,0,"P",fontsize="xx-large")
ax.text(2,1,1,"Q",fontsize="xx-large")

vector_3d(ax, point_O, point_A, "red",1,0.05)
vector_3d(ax, point_O, point_B, "red",1,0.05)
vector_3d(ax, point_O, point_C, "red",1,0.05)
vector_3d(ax, point_A, point_B-point_A, "red",1,0.05)
n=20
dd=2.5
for x in np.linspace(0,1,n):
     for y in np.linspace(0,1,n):
         if x == y:
             point_X=x*point_C
             point_Y=(1-y)*point_B+y*point_C
             r=random.random()
             g=random.random()
             b=random.random()

             vector_3d(ax,point_Q,point_Y-point_Q,color=(r,g,b),length=dd)
             vector_3d(ax,point_P,point_X-point_P,color=(r,g,b),length=dd)
plt.show()