11import {
2- copyPoint ,
32 length ,
43 reverse ,
54 shift ,
87 mod ,
98 angleOf ,
109 coordEqual ,
10+ mapShape ,
11+ forShape ,
1112} from "../util" ;
1213import { Point , Shape } from "../types" ;
1314
@@ -40,21 +41,21 @@ const optimizeOrder = (a: Shape, b: Shape): Shape => {
4041} ;
4142
4243// OPT allow extra division
43- export const divideShape = ( count : number , points : Shape ) : Shape => {
44- if ( points . length < 3 ) throw new Error ( "not enough points" ) ;
45- if ( count < points . length ) throw new Error ( "cannot remove points" ) ;
46- if ( count === points . length ) return points . slice ( ) ;
44+ export const divideShape = ( count : number , shape : Shape ) : Shape => {
45+ if ( shape . length < 3 ) throw new Error ( "not enough points" ) ;
46+ if ( count < shape . length ) throw new Error ( "cannot remove points" ) ;
47+ if ( count === shape . length ) return shape . slice ( ) ;
4748
48- const lengths = [ ] ;
49- for ( let i = 0 ; i < points . length ; i ++ ) {
50- lengths . push ( length ( points [ i ] , points [ mod ( i + 1 , points . length ) ] ) ) ;
51- }
49+ const lengths : number [ ] = [ ] ;
50+ forShape ( shape , ( { curr , next } ) => {
51+ lengths . push ( length ( curr , next ( ) ) ) ;
52+ } ) ;
5253
53- const divisors = divideLengths ( lengths , count - points . length ) ;
54+ const divisors = divideLengths ( lengths , count - shape . length ) ;
5455 const out : Shape = [ ] ;
55- for ( let i = 0 ; i < points . length ; i ++ ) {
56- const curr : Point = out [ out . length - 1 ] || points [ i ] ;
57- const next = points [ mod ( i + 1 , points . length ) ] ;
56+ for ( let i = 0 ; i < shape . length ; i ++ ) {
57+ const curr : Point = out [ out . length - 1 ] || shape [ i ] ;
58+ const next = shape [ mod ( i + 1 , shape . length ) ] ;
5859 out . pop ( ) ;
5960 out . push ( ...insertCount ( divisors [ i ] , curr , next ) ) ;
6061 }
@@ -66,39 +67,29 @@ export const divideShape = (count: number, points: Shape): Shape => {
6667
6768// OPT disable
6869const fixAnglesWith = ( fixee : Shape , fixer : Shape ) : Shape => {
69- const out : Shape = [ ] ;
70- for ( let i = 0 ; i < fixee . length ; i ++ ) {
71- const before = fixee [ mod ( i - 1 , fixee . length ) ] ;
72- const after = fixee [ mod ( i + 1 , fixee . length ) ] ;
73- const point = copyPoint ( fixee [ i ] ) ;
74- if ( point . handleIn . length === 0 && coordEqual ( before , point ) ) {
75- point . handleIn . angle = fixer [ i ] . handleIn . angle ;
70+ return mapShape ( fixee , ( { index, curr, prev, next} ) => {
71+ if ( curr . handleIn . length === 0 && coordEqual ( prev ( ) , curr ) ) {
72+ curr . handleIn . angle = fixer [ index ] . handleIn . angle ;
7673 }
77- if ( point . handleOut . length === 0 && coordEqual ( after , point ) ) {
78- point . handleOut . angle = fixer [ i ] . handleOut . angle ;
74+ if ( curr . handleOut . length === 0 && coordEqual ( next ( ) , curr ) ) {
75+ curr . handleOut . angle = fixer [ index ] . handleOut . angle ;
7976 }
80- out . push ( point ) ;
81- }
82- return out ;
77+ return curr ;
78+ } ) ;
8379} ;
8480
8581// OPT disable
8682const fixAnglesSelf = ( shape : Shape ) : Shape => {
87- const out : Shape = [ ] ;
88- for ( let i = 0 ; i < shape . length ; i ++ ) {
89- const before = shape [ mod ( i - 1 , shape . length ) ] ;
90- const after = shape [ mod ( i + 1 , shape . length ) ] ;
91- const angle = angleOf ( before , after ) ;
92- const point = copyPoint ( shape [ i ] ) ;
93- if ( point . handleIn . length === 0 ) {
94- point . handleIn . angle = angle + Math . PI ;
83+ return mapShape ( shape , ( { curr, prev, next} ) => {
84+ const angle = angleOf ( prev ( ) , next ( ) ) ;
85+ if ( curr . handleIn . length === 0 ) {
86+ curr . handleIn . angle = angle + Math . PI ;
9587 }
96- if ( point . handleOut . length === 0 ) {
97- point . handleOut . angle = angle ;
88+ if ( curr . handleOut . length === 0 ) {
89+ curr . handleOut . angle = angle ;
9890 }
99- out . push ( point ) ;
100- }
101- return out ;
91+ return curr ;
92+ } ) ;
10293} ;
10394
10495const divideLengths = ( lengths : number [ ] , add : number ) : number [ ] => {
@@ -124,9 +115,9 @@ const divideLengths = (lengths: number[], add: number): number[] => {
124115} ;
125116
126117export const prepShapes = ( a : Shape , b : Shape ) : [ Shape , Shape ] => {
127- const points = Math . max ( a . length , b . length ) ;
128- const aNorm = divideShape ( points , a ) ;
129- const bNorm = divideShape ( points , b ) ;
118+ const pointCount = Math . max ( a . length , b . length ) ;
119+ const aNorm = divideShape ( pointCount , a ) ;
120+ const bNorm = divideShape ( pointCount , b ) ;
130121 const bOpt = optimizeOrder ( aNorm , bNorm ) ;
131122 return [ fixAnglesWith ( fixAnglesSelf ( aNorm ) , bNorm ) , fixAnglesWith ( fixAnglesSelf ( bOpt ) , aNorm ) ] ;
132123} ;
0 commit comments