国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

JavaScript實(shí)現(xiàn)封閉區(qū)域布爾運(yùn)算的示例代碼

2019-11-19 13:36:49
字體:
供稿:網(wǎng)友

這篇文章主要介紹多段線實(shí)現(xiàn)布爾運(yùn)算的方法

先上代碼

function getOperatedCurves(sourceCurs: Curve[], targetCus: Curve[])  {    let source: Polyline | Circle = (sourceCurs[0] instanceof Circle) ? sourceCurs[0] as Circle : new Polyline().Combine(sourceCurs)[0];    let target: Polyline | Circle = (targetCus[0] instanceof Circle) ? targetCus[0] as Circle : new Polyline().Combine(targetCus)[0];    try    {      if (!source.IsClose || !target.IsClose) throw new Error("不是封閉曲線");    }    catch (err)    {      console.log(err);    }    let interPts = source.IntersectWith(target, IntersectOption.OnBothOperands);    let sourceContainerTarget = isTargetCurInSourceCur(source, target);    let targetContainerSource = isTargetCurInSourceCur(target, source);    let isContainer = sourceContainerTarget || targetContainerSource;    let intersectionList: Curve[] = []; //交集    let unionList: Curve[] = []; //并集    let subList: Curve[] = []; //補(bǔ)集    /*    *兩封閉區(qū)域有交點(diǎn)并且不是包含關(guān)系,則通過交點(diǎn)把區(qū)域分割    */    if (interPts.length && !isContainer)    {      let pars1 = interPts.map(p => source.GetParamAtPoint(p)).sort((a, b) => a - b);      let pars2 = interPts.map(p => target.GetParamAtPoint(p)).sort((a, b) => a - b);      let cus1: Array<Polyline | Arc> = source.GetSplitCurves(pars1);      cus1.forEach(pl =>      {        if (isTargetCurInSourceCur(target, pl))        {          intersectionList.push(pl);        }        else        {          subList.push(pl);          unionList.push(pl);        }      })      let cus2: Array<Polyline | Arc> = target.GetSplitCurves(pars2);      cus2.forEach(pl =>      {        if (isTargetCurInSourceCur(source, pl))        {          intersectionList.push(pl);          subList.push(pl);        }        else        {          unionList.push(pl);        }      })    }    else    {      if (isContainer)      {        if (sourceContainerTarget)        {          intersectionList.push(target);          subList.push(source, target);          unionList.push(source);        }        else        {          unionList.push(target);          intersectionList.push(source);        }      }      else      {        unionList.push(source, target)        subList.push(source);      }    }    return {      intersectionList, unionList, subList    }  }

由于一些曲線類實(shí)現(xiàn)方法不一,這里主要說一些實(shí)現(xiàn)布爾運(yùn)算的思路

  1. 判斷2封閉曲線是否是被包含的關(guān)系
  2. 獲取2封閉曲線的所有交點(diǎn),這里交點(diǎn)可能是圓和線,線和線,圓和圓的,求交點(diǎn)的方法網(wǎng)上應(yīng)該很多,以后有時(shí)間也會(huì)寫寫用JavaScript實(shí)現(xiàn)方式
  3. 根據(jù)所有的交點(diǎn)把2封閉曲線分割為多個(gè)部分
  4. 對(duì)分割后的線段進(jìn)行整理,其中相交部分是曲線在對(duì)方曲線內(nèi)部的部分,合并是互不在對(duì)方曲線內(nèi)部的部分,相減類似不想說了,具體看代碼,如果是被包含狀態(tài)則更加就簡單了

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 建昌县| 麻城市| 蓝田县| 安龙县| 富裕县| 砚山县| 谷城县| 甘南县| 嵊州市| 大港区| 巴林右旗| 日喀则市| 行唐县| 博野县| 新竹市| 静海县| 黑龙江省| 卢氏县| 阿巴嘎旗| 梁平县| 遂川县| 溆浦县| 仪征市| 沅江市| 龙口市| 罗平县| 孙吴县| 桑日县| 太白县| 东莞市| 苍溪县| 麻栗坡县| 南岸区| 微山县| 永定县| 沙田区| 马鞍山市| 丰宁| 会宁县| 壶关县| 鸡东县|