1.2.15 • Published 3 years ago

polygon-generator v1.2.15

Weekly downloads
221
License
BSD 2-Clause Lice...
Repository
github
Last release
3 years ago

npm version Run Status Coverage Badge Codacy Badge

polygon-generator

API

polygon = require('polygon-generator');

coordinates method

Calculate coordinates of an equilateral polygon. The returned object is an array of the following structure:

[{x: 0, y: 0}, {x: 1, y: 0}, {x: 0.5, y: 0.866}]

The vectors form an anti-clockwise path with the last vector connecting back to the first one. All coordinates will be in the first quadrant of a cartesian coordinate system meaning there will be no negative numbers. This method takes three parameters:

  • sides: the number of sides of the polygon (minimum is 3)
  • sideLength: the length of each side (default is 1)
  • startingAngle: determines the angle of the first edge, rotates anti-clockwise (default is 0, horizontal line)
sides = 3;
sideLength = 1;
startingAngle = 10;
vertices = polygon.coordinates(sides, sideLength, startingAngle);

polygon wrapper

The polygon object provides translation, rotation and scaling methods for polygons. To create one simply pass in an array of vectors representing the vertices.

poly = polygon.polygon([{x: 0, y: 0}, {x: 1, y: 0}, {x: 0.5, y: 0.866}]);

You can also directly pass in the results of the coordinates method.

poly = polygon.polygon(polygon.coordinates(5,2,20));

translate

poly.translate(vector);

Translate shifts a polygon along a vector. The vector needs to be an object with a field x and y, which should both be numbers.

rotate

poly.rotate(angle, point);

Rotate a polygon anti-clockwise around a given point {x: ?, y: ?} by the specified angle. If no point is specified then the centroid of the polygon will be used.

scale

poly.rotate(factor, point);

Scale a polygon by a certain factor with regard to a given point. If no point is specified then the centroid of the polygon will be used.

area

poly.area();

Returns the area of the polygon.

centroid

poly.centroid();

Returns the coordinates of the centroid {x: Cx, y: Cy}.

Further Links

Contributing

License

1.2.14

3 years ago

1.2.15

3 years ago

1.2.13

3 years ago

1.2.12

3 years ago

1.2.11

4 years ago

1.2.9

4 years ago

1.2.8

4 years ago

1.2.7

5 years ago

1.2.6

5 years ago

1.2.5

5 years ago

1.2.4

5 years ago

1.2.3

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago