rust-rectangle-dividing

Crates.iorust-rectangle-dividing
lib.rsrust-rectangle-dividing
version0.1.4
sourcesrc
created_at2023-11-09 15:49:33.195832
updated_at2024-07-18 16:56:00.649428
descriptionA rectangle dividing algorithm written in Rust
homepage
repositoryhttps://github.com/kitsuyui/rust-rectangle-dividing/
max_upload_size
id1030241
size65,726
kitsuyui (kitsuyui)

documentation

README

rust-rectangle-dividing

A library of rectangle dividing written in Rust. It can be compiled to WebAssembly. So you can use it in JavaScript.

Motivation

I want to divide a rectangle into smaller rectangles by given conditions (weights, aspect ratio, etc.).

Mainly, I want to use this for generating a map for my react-playground

Usage in JavaScript

Install

This package is published on NPM @kitsuyui/rectangle-dividing

$ npm install @kitsuyui/rectangle-dividing
# or
$ yarn add @kitsuyui/rectangle-dividing
# or
$ pnpm add @kitsuyui/rectangle-dividing

Example

import { dividing } from "@kitsuyui/rectangle-dividing";

const rect = { x: 0, y: 0, w: 900, h: 800 };
const weights: Float32Array = Float32Array.from([4, 4, 1, 1, 1, 1]);
const aspectRatio = 1.5;
const verticalFirst = true;
const boustrophedron = true;
const divided = dividing(rect, weights, aspectRatio, verticalFirst, boustrophedron);
for (const d of divided) {
  console.log(d);
}

Result

{ x: 0, y: 0, w: 600, h: 400 }
{ x: 0, y: 400, w: 600, h: 400 }
{ x: 600, y: 600, w: 300, h: 200 }
{ x: 600, y: 400, w: 300, h: 200 }
{ x: 600, y: 200, w: 300, h: 200 }
{ x: 600, y: 0, w: 300, h: 200 }

dividing's arguments are

  • rect: The rectangle to be divided
  • weights: The weights of each rectangle
  • isVertical: The direction of the first division
  • aspectRatio: The aspect ratio of each rectangle
  • boustrophedon: The direction of the next division in the same level

License

MIT

Commit count: 46

cargo fmt