国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 壶关县| 天等县| 晋中市| 广水市| 竹北市| 志丹县| 上思县| 建宁县| 遂昌县| 资讯 | 临泽县| 二连浩特市| 望城县| 乃东县| 上林县| 德庆县| 景洪市| 都昌县| 稷山县| 新闻| 灵川县| 桐梓县| 鸡东县| 宕昌县| 锡林郭勒盟| 麻江县| 新泰市| 石楼县| 社旗县| 政和县| 安顺市| 江都市| 遂川县| 嵊州市| 额济纳旗| 宜丰县| 贺兰县| 临泽县| 体育| 孟州市| 洮南市|