Finding closest possible coordinatesHow to check if a possible path exist?How do I find out which DOM element has the focus?Where can I find documentation on formatting a date in JavaScript?Easy interview question got harder: given numbers 1..100, find the missing number(s)Getting the closest string matchFind an integer not among four billion given onesFind object by id in an array of JavaScript objectsHow to find time complexity of an algorithmIs it possible to apply CSS to half of a character?Detecting 3D collision between a triangle and a rotating sphereFinding closest x,y coordinates

How to not starve gigantic beasts

Checks user level and limit the data before saving it to mongoDB

Was there a Viking Exchange as well as a Columbian one?

Implications of cigar-shaped bodies having rings?

Size of electromagnet needed to replicate Earth's magnetic field

What are the characteristics of a typeless programming language?

What happened to Captain America in Endgame?

How do I check if a string is entirely made of the same substring?

Is it idiomatic to construct against `this`

Is this homebrew Wind Wave spell balanced?

Two field separators (colon and space) in awk

Which big number is bigger?

Elements other than carbon that can form many different compounds by bonding to themselves?

Dynamic SOQL query relationship with field visibility for Users

How come there are so many candidates for the 2020 Democratic party presidential nomination?

How to fry ground beef so it is well-browned

How to have a sharp product image?

What term is being referred to with "reflected-sound-of-underground-spirits"?

Providing evidence of Consent of Parents for Marriage by minor in England in early 1800s?

How does Captain America channel this power?

Multiple options vs single option UI

A Paper Record is What I Hamper

How could Tony Stark make this in Endgame?

555 timer FM transmitter



Finding closest possible coordinates


How to check if a possible path exist?How do I find out which DOM element has the focus?Where can I find documentation on formatting a date in JavaScript?Easy interview question got harder: given numbers 1..100, find the missing number(s)Getting the closest string matchFind an integer not among four billion given onesFind object by id in an array of JavaScript objectsHow to find time complexity of an algorithmIs it possible to apply CSS to half of a character?Detecting 3D collision between a triangle and a rotating sphereFinding closest x,y coordinates






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I have x,y coordinates board like this:



enter image description here



the board is up to 100x100.



Where myPosition is gold, destinations is green and collisions is red. myPosition is object destinations and collisions are array of objects:



let myPosition=x:0,y:0;
let destinations = [x: 0, y: 5, x: 2, y: 0, x: 2, y: 2];
let collisions = [x: 1, y: 0,x: 1, y: 1,x: 1, y: 2,x: 1, y: 3,x: 1, y: 4,x: 2, y: 1,x: 2, y: 0,x: 2, y: 1]


With this code (live demo) I'm able to find nearest destination but it doesn't know about collisions at all. I can not figure how to write algorithm which additionally would check for collision and give ouput 0,5 in above scenerio.



There is also assumption that we can not move diagonally.



I found this SO answer which seems to provide answer to my question but I can not get it to work with my input arrays.










share|improve this question






















  • This sounds to me like you're looking for a pathing solution, the first that comes to mind is a-star ( a* ) see: buildnewgames.com/astar . a* is also mentioned in the SO answer you referenced

    – Steven Stark
    Mar 22 at 17:44












  • even your current solution is wrong as it calculates squared distance so it allows diagonal moves. You want to do something like bfs

    – juvian
    Mar 22 at 17:46











  • I found A* library but in usage github.com/prettymuchbryce/easystarjs#usage there is grid with 0 and 1 where I believe 0 is walkable and 1 is not. Can I somehow convert my object/arrays to grid like this?

    – BT101
    Mar 22 at 18:46

















0















I have x,y coordinates board like this:



enter image description here



the board is up to 100x100.



Where myPosition is gold, destinations is green and collisions is red. myPosition is object destinations and collisions are array of objects:



let myPosition=x:0,y:0;
let destinations = [x: 0, y: 5, x: 2, y: 0, x: 2, y: 2];
let collisions = [x: 1, y: 0,x: 1, y: 1,x: 1, y: 2,x: 1, y: 3,x: 1, y: 4,x: 2, y: 1,x: 2, y: 0,x: 2, y: 1]


With this code (live demo) I'm able to find nearest destination but it doesn't know about collisions at all. I can not figure how to write algorithm which additionally would check for collision and give ouput 0,5 in above scenerio.



There is also assumption that we can not move diagonally.



I found this SO answer which seems to provide answer to my question but I can not get it to work with my input arrays.










share|improve this question






















  • This sounds to me like you're looking for a pathing solution, the first that comes to mind is a-star ( a* ) see: buildnewgames.com/astar . a* is also mentioned in the SO answer you referenced

    – Steven Stark
    Mar 22 at 17:44












  • even your current solution is wrong as it calculates squared distance so it allows diagonal moves. You want to do something like bfs

    – juvian
    Mar 22 at 17:46











  • I found A* library but in usage github.com/prettymuchbryce/easystarjs#usage there is grid with 0 and 1 where I believe 0 is walkable and 1 is not. Can I somehow convert my object/arrays to grid like this?

    – BT101
    Mar 22 at 18:46













0












0








0








I have x,y coordinates board like this:



enter image description here



the board is up to 100x100.



Where myPosition is gold, destinations is green and collisions is red. myPosition is object destinations and collisions are array of objects:



