import { Point , PolyBool } from "bp/math" ; import * as paper from "paper" ; import type { Line } from "bp/math" ; /**
 * 這個靜態類別提供了一些 paper.js 相關的工具。
 */ export namespace PaperUtil { /**
     * 替換掉一個  paper . CompoundPath 的內容。
     *  param target param source param clone export function $replaceContent ( target , source , clone ) { target . removeChildren ( ) ; if ( source instanceof paper . CompoundPath ) { target . copyContent ( source ) ; } else { if ( clone ) source = source . clone ( { insert : false } ) ; target . addChild ( source ) ; } } export function $setRectangleSize ( rect , width , height ) { rect . segments [ 1 ] . point . set ( width , 0 ) ; rect . segments [ 2 ] . point . set ( width , height ) ; rect . segments [ 3 ] . point . set ( 0 , height ) ; } /** 把一個  paper . CompoundPath 加入指定兩點定義的線段  export function $addLine ( path , p1 , p2 ) { if ( p1 instanceof Point ) p1 = p1 . $toPaper ( ) ; if ( p2 instanceof Point ) p2 = p2 . $toPaper ( ) ; path . moveTo ( p1 ) ; path . lineTo ( p2 ) ; } /** 把一個  paper . CompoundPath 的內容替換成傳入的直線陣列組  export function $setLines ( path , ... lines ) { path . removeChildren ( ) ; for ( let set of lines ) for ( let l of set ) PaperUtil . $addLine ( path , l . p1 , l . p2 ) ; } export function $unite ( p1 , p2 ) { if ( p1 . isEmpty ( ) ) return p2 ; else return p1 . unite ( p2 , { insert : false } ) ; } export const $black = new paper . Color ( 'black' ) ; export const $red = new paper . Color ( 'red' ) ; export function $fromShape ( shape ) { let poly = PolyBool . toPolygon ( shape ) ; return poly . regions . map ( r => new paper . Path ( { segments : r , closed : true , } ) ) ; } }