UNPKG

fabric-texture

Version:

This JavaScript library enables fast and efficient image distortion transformations using Canvas 2D.

57 lines (56 loc) 1.79 kB
/** * 计算二维平面上两条线段的交点 * * 使用解析几何方法计算两条线段的交点坐标。 * 处理了以下特殊情况: * 1. 平行线段(包括重合) * 2. 垂直于坐标轴的线段 * 3. 考虑浮点数精度问题 * * @param p1 - 第一条线段的起点 * @property {number} x - x坐标 * @property {number} y - y坐标 * @param p2 - 第一条线段的终点 * @property {number} x - x坐标 * @property {number} y - y坐标 * @param p3 - 第二条线段的起点 * @property {number} x - x坐标 * @property {number} y - y坐标 * @param p4 - 第二条线段的终点 * @property {number} x - x坐标 * @property {number} y - y坐标 * * @returns {Coord | null} 交点坐标,如果线段平行或重合则返回 null * * @example * ```typescript * // 计算两条相交线段的交点 * const p1 = { x: 0, y: 0 }; * const p2 = { x: 2, y: 2 }; * const p3 = { x: 0, y: 2 }; * const p4 = { x: 2, y: 0 }; * * const intersection = calcIntersection(p1, p2, p3, p4); * console.log(intersection); // { x: 1, y: 1 } * * // 处理平行线段 * const parallel = calcIntersection( * { x: 0, y: 0 }, { x: 1, y: 1 }, * { x: 0, y: 1 }, { x: 1, y: 2 } * ); * console.log(parallel); // null * * // 处理垂直线段 * const vertical = calcIntersection( * { x: 1, y: 0 }, { x: 1, y: 2 }, // 垂直线 * { x: 0, y: 1 }, { x: 2, y: 1 } // 水平线 * ); * console.log(vertical); // { x: 1, y: 1 } * ``` * * @remarks * - 使用 1e4 的放大因子处理浮点数精度问题 * - 使用 0.1**12 作为近似值判断平行阈值 * - 返回的坐标可能会有微小的精度误差 */ export declare const calcIntersection: (p1: Coord, p2: Coord, p3: Coord, p4: Coord) => Coord | null;