import { round , radToAngle , angleToRad } from "../misc/index" ; import { PathData , GroupParameters , Frame , InputParameters , FrameVertex , FrameParameters } from "./interfaces" ; import parseGroupParameter from "./lib/parseGroupParameter" ; import { pipe } from "fp-ts/lib/pipeable" ; const calcNumOfVertexes = ( numOfSegments , depth ) : number => numOfSegments * Math . pow ( 2 , depth ) ; const getRadiansValue = ( group , vertexIndex ) : number => { let parameter = group . radians ; parameter = parseGroupParameter ( parameter , vertexIndex ) ; if ( ! parameter ) return parameter ; else if ( typeof parameter !== "number" ) throw `Wrong 'radians' parameter in group number ${ group . pk }` ; else return parameter ; } ; const setFrameParameters = ( parameters ) : FrameParameters => ( { numOfSegments : parameters . numOfSegments , depth : parameters . depth , rotate : parameters . rotate , group : parameters . groups [ 0 ] } ) ; const setNumOfVertexes = ( frame ) => ( { ... frame , numOfVertexes : frame . numOfSegments * Math . pow ( 2 , frame . depth ) } ) ; const vertexesReducer = ( frame ) => ( acc , vertex , i ) => { let radians ; radians = frame . group . radians ? getRadiansValue ( frame . group , i ) : ( ( Math . PI * 2 ) / frame . numOfVertexes ) * i ; radians = radians + angleToRad ( frame . rotate ) ; const angle = radToAngle ( radians ) ; const cosx = round ( Math . cos ( radians ) ) ; const siny = round ( Math . sin ( radians ) ) ; const x = cosx ; const y = siny ; return [ ... acc , { cosx , siny , x , y , radians , angle } ] ; } ; const setVertexes = ( frame ) => ( { ... frame , vertexes : Array ( frame . numOfVertexes ) . fill ( { } ) . reduce ( vertexesReducer ( frame ) , [ ] ) } ) ; /**
 * Generate frame which is the base for a path and
 * serve as the base for a 0-group vertexes.
 */ export default ( path ) : PathData => ( { ... path , frame : pipe ( setFrameParameters ( path . parameters ) , setNumOfVertexes , setVertexes ) } ) ;