Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ml-affine-transform

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ml-affine-transform

Get and apply affine transform to 2D points.

  • 1.0.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

affine-transform

NPM version build status Test coverage npm download

Get and apply affine transform to 2D points.

Installation

$ npm i ml-affine-transform

Main steps of the algorithm to get the affine transform

Based on the tutorial: https://nghiaho.com/?page_id=671

  1. Find centroids of the two point sets and deduce the translation from one to the other
  2. Find rotation using SVD

When the transform is applied to a function, the operations are made in the following order:

  1. Rotate
  2. Scale
  3. Translate

API

The inputs of the functions are 3xN matrices consisting of the source and the destination points. The third dimension for Z must be padded with ones. The output is an object containing the x and y translations as well as the anti-clockwise angle in degrees.

export interface AffineTransformParameters {
  rotation: number;
  translation: { x: number; y: number };
  scale: number;
}

export function getAffineTransform(
  source: Matrix,
  destination: Matrix,
): AffineTransformParameters;

Coordinates system

In this project, standard x and y axes are used, as in mathematics (y pointing up and x to the right). The angles are expressed in degrees and positive angles are in the anti-clockwise direction.

Example

import Matrix from 'ml-matrix';
import { getAffineTransform } from '../getAffineTransform';

const sourceMatrix = new Matrix([
  [1, 1, -3], // x
  [2, -1, -1], // y
  [1, 1, 1], // z
]);
const destinationMatrix = new Matrix([
  [4, -2, -2],
  [-2, -2, 6],
  [1, 1, 1],
]);

const result = getAffineTransform(sourceMatrix, destinationMatrix);

// result = {
//   translation: { x: 0, y: 0 },
//   scale: 2,
//   rotation: -90,
// }

License

MIT

Keywords

FAQs

Package last updated on 15 Jun 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc