website

source code for the ttmaths.uk website
Log | Files | Refs | README

Polygon.jsx (660B)


      1 import { getColourClass } from '../../scripts/graphUtils.js';
      2 import Label from './Label.jsx';
      3 
      4 function getMean(arr) {
      5   return arr.reduce((a, b) => a + b) / arr.length;
      6 }
      7 
      8 function getMiddle(points) {
      9   const x = points.map(point => point[0]);
     10   const y = points.map(point => point[1]);
     11   return [getMean(x), getMean(y)];
     12 }
     13 
     14 export default function Polygon({ points, label=null, colour="auto" }) {
     15   const pointsString = points.map((point) => point.join(",")).join(" ");
     16   return (
     17     <g>
     18       <polygon points={pointsString} class={getColourClass(colour)}/>
     19       {label && <Label point={getMiddle(points)} colour={colour}>{label}</Label>}
     20     </g>
     21   );
     22 }