DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • TypeScript: Useful Features
  • useState() vs. useRef(): Understand the Technical Difference
  • A Comprehensive Guide on JavaScript Array Map Method
  • JSON Minify Full Guideline: Easy For You

Trending

  • How to Convert XLS to XLSX in Java
  • The Smart Way to Talk to Your Database: Why Hybrid API + NL2SQL Wins
  • How to Use AWS Aurora Database for a Retail Point of Sale (POS) Transaction System
  • Operational Principles, Architecture, Benefits, and Limitations of Artificial Intelligence Large Language Models
  1. DZone
  2. Data Engineering
  3. Data
  4. Transforming Arrays of Points in JavaScript

Transforming Arrays of Points in JavaScript

In this tutorial, you use the map array method to transform arrays of points, reflecting and rotating them.

By 
John Larsen user avatar
John Larsen
·
Nov. 10, 16 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
6.3K Views

Join the DZone community and get the full member experience.

Join For Free

In this tutorial, you use the map array method to transform arrays of points, reflecting and rotating them. For example, you reflect a simple shape across the y-axis, as shown below.

Reflecting points in the y axis

Representing a Single Point

For a single point, use an array with two elements, to represent the x- and y- coordinates. The following code produces this output:

 x is 3 and y is 4 

var p1 = [ 3 , 4 ];

console.log("x is " + p1[0] + " and y is " + p1[1]);


You create an array,  [ 3 , 4 ]  and assign it to the variable  p1 . To access the elements in the array, append the index of the required element, in square brackets, to the variable:  p1[0]  for the first element and  p1[1]  for the second. (Indexes for arrays start at 0.)

Reflecting Your Point

To reflect a single point in the y-axis, switch the sign of the point's x-coordinate. For example, [ 3 , 4 ]   reflected in the y-axis becomes  [ -3 , 4 ] , as shown in figure 2.

Reflecting a point in the y axis

To perform the reflection, create a function; you'll want to use the function again and again to reflect multiple points. The code block below displays the following output:

 (3, 4) becomes (-3, 4) 

// Change the sign of the x-coordinate
function reflectInY (point) {
  return [ -point[0] , point[1] ];
}

// Return a string representation of the point
function getPointString (point) {
  return "(" + point[0] + ", " + point[1] + ")";
}

// Create our initial point
var p1 = [ 3 , 4 ];

// Create a reflected point
var p2 = reflectInY(p1);

// Display the transformation
console.log(getPointString(p1) + " becomes " + getPointString(p2));

http://jsbin.com/sarenixozi/edit?js,console

You pass the function,  reflectInY , a point as an argument and it returns a new point with the updated x-coordinate:

 return [ -point[0] , point[1] ]; 

The reflectInY function changes the sign of the x coordinate

Reflecting Multiple Points Using Map

To represent multiple points in code, you use an array of points, an array of arrays:

var points = [ [3, 4], [3, 1], [1, 1] ];

To reflect all of the points in the y-axis, you need to pass each point to the  reflectInY  function. The result should be a new array of points.

JavaScript has a  map  method that creates a new array from an existing array. You specify the mapping function that  map  will use to create each new element from the corresponding existing element.

var newArray = oldArray.map(mappingFunction);

The map method creates new elements from old

So, for your set of points, the mapping function will be  reflectInY .

var points = [ [3, 4], [3, 1], [1, 1] ];

function reflectInY (point) {
  return [ -point[0], point[1] ];
}

var reflected = points.map(reflectInY);

console.log(reflected);
// [ [-3, 4], [-3, 1], [-1, 1] ]

 map  passes each point in the original  points  array to the  reflectInY  function. The reflected points that  reflectInY  returns make up the elements of the new  reflected  array.

Use map to pass all points through reflectInY

However, you might want to hide the implementation, how you performed the reflection, and provide a  reflectAllInY  function instead:

var points = [ [3, 4], [3, 1], [1, 1] ];

function reflectInY (point) {
  return [ -point[0], point[1] ];
}

function reflectAllInY (pointsArray) {
  return pointsArray.map(reflectInY);
}

var reflected = reflectAllInY(points);

console.log(reflected);
// [ [-3, 4], [-3, 1], [-1, 1] ]

http://jsbin.com/ducopuq/edit?js,console

Adding More Transformations

You don't want to be able only to reflect points in the y-axis; you want a range of transformations at your fingertips. For now, you add the ability to reflect in the x-axis and to rotate 90 degrees clockwise.

The figure below shows a point reflected in the x-axis. Notice how the sign of its y-coordinate is changed.

Reflecting a point in the x-axis

Here's the function to perform the reflection:

function reflectInX (point) {
  return [ point[0], -point[1] ];
}

As with  reflectInY , the  reflectInX  function returns a new point with the required coordinates.

The next figure shows a point rotated by 90 degrees clockwise about (0, 0). Pay careful attention to how the x- and y-coordinates are changed by the transformation.

Rotating a point by 90 degrees

And here's the function to perform the rotation:

function rotate90cw (point) {
  return [ point[1], -point[0] ];
}

The function returns a new point. The new x-coordinate is the old y-coordinate. The new y-coordinate is the old x-coordinate with its sign changed.

Applying the Transformations to Multiple Points

You can now bring all of your transformation functions together and apply them to multiple points using the  map  method.

// Individual transformations

function reflectInY (point) {
  return [ -point[0], point[1] ];
}

function reflectInX (point) {
  return [ point[0], -point[1] ];
}

function rotate90cw (point) {
  return [ point[1], -point[0] ];
}


// Group transformations

function reflectAllInY (points) {
  return points.map(reflectInY);
}

function reflectAllInX (points) {
  return points.map(reflectInX);
}

function rotateAll90cw (points) {
  return points.map(rotate90cw);
}


// Test the functions

var points = [ [3, 4], [3, 1], [1, 1] ];

var reflected = reflectAllInX(points);
var rotated = rotateAll90cw(points);

console.log(reflected);  // [ [3, -4], [3, -1], [1, -1] ]
console.log(rotated);    // [ [4, -3], [1, -3], [1, -1] ]

http://jsbin.com/leyosu/edit?js,console

Transformations in Action

You can see a demo of the transformations over on JS Bin. Click the buttons to perform the transformations.

http://output.jsbin.com/tudoji

Transformations demo on JS Bin

Data structure JavaScript

Opinions expressed by DZone contributors are their own.

Related

  • TypeScript: Useful Features
  • useState() vs. useRef(): Understand the Technical Difference
  • A Comprehensive Guide on JavaScript Array Map Method
  • JSON Minify Full Guideline: Easy For You

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!