You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在SageMath中为绘图中半圆上方两竖线间的指定区域上色

enter image description here

你可以通过SageMath内置的polygon函数构造封闭填充区域来实现需求,核心思路是先列出待填充区域的所有顶点:左竖线上端点 -> 左竖线与半圆交点 -> 沿半圆从左到右的采样点 -> 右竖线与半圆交点 -> 右竖线上端点,最后闭合路径即可。

以下是完整可运行的实现代码:

var('x')
# 定义上半圆函数
e = sqrt(1 - x^2)
# 构造两条竖线
f = line([(-1/2, sqrt(3)/2), (-1/2, 3)], color='black')
g = line([(1/2, sqrt(3)/2), (1/2, 3)], color='black')
# 生成半圆介于x=-1/2到x=1/2之间的采样点,步长越小边缘越平滑
semicircle_points = [(x_val, sqrt(1 - x_val^2)) for x_val in srange(-1/2, 1/2 + 0.01, 0.01)]
# 拼接填充区域的所有顶点
fill_points = [(-1/2, 3)] + semicircle_points + [(1/2, 3)]
# 生成填充图层,alpha控制透明度,color控制填充色
fill_area = polygon(fill_points, color='lightblue', alpha=0.5, edgecolor='none')
# 合并所有图层展示
show(plot(e, color='black') + fill_area + f + g, aspect_ratio=1)

你可以根据需求调整填充色、透明度、填充区域的最大高度等参数,修改对应变量的取值即可。

内容的提问来源于stack exchange,提问作者Dedekid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 06:48:02