Git Product home page Git Product logo

geometry.js's Introduction

geometry.js

Some useful structures for graphics programming in JavaScript I guess...

Structures included

  • Point (x, y)
  • Size (width, height)
  • Rectangle (Point, Size)
  • Circle (Point, radius)
  • Point3D (x, y, z)
  • Size3D (width, height, depth)
  • Cube (Point3D, Size3D)
  • Sphere (Point3D, radius)
  • Line (Point, Point)
  • Line3D (Point3D, Point3D)
  • Polygon (optional Point[])
  • Polygon3D (optional Point3D[])

NodeJS require all

const { Size, Point, Rectangle, Circle, Point3D, Size3D, Cube, Sphere, Line, Line3D, Polygon, Polygon3D } = require('./geometry.js');

Some examples

Point

Get angle between two points in degrees

const a = new Point(0, 0);
const b = new Point(40, 40);
console.log(a.angleTo(b).degrees);

Get distance between two points

const a = new Point(50, 50);
const b = new Point(100, 400);
console.log(a.distanceTo(b));

Rectangle

Determine if a rectangle is completely inside another rectangle

const a = new Rectangle(new Point(0, 0), new Size(20, 15));
const b = new Rectangle(new Point(5, 5), new Size(10, 8));
console.log(b.isInside(a));

Circle

Determine if two circles intersect

const a = new Circle(new Point(-20, -20), 40);
const b = new Circle(new Point(50, 50), 90);
console.log(b.intersectsWith(a));

Cube

Determine if two cubes intersect

const a = new Cube(new Point3D(0, 0, 0), new Size3D(200, 50, 50));
const b = new Cube(new Point3D(100, 0, 0), new Size3D(200, 50, 50));
console.log(a.intersectsWith(b));

Point3D

Get distance between two points in 3d space

const a = new Point3D(0, 0, 0);
const b = new Point3D(100, 100, 100);
console.log(a.distanceTo(b));

Sphere

Determine if two spheres intersect

const a = new Sphere(new Point3D(0, 0, 0), 300);
const b = new Sphere(new Point3D(200, 0, 0), 300);
console.log(a.intersectsWith(b));

Determine if a sphere is completely inside another sphere

const a = new Sphere(new Point3D(0, 0, 0), 300);
const b = new Sphere(new Point3D(30, 30, 0), 190);
console.log(b.isInside(a));

Line

Get line's length and angle between it's two points in radians

const line = new Line(new Point(0, 0), new Point(100, 100));
console.log(line.length());
console.log(line.angle().radians);

Polygon

Create a polygon from array of points

const polygon = new Polygon(
    new Point(1500, 1000),
    new Point(1154.5084971874737, 524.4717418524233),
    new Point(595.4915028125263, 706.1073738537634),
    new Point(595.4915028125263, 1293.8926261462366),
    new Point(1154.5084971874735, 1475.5282581475767)
);

Drawing the polygon above to HTML5 canvas (or nodejs)

ctx.lineWidth = 50;
ctx.strokeStyle = '#348feb';
ctx.fillStyle = '#0f0f0f';

ctx.beginPath();
polygon.Points.forEach((point, i)=>{
   if (i==0) ctx.moveTo(point.X, point.Y);
   ctx.lineTo(point.X, point.Y);
});
ctx.closePath();

ctx.fill();
ctx.stroke();

geometry.js's People

Contributors

heapoverride avatar

Stargazers

Wesley avatar hamad avatar AB avatar  avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.