Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7025665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:00:21+00:00 2026-05-28T00:00:21+00:00

I would like to write a function that returns closest grid node (as a

  • 0

I would like to write a function that returns closest grid node (as a point) of a triangular grid (made of equilateral triangles with a base width w, parallel to OX axis) closest to some point p. Grid starts in point s. Function signature should look like:

def snapToGrid(p,w,s):
    ...

for exapmle
snapToGrid([0.49,0],0.5,[0.0]) == [0.5,0]

any ideas how to start?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:00:22+00:00Added an answer on May 28, 2026 at 12:00 am

    EDIT:
    This answer was before knowing that the triangular grid was to be dynamically computed, I will leave it up for now, because it could be helpful, but it will not help with dynamic point generation.

    I do believe the answer to dynamically generating the points will rely on finding the height of a triangle and then realising that the nodes will only ever exist at multiples of that triangle height.
    This should take care of your X component.

    Finding the width of a triangle will also tell you the multiples at which the nodes occur along the Y axis, however, every second row of triangles will have the node point at 1/2 the width of the triangle.
    Finding the number of rows away from your origin your original point will be is the first step.

    This in turn will allow let you know whether to use triangle width as the the Y coordinate multiple, or triangle width+1/2 triangle width for the Y multiplier.

    Using these two values it should be easy to find the closest n*height and n*width combination that is closest to your point.

    Sorry about the wall of text, if you draw your grid on paper, you will see that the triangles form rows of nodes along the X axis, and columns (for even rows) and half columns (for odd rows).


    When finding the nearest point it usually doesn’t matter what type / shape the grid is. Logically the point you are closest to will belong to the shape your point is in. So to simplify things all you need to do is find the nearest point to yours, which is quite a simple problem.

    This seems like it would most easily be found by using a K-D Tree, where the split points are the X and Y coordinates of the grid. A simple binary search will then reveal the closest grid point to the coordinate you have input.

    The linked article has pseudo code for K-D Tree operations.


    You could also have a dictionary mapped to X coordinates, with each value containing a dictionary of y:name pairs that correspond to that X coordinate.

    This will allow you to search for a node in the following way:

    coordinate_x[<xvale>][<yvalue>]
    

    which will then return the node you are looking for, to get xvalue and yvalue simply do a nearest search through the dictionary keys like:

    for x in coordinate_x.keys():
        if x-point_x < min:
            make x-point the closest
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to write a PLSQL function that returns true if the domain
I'd like to write a function that would have some optional code to be
In Python, I'd like to write a function that would pretty-print its results to
I would like to write a cross-platform function in C++ that contains system calls.
I would like to write a mysql function that takes an integer as input
I would like to write a function that takes a 3d triangle (as 3
I would like to write a function that takes a function f as an
I would like to write an OCaml function which takes a URL and returns
Can non-local-returns be implemented in JavaScript, so that you could write something like: function
I would like to write a function that takes in 3 characters and increments

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.