npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

divide-up-square-in-parallel-slices

v0.0.1

Published

```bash yarn add divide-up-square-in-parallel-slices ```

Downloads

11

Readme

Divide a square in parallel slices whose area is proportional to the data demo page


This library allows you to divide a square into parallel slices whose area is proportional to the data received in input. Here is an example:

step1

This means that the red slice (S1) has area equal to 30% of the area of ​​the square, the blue slice (S2) also, and the green slice (S3) has area equal to 40% of the total square area.

Install

yarn add divide-up-square-in-parallel-slices

or

npm install divide-up-square-in-parallel-slices --save

Screenshots

demo

API

As seen before, you can create parallel slices proportional to square area.

computeSquareSlices(dataset, squareSide)

The computeSquareSlices function accepts two parameters and returns an array of objects containing useful information about each slice.

Parameters

| Argument | Type | Description | | ------------ | -------- | ---------------------------------------------------------------------------------------- | | dataset | T[] | array of objects, each object must contains a percentage property (number in [0, 1]) | | squareSide | number | square side lenght |

Note: the sum of percentage values must be 1.

and:

interface Point {
  x: number
  y: number
}

Returns

The returned array contains one object for each slice. Each object has these properties:

| Property name | Type | Description | | -------------------------- | --------------------------- | ------------------------------------------------------------------ | | ...datum | / | the original dataset object info | | percentage | number | number in [0, 1] | | cumulativePercentage | number | cumulative percentage value | | sidePosition | left or right or over | slice position compared to square diagonal | | vertices | Vertices | slice vertices coordinates | | height | number | slice height | | middlePointLeftDiagonal | Point | coordinates of the point at the center of the slice left diagonal | | middlePointRightDiagonal | Point | coordinates of the point at the center of the slice right diagonal | | leftDiagonalLenght | number | left diagonal lenght | | rightDiagonalLenght | number | rigth diagonal lenght | | path | string | slice path |

interface Vertices {
  ldt: Point
  ldb: Point
  rdt: Point
  rdb: Point
  rt: Point
  lb: Point
}

Example

I hope the following example can better explain the information written above.

import { computeSquareSlices } from 'divide-up-square-in-parallel-slices'

const dataset = [
  { percentage: 0.3, color: "#ff787a" },
  { percentage: 0.3, color: "#7f7ad9" },
  { percentage: 0.4, color: "#74dfc9" },
]
const squareSide = 125
const slicesInfo = computeSquareSlices(dataset, squareSide)

// [
//   {
//     "percentage": 0.3,
//     "color": "#ff787a",
//     "cumulativePercentage": 0.3,
//     "sidePosition": "left",
//     "vertices": {
//       "ldt": { "x": 0,  "y": 0 },
//       "ldb": { "x": 0,  "y": 0 },
//       "rdt": { "x": 193.64916731037084, "y": 0 },
//       "rdb": { "x": 0, "y": 193.64916731037084 },
//       "rt": { "x": 193.64916731037084, "y": 0 },
//       "lb": { "x": 0, "y": 193.64916731037084 }
//     },
//     "height": 136.93063937629154,
//     "middlePointLeftDiagonal": { "x": 0, "y": 0 },
//     "middlePointRightDiagonal": { "x": 96.82458365518542, "y": 96.82458365518542 },
//     "leftDiagonalLenght": 0,
//     "rightDiagonalLenght": 273.8612787525831,
//     "path": "M 0 0L 193.64916731037084 0L 193.64916731037084 0L 0 193.64916731037084L 0 193.64916731037084L 0 0 Z"
//   },
//   { ... },
//   { ... }

You can draw the slice using the information above and here is the result:

step1

In particular:

stepS1

the red slice has side = left because it lays on the left side of the square diagonal (the diagonal that connects the top-left vertice to the bottom-right vertice).

stepS2

the blue slice has side = over because its area it's above the square diagonal.

And so on...

Demo page

A demo page is available.

License

MIT © Ilaria Venturini