# -*- coding: utf-8 -*-
"""图 5-19　开发方法
一条从左（预测型）到右（适应型）的渐变粗箭头，中间「混合型」；
下方楔形示意「迭代性和增量性逐渐增加」。
"""
import os
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
from matplotlib.colors import to_rgb
from _draw import setup, save, canvas, INK, NAV

setup()

fig, ax = canvas(10.0, 3.9, (2, 98), (14, 90))

# ---------- 渐变配色：左深右浅 ----------
def mix(c1, c2, t):
    a, b = to_rgb(c1), to_rgb(c2)
    return tuple(a[i] + (b[i] - a[i]) * t for i in range(3))

DARK, LIGHT = '#12304f', '#cfe4f5'

# 箭杆（带左侧凹口 + 右侧箭头）
x0, x1 = 8.0, 96.0
ybot, ytop = 62.0, 80.0
shaft_end = 88.0          # 箭头锥体起点
mid = (ybot + ytop) / 2.0

n = 160
xs = [x0 + (shaft_end - x0) * i / n for i in range(n + 1)]
for i in range(n):
    t = i / (n - 1)
    ax.fill_between([xs[i], xs[i + 1]], ybot, ytop,
                    color=mix(DARK, LIGHT, t), lw=0, zorder=2)

# 尾部凹口（左端箭头形状）
notch = Polygon([(x0, ybot), (x0, ytop), (x0 + 4.2, mid)], closed=True,
                facecolor='white', edgecolor='none', zorder=3)
ax.add_patch(notch)
# 箭头锥体（右端）
head = Polygon([(shaft_end, ybot - 2.4), (x1, mid), (shaft_end, ytop + 2.4)],
               closed=True, facecolor=mix(DARK, LIGHT, 1.0), edgecolor='none', zorder=2)
ax.add_patch(head)

# ---------- 箭头内三档标签 ----------
ax.text(24, mid, '预测型', ha='center', va='center', fontsize=15,
        color='white', fontweight='bold', zorder=5)
ax.text(56, mid, '混合型', ha='center', va='center', fontsize=15,
        color='white', fontweight='bold', zorder=5)
ax.text(79, mid, '适应型', ha='center', va='center', fontsize=15,
        color=NAV, fontweight='bold', zorder=5)

# ---------- 下方楔形：迭代性和增量性逐渐增加 ----------
bx0, bx1 = 30.0, 88.0
top_y = 56.0
h0, h1 = 1.8, 36.0
m = 120
bxs = [bx0 + (bx1 - bx0) * i / m for i in range(m + 1)]
for i in range(m):
    t = i / (m - 1)
    h = h0 + (h1 - h0) * (t ** 0.75)
    ax.fill_between([bxs[i], bxs[i + 1]], top_y - h, top_y,
                    color=mix('#8fb8d8', LIGHT, t), lw=0, zorder=2)

ax.text(67, 41, '迭代性和增量性逐渐增加', ha='center', va='center',
        fontsize=13.5, color=NAV, zorder=6)
# 楔形右端的细描边，强调"逐渐增加"的方向
ax.plot([bx1, bx1], [top_y - h1, top_y], color='#5b8db8', lw=1.0, zorder=3)

fig.tight_layout()
out = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..',
                   'assets', '图5-19-开发方法.png')
save(fig, out)