let myPosition=x:0,y:0;
let destinations = [x: 0, y: 5, x: 2, y: 0, x: 2, y: 2];
let collisions = [x: 1, y: 0,x: 1, y: 1,x: 1, y: 2,x: 1, y: 3,x: 1, y: 4,x: 2, y: 1,x: 2, y: 0,x: 2, y: 1]


With this code (live demo) I'm able to find nearest destination but it doesn't know about collisions at all. I can not figure how to write algorithm which additionally would check for collision and give ouput 0,5 in above scenerio.



There is also assumption that we can not move diagonally.



I found this SO answer which seems to provide answer to my question but I can not get it to work with my input arrays.










share|improve this question














I have x,y coordinates board like this:



enter image description here



the board is up to 100x100.



Where myPosition is gold, destinations is green and collisions is red. myPosition is object destinations and collisions are array of objects:



let myPosition=x:0,y:0;
let destinations = [x: 0, y: 5, x: 2, y: 0, x: 2, y: 2];
let collisions = [x: 1, y: 0,x: 1, y: 1,x: 1, y: 2,x: 1, y: 3,x: 1, y: 4,x: 2, y: 1,x: 2, y: 0,x: 2, y: 1]


With this code (live demo) I'm able to find nearest destination but it doesn't know about collisions at all. I can not figure how to write algorithm which additionally would check for collision and give ouput 0,5 in above scenerio.



There is also assumption that we can not move diagonally.



I found this SO answer which seems to provide answer to my question but I can not get it to work with my input arrays.







javascript algorithm collision nearest-neighbor






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 22 at 17:40









BT101BT101

2092626




2092626












  • This sounds to me like you're looking for a pathing solution, the first that comes to mind is a-star ( a* ) see: buildnewgames.com/astar . a* is also mentioned in the SO answer you referenced

    – Steven Stark
    Mar 22 at 17:44












  • even your current solution is wrong as it calculates squared distance so it allows diagonal moves. You want to do something like bfs

    – juvian
    Mar 22 at 17:46











  • I found A* library but in usage github.com/prettymuchbryce/easystarjs#usage there is grid with 0 and 1 where I believe 0 is walkable and 1 is not. Can I somehow convert my object/arrays to grid like this?

    – BT101
    Mar 22 at 18:46

















  • This sounds to me like you're looking for a pathing solution, the first that comes to mind is a-star ( a* ) see: buildnewgames.com/astar . a* is also mentioned in the SO answer you referenced

    – Steven Stark
    Mar 22 at 17:44












  • even your current solution is wrong as it calculates squared distance so it allows diagonal moves. You want to do something like bfs

    – juvian
    Mar 22 at 17:46











  • I found A* library but in usage github.com/prettymuchbryce/easystarjs#usage there is grid with 0 and 1 where I believe 0 is walkable and 1 is not. Can I somehow convert my object/arrays to grid like this?

    – BT101
    Mar 22 at 18:46
















This sounds to me like you're looking for a pathing solution, the first that comes to mind is a-star ( a* ) see: buildnewgames.com/astar . a* is also mentioned in the SO answer you referenced

– Steven Stark
Mar 22 at 17:44






This sounds to me like you're looking for a pathing solution, the first that comes to mind is a-star ( a* ) see: buildnewgames.com/astar . a* is also mentioned in the SO answer you referenced

– Steven Stark
Mar 22 at 17:44














even your current solution is wrong as it calculates squared distance so it allows diagonal moves. You want to do something like bfs

– juvian
Mar 22 at 17:46





even your current solution is wrong as it calculates squared distance so it allows diagonal moves. You want to do something like bfs

– juvian
Mar 22 at 17:46













I found A* library but in usage github.com/prettymuchbryce/easystarjs#usage there is grid with 0 and 1 where I believe 0 is walkable and 1 is not. Can I somehow convert my object/arrays to grid like this?

– BT101
Mar 22 at 18:46





I found A* library but in usage github.com/prettymuchbryce/easystarjs#usage there is grid with 0 and 1 where I believe 0 is walkable and 1 is not. Can I somehow convert my object/arrays to grid like this?

– BT101
Mar 22 at 18:46












1 Answer
1






active

oldest

votes


















0














I used pathFinding.js library and finding is very simple:



currentPath = finder.findPath(heroCoords.x, 
heroCoords.y,
monstersCoords[i].x,
monstersCoords[i].y,
currentGrid);





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55305115%2ffinding-closest-possible-coordinates%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I used pathFinding.js library and finding is very simple:



    currentPath = finder.findPath(heroCoords.x, 
    heroCoords.y,
    monstersCoords[i].x,
    monstersCoords[i].y,
    currentGrid);





    share|improve this answer



























      0














      I used pathFinding.js library and finding is very simple:



      currentPath = finder.findPath(heroCoords.x, 
      heroCoords.y,
      monstersCoords[i].x,
      monstersCoords[i].y,
      currentGrid);





      share|improve this answer

























        0












        0








        0







        I used pathFinding.js library and finding is very simple:



        currentPath = finder.findPath(heroCoords.x, 
        heroCoords.y,
        monstersCoords[i].x,
        monstersCoords[i].y,
        currentGrid);





        share|improve this answer













        I used pathFinding.js library and finding is very simple:



        currentPath = finder.findPath(heroCoords.x, 
        heroCoords.y,
        monstersCoords[i].x,
        monstersCoords[i].y,
        currentGrid);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 24 at 13:30









        BT101BT101

        2092626




        2092626





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55305115%2ffinding-closest-possible-coordinates%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript