Obtain the grid's coordinates

I am seeking assistance with creating a javascript function that can retrieve coordinates within the specified range of a start and endpoint on a grid. Below is a representation of the grid structure:

 _______________________
|       |       |       |
| -1,-1 |  0,-1 |  1,-1 |
|_______|_______|_______|
|       |       |       |
| -1,0  |  0,0  |  1,0  |
|_______|_______|_______|
|       |       |       |
| -1,1  |  0,1  |  1,1  |
|_______|_______|_______|

For example, if I specify the range from coordinates (-1,-1) to (0,0), I would expect to receive the following values: (-1,-1), (0,-1), (-1,0), (0,0).

Although these are basic mathematics principles, I am struggling to find an optimal solution for this problem.

Answer №1

If you need to generate a list of coordinates within a specified range, you can use the following function:

function GenerateCoordinatesInRange(xStart, xEnd, yStart, yEnd) {
    var currentX = xStart;
    var currentY = yStart;
    var result = []; // Placeholder for storing coordinates    

    while (currentX <= xEnd) {
        while (currentY <= yEnd) {
            result.push({ x: currentX, y: currentY });
            currentY++;
        }
        currentX++;
        currentY = yStart;
    }

    return result;
}

This function will provide you with an array of coordinates in the format shown below:

[
    0: { x: -1, y: -1},
    1: { x: -1, y: 0},
    ...
]

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Leveraging setter methods within knockoutjs bindings allows for efficient data manipulation

As the day comes to a close, my mind is winding down for the night. I've been diving into the world of setters when dynamically binding to Html elements and trying to wrap my head around it. While I have read through several examples, these URLs below ...

provide an element reference as an argument to a directive

I am trying to figure out how to pass an element reference to a directive. I know that I can get the reference of the element where the directive is applied using private _elemRef: ElementRef but my goal is to pass the reference of another element to the ...

To successfully display the data on my ChartJS Line graph, I have to first click on the color legend

I am currently using Chart JS to visualize sensor data retrieved from a firebase firestore database. I've come across an unusual issue where my chart fails to display the data initially >> https://i.sstatic.net/qD6Sd.jpg but after performing two ...

Tips on connecting the endpoints of two parallel quadratic Bezier curves that both begin with a MoveTo command

I am currently working on creating a Sankey Diagram from scratch using VueJS and SVG. I have encountered challenges in closing the paths of two parallel quadratic Bezier curve paths from nodes to nodes. For instance, after some additional calculations, I ...

What is the correct way to define a function parameter when using AngularJS Emit?

I've been exploring the emit feature in AngularJS and had a question about passing a function as an argument. In the Parent Controller: $scope.$on("getChildFunction", function(event, func) { console.log("The function is...", func); }) In the Ch ...

Critical bug discovered in fundamental Vue.js component by Internet Explorer

My Vue.js-powered application is performing flawlessly in all web browsers, except for one... Upon attempting to launch it on Internet Explorer, a frustrating error appears: An anticipated identifier in vue.min.js, line 6 character 4872 Locating the spe ...

Tips for maintaining the data on a page continuously updating in AngularJS

I have this code snippet: $cookieStore.put('profileData', $scope.profileData); var profileData = $cookieStore.get('profileData'); $scope.init = function(){ var profileData = $cookieStore.get('pr ...

Simulated script in a different component

I am looking to simulate the functionality of Amazon AWS S3 getObject The specific code I aim to test is located in helper.js var AWS = require('aws-sdk'); var s3 = new AWS.S3(); exports.get_data_and_callback = function(callback, extra){ s3. ...

Use CSS media queries to swap out the map for an embedded image

Looking to make a change on my index page - swapping out a long Google Map for an embedded image version on mobile. The map displays fine on desktop, but on mobile it's too lengthy and makes scrolling difficult. I already adjusted the JS setting to "s ...

Issue with Firefox mobile's camera functionality

I am facing an issue with my web app for mobile devices where I am trying to display the mobile camera using Firefox mobile browser. I am using nodejs with express as a server and connecting to the server via localhost with my smartphone. Although Firefox ...

Arranging HTML elements using JavaScript or jQuery

Something seems off. The sorting doesn't work as expected. Check out the JavaScript code below: function sortDescending(a, b) { var date1 = $(a).data('date'); var date2 = $(b).data('date'); return date1 > date2; } ...

Utilize ModifyDOM to erase text triggered by selecting a radio button

I have created a form that includes four radio buttons along with a reset button to clear the form. When a radio button is selected, information is displayed using "displayText". I am looking to add an onclick handler to trigger a function that will delete ...

The button text in Bootstrap 5 is black instead of white as shown in their demo

During the installation of Bootstrap 5, I encountered an issue where many of my buttons are displaying a black font instead of the expected white font as shown in the Bootstrap 5 Documentation For instance, the .btn-primary button on the official Bootstra ...

Utilizing AJAX and JavaScript to generate a table using the AJAX response and placing it within a <div> element

I am currently passing the response of this action in text form, but I would like to display it in a table format. Is there a way to do this? function loadAditivos(){ $('#aditivoAbertoInformacoesTexto').html('<div id="loaderMaior ...

Refreshing a webpage to accurately display changes made in a CRUD application without the need for a hard reset

My to-do app is almost fully functional - I can create items, mark them as completed, and delete them using a delete button. However, there's one issue: when I delete an item, the page doesn't update in real-time. I have to manually refresh the p ...

Ways to retrieve the ID attribute value of the third ancestor above

How can I retrieve the ID attribute of the 3rd parent element when a link is clicked? Take for example: <div id="First Div"> <div class="class A"> <div class="class B"></div> <div class="class C"></div> &l ...

Guide to programmatically configuring meta title, description, and image in a Vue.js project with the help of Vue Unhead and Vue Meta

I'm currently developing a Vue.js project where I need to dynamically set the meta title, description, and image based on data fetched from an API. To handle the meta tags, I am utilizing Vue Vue Unhead along with Vue Meta. Below is a snippet of the r ...

My AJAX request seems to be redirecting to my PHP file instead of simply returning the response text. What could be causing this issue?

I am currently working on a project for my Web Engineering course and I am incorporating jQuery to execute an AJAX request to my PHP file using the POST method. The task specifies that "each time the [submit] button is pressed, the form data should be disp ...

Export multiple variables at once

Within TypeScript, I have Class1 defined in class.ts, along with some functions from helper.ts and variables from variables.ts: For instance, the content of variables.ts is as follows: export const test1 = 'test1'; export const test2 = 0; expor ...

Error: Headers cannot be set once they have already been sent

My app.js file has the following code snippet: app.use(function(req, res, next){ if(!req.user){ return res.redirect('/login_'); } next(); }) Everything seems correct so far. In my route/index.js file, I have the following code: rout ...