# -*- coding: utf-8 -*-
"""第 4 章 · 图 4-1　需求跟踪示例（教材 p63）
按教材图意重绘：三个文字框（用户需求 → 需求文件 → 产品成果），
相邻框之间上方为「正向跟踪」右向箭头、下方为「逆向跟踪」左向箭头。
输出：assets/图4-1-需求跟踪示例.png
用法：~/Downloads/.tools/pyvenv/bin/python scripts/fig4-1-需求跟踪示例.py
"""
import os
import sys

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from _draw import setup, canvas, box, arrow, caption, save  # noqa: E402

setup()
fig, ax = canvas(w=10.0, h=2.9, xlim=(0, 100), ylim=(0, 64))

BW, BH = 20.0, 12.0          # 方框宽高
YT = 52.0                    # 方框上边界
xs = [6.0, 40.0, 74.0]       # 三个方框左上角 x
labels = ['用户需求', '需求文件', '产品成果']

for x, t in zip(xs, labels):
    box(ax, x, YT, BW, BH, t, fs=12, bold=False)

TOP_Y, BOT_Y = 49.0, 43.0    # 上/下箭头高度
for i in range(2):
    x1 = xs[i] + BW          # 左框右边缘
    x2 = xs[i + 1]           # 右框左边缘
    mid = (x1 + x2) / 2.0
    arrow(ax, (x1 + 0.6, TOP_Y), (x2 - 0.6, TOP_Y))          # 正向跟踪 ——→
    ax.text(mid, TOP_Y + 2.6, '正向跟踪', ha='center', va='bottom',
            color='#1a365d', fontsize=11)
    arrow(ax, (x2 - 0.6, BOT_Y), (x1 + 0.6, BOT_Y))          # ←—— 逆向跟踪
    ax.text(mid, BOT_Y - 2.6, '逆向跟踪', ha='center', va='top',
            color='#1a365d', fontsize=11)

caption(ax, '图 4-1　需求跟踪示例', fs=11, y=6)
save(fig, os.path.join(os.path.dirname(os.path.abspath(__file__)), '..',
                       'assets', '图4-1-需求跟踪示例.png'))
