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

Slobi @slobodan4nista

About: I am Slobodan, a developer with a passion for combining old and new technologies. I like to explore unique approach to building modern applications with a touch of the past.

Location:
Niš, Srbija
Joined:
Aug 18, 2018

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

Publish Date: Nov 5 '23
3 2

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!

Comments 2 total

  • Slobi
    SlobiNov 28, 2023

    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.

Add comment