import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

# Dados das balizas do casco quinado 7m de Tino Neto
data = [
    [1, 0.0, 0.830, 0.770, 0.910, 0.045, 0.0],
    [2, 1.0, 0.960, 0.890, 0.900, 0.050, 0.0],
    [3, 2.0, 1.050, 0.975, 0.895, 0.060, 0.0],
    [4, 3.0, 1.075, 0.990, 0.895, 0.070, 0.010],
    [5, 4.0, 1.030, 0.920, 0.905, 0.110, 0.040],
    [6, 5.0, 0.880, 0.740, 0.925, 0.190, 0.120],
    [7, 6.0, 0.580, 0.440, 0.955, 0.380, 0.280],
    [8, 7.0, 0.000, 0.000, 1.000, 1.000, 1.000],
]

fig = plt.figure(figsize=(15, 10), dpi=220)

# 1. Vista Perspectiva 3D
ax1 = fig.add_subplot(221, projection='3d')
for row in data:
    stn, x, sh_hb, ch_hb, sh_ht, ch_ht, k_ht = row
    ys = [-sh_hb, -ch_hb, 0.0, ch_hb, sh_hb]
    zs = [sh_ht, ch_ht, k_ht, ch_ht, sh_ht]
    xs = [x, x, x, x, x]
    ax1.plot(xs, ys, zs, color='#1F4E79', linewidth=1.5, alpha=0.85)

xs = [r[1] for r in data]
ax1.plot(xs, [-r[2] for r in data], [r[4] for r in data], color='#C00000', linewidth=2.0, label='Borda (Sheer)')
ax1.plot(xs, [r[2] for r in data], [r[4] for r in data], color='#C00000', linewidth=2.0)
ax1.plot(xs, [-r[3] for r in data], [r[5] for r in data], color='#2E75B6', linewidth=2.0, label='Chine (Quina)')
ax1.plot(xs, [r[3] for r in data], [r[5] for r in data], color='#2E75B6', linewidth=2.0)
ax1.plot(xs, [0.0 for _ in data], [r[6] for r in data], color='#385723', linewidth=2.2, label='Quilha')
ax1.set_title('Vista Perspectiva 3D', fontsize=11, fontweight='bold', color='#1F3864')
ax1.set_xlabel('X (m)')
ax1.set_ylabel('Y (m)')
ax1.set_zlabel('Z (m)')
ax1.view_init(elev=20, azim=-60)
ax1.legend(loc='upper right', fontsize=8)

# 2. Vista do Corpo de Balizas (Seção Transversal)
ax2 = fig.add_subplot(222)
for row in data:
    stn, x, sh_hb, ch_hb, sh_ht, ch_ht, k_ht = row
    ys = [-sh_hb, -ch_hb, 0.0, ch_hb, sh_hb]
    zs = [sh_ht, ch_ht, k_ht, ch_ht, sh_ht]
    ax2.plot(ys, zs, label=f'Baliza {int(stn)} (x={x:.1f}m)', linewidth=1.5)
ax2.set_title('Plano de Balizas (Seção Transversal)', fontsize=11, fontweight='bold', color='#1F3864')
ax2.set_xlabel('Meia-Boca Y (m) [BB / BE]')
ax2.set_ylabel('Altura Z (m) acima da quilha')
ax2.grid(True, linestyle='--', alpha=0.5)
ax2.set_aspect('equal')
ax2.legend(loc='upper right', fontsize=7, ncol=2)

# 3. Vista de Perfil Lateral (Linha de Base, Chine, Borda)
ax3 = fig.add_subplot(223)
ax3.plot(xs, [r[4] for r in data], 'o-', color='#C00000', linewidth=2.0, label='Borda (Sheer)')
ax3.plot(xs, [r[5] for r in data], 's-', color='#2E75B6', linewidth=2.0, label='Chine (Quina)')
ax3.plot(xs, [r[6] for r in data], '^-', color='#385723', linewidth=2.0, label='Quilha / Fundo')
ax3.set_title('Perfil Lateral (Elevação)', fontsize=11, fontweight='bold', color='#1F3864')
ax3.set_xlabel('Comprimento X (m) [Popa -> Proa]')
ax3.set_ylabel('Altura Z (m)')
ax3.grid(True, linestyle='--', alpha=0.5)
ax3.legend(loc='upper left', fontsize=8)

# 4. Vista de Planta (Meia-Boca Superior)
ax4 = fig.add_subplot(224)
ax4.plot(xs, [r[2] for r in data], 'o-', color='#C00000', linewidth=2.0, label='Linha de Borda (Topo)')
ax4.plot(xs, [r[3] for r in data], 's-', color='#2E75B6', linewidth=2.0, label='Linha de Chine (Fundo)')
ax4.plot(xs, [-r[2] for r in data], 'o-', color='#C00000', linewidth=2.0)
ax4.plot(xs, [-r[3] for r in data], 's-', color='#2E75B6', linewidth=2.0)
ax4.plot(xs, [0.0 for _ in data], '--', color='#385723', alpha=0.7, label='Linha de Centro')
ax4.set_title('Planta Baixa (Linhas d\'Água e Boca)', fontsize=11, fontweight='bold', color='#1F3864')
ax4.set_xlabel('Comprimento X (m)')
ax4.set_ylabel('Boca Total Y (m)')
ax4.grid(True, linestyle='--', alpha=0.5)
ax4.legend(loc='upper right', fontsize=8)

plt.suptitle('Plano de Linhas & Geometria 3D — Barco 7,00 m Fundo Semi-Plano\nProjetista: Tino Neto | Motor: Agrale M90 (13 cv)', fontsize=13, fontweight='bold', color='#1F3864')
plt.tight_layout()
plt.savefig('/home/magdalena/eee/alunos/tino-neto/desenho_casco_quinado_3d_tino_neto.png', dpi=220)
print('Imagem gerada com sucesso!')
