欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2018年06月25日 10:36:39   作者:ZoeLee  
這篇文章主要介紹了JavaScript實(shí)現(xiàn)封閉區(qū)域布爾運(yùn)算的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

這篇文章主要介紹多段線實(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)則更加就簡(jiǎn)單了

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論