DEV Community

Cover image for How do you define your points [x,y],{x,y}, x, y?
Slobi
Slobi

Posted on • Edited on

How do you define your points [x,y],{x,y}, x, y?

1. The Mix of Point Formats: [] or {x, y} or x, y

Over the years, I've found myself dealing with geometric functions that expect points as arrays, objects with structures like {x, y}, or even as separate x and y parameters. This mix of formats can make working with code a bit clunky, often requiring manual conversions.
The need for a standardized solution became apparent, prompting the creation of the Vector object.

2. Alternative solution

In the search for a solution to handle various point formats, an alternative approach is creating multiple versions of functions tailored to different formats. For instance, we have functions like distance12 and distanceXY designed to bridge the gap between different representations.

function distance12(a, b) {
    return distance(a[0], a[1], b[0], b[1]);
}

function distanceXY(a, b) {
    return distance(a.x, a.y, b.x, b.y);
}

function distance(aX, aY, bX, bY) {
    // Calculation logic for distance
    // between points (aX, aY) and (bX, bY)
    // ...
}
Enter fullscreen mode Exit fullscreen mode

While this method allows for specific handling of different formats, it can lead to code duplication and increased complexity. This is where the Vector object approach simplifies the process and enhances code readability and maintainability.

3. The Quest for a Unified Solution

After overcoming numerous challenges and iterations, the Vector object emerged as the definitive solution. Its flexibility allows for instantiation through various formats: new Vector(0, 1), new Vector([0, 1]), or new Vector({x: 0, y: 1}).

4. A Singular Interface, Many Possibilities:

The Vector object simplifies access to coordinates. Want x and y values? Access them directly with myVector.x and myVector.y.
Need a specific index? Retrieve it using array-like notation: myVector[3].
The Vector object seamlessly integrates with existing functions, enabling effortless transformations with spread syntax: myFunction(...myVector).

Now we can call any function like:

const pointA = new Vector(2, 3);
const pointB = new Vector(5, 7);

const distance12Result = distance12(pointA, pointB);

const distanceXYResult = distanceXY(pointA, pointB);

const distanceResult = distance(...pointA, ...pointB);
Enter fullscreen mode Exit fullscreen mode

and I think it is beautiful, especially proud of spread syntax

5. One format to rule them all

Vector represents a unified approach to points and vectors in JavaScript. It eliminates the need for manual format conversions, making code more readable, maintainable, and efficient. With standard operations like addition, subtraction, negation, normalization, and magnitude, Vector stands as the universal solution for vector-related tasks.

Embrace Vector and unlock a new level of elegance and efficiency in your projects.

You can find my library at: https://github.com/slobacartoonac/js_lib/blob/main/shapes/vector.js
With test cases: https://github.com/slobacartoonac/js_lib/blob/main/test/vec_test.js

What standard did you settled on or you use all of them like me?

Thank for reading!

Heroku

Amplify your impact where it matters most — building exceptional apps.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (2)

Collapse
 
slobodan4nista profile image
Slobi

Thank you 😊
Arrays support for N dimensional spaces so they are more versatile.
Having plane x, y cords on function is more performant, unfortunately not by a little.
I aggre that {x, y} is usually the rght answer.

Eliminate Context Switching and Maximize Productivity

Pieces.app

Pieces Copilot is your personalized workflow assistant, working alongside your favorite apps. Ask questions about entire repositories, generate contextualized code, save and reuse useful snippets, and streamline your development process.

Learn more

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay