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

Python中如何计算多条(如4条)曲线之间的重叠面积

多条曲线公共重叠面积Python实现方案

首先确认使用场景,目前主流的实现分为两种,对应不同的曲线类型:

场景1:开放型曲线(与X轴围成下侧区域,即你附图中的场景)

4条曲线绘图示例
核心思路是对齐所有曲线的X轴采样,取每个X点所有曲线Y值的最小值作为重叠区域上界,积分后得到面积。

实现代码

import numpy as np
from scipy.interpolate import interp1d
from scipy.integrate import trapezoid

def calc_multi_curve_overlap_area(curves, sample_density=10000):
    """
    计算多条开放曲线与X轴围成区域的公共重叠面积
    :param curves: 列表,每个元素为(x_array, y_array)格式的曲线坐标序列
    :param sample_density: 统一X轴的采样点数,数值越高精度越高
    :return: 公共重叠面积
    """
    # 取所有曲线的公共X轴范围
    x_min = max([np.min(c[0]) for c in curves])
    x_max = min([np.max(c[0]) for c in curves])
    if x_min >= x_max:
        return 0.0
    
    # 生成统一采样的X轴
    uniform_x = np.linspace(x_min, x_max, sample_density)
    
    # 所有曲线插值到统一X轴
    interp_y_list = []
    for x, y in curves:
        interp_func = interp1d(x, y, bounds_error=False, fill_value=0)
        interp_y_list.append(interp_func(uniform_x))
    interp_y_arr = np.array(interp_y_list)
    
    # 取每个点的Y最小值作为重叠区域上边界
    overlap_upper_bound = np.min(interp_y_arr, axis=0)
    # 过滤X轴下方的无效区域,可根据需求删除该行
    overlap_upper_bound[overlap_upper_bound < 0] = 0
    
    # 梯形法积分求面积
    return trapezoid(overlap_upper_bound, uniform_x)

使用示例

# 构造4条测试曲线
x = np.linspace(0, 10, 200)
curve_1 = (x, np.sin(x) + 2)
curve_2 = (x, np.cos(x) + 2.2)
curve_3 = (x, 0.12 * x + 1.1)
curve_4 = (x, -0.04 * x**2 + 0.4 * x + 1.6)

# 计算重叠面积
print(calc_multi_curve_overlap_area([curve_1, curve_2, curve_3, curve_4]))

场景2:封闭型曲线(任意闭合轮廓围成的区域)

如果是闭合曲线,直接用shapely库计算所有多边形的交集面积即可,无需自己处理插值逻辑:

实现代码

from shapely.geometry import Polygon
from shapely.ops import intersection_all

def calc_closed_curve_overlap_area(closed_curves):
    """
    计算多条封闭曲线围成区域的公共重叠面积
    :param closed_curves: 列表,每个元素为(N, 2)格式的封闭曲线坐标序列
    :return: 公共重叠面积
    """
    polygon_list = [Polygon(curve) for curve in closed_curves]
    return intersection_all(polygon_list).area

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 09:06:00