Get Coordinates around a point

Eugen

I have trouble to find a good algorithm to get all points around a point(the mid point included) in a coordinate system.

Example:

  1 1   2 1   3 1

  1 2   2 2   3 2

  1 3   2 3   3 3

I have the above grid, so for example if I input: get_around_coordinates(2, 2, 1)

It would return an array which would look like this [[1, 1], [2, 1], [3, 1], [1, 2], [2, 2], [2, 3], [3, 1], [3,2], [3, 3]]

This is how my method looks like:

 def get_around_coordinates(px, py, radius)
    coord = []
    start_x = px - radius
    start_y = py - radius
    (radius * 2 + 1).times do |x|
      (radius * 2 + 1).times do |y|
        coord.push([start_x + x, start_y + y])
      end
    end
    return coord
  end

I hope that someone can give me something better than this.

tokland

Using Array#product:

def get_around_coordinates(px, py, radius)
  xs = (px - radius..px + radius).to_a
  ys = (py - radius..py + radius).to_a
  xs.product(ys)
end

Now let's imagine Ruby lacked this method. The functional approach would be a OOP list-comprehension (flat_map + [flat_map...] + map): xs.flat_map { |x| ys.map { |y| [x, y] } }.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get Coordinates around a point

From Dev

Get the point coordinates of a CombinedGeometry

From Dev

Python - Get the coordinates of densest point

From Dev

get coordinates of arrow of a line point

From Dev

Calculating integrer x and y coordinates around point by range variable

From Dev

How to get the world coordinates of a point in a child actor?

From Dev

how to get tap point (coordinates) in iOS swift

From Dev

Get coordinates from a lambda function point

From Dev

How to get the world coordinates of a point in a child actor?

From Dev

Get coordinates of intersecting point of two trend lines

From Dev

How to get point coordinates in same projection as raster

From Dev

Creating a buffer around a geographic point and then checking whether a list of coordinates is inside that buffer?

From Dev

Is there a way to get all location point (lat and lon) around a specific country?

From Dev

OpenLayers 2 - How to get coordinates for selected feature (point)

From Dev

How to get coordinates of point where drawing arc ended in canvas?

From Dev

How to get coordinates about a point in Google Maps API iOS?

From Dev

How to get coordinates about a point in Google Maps API iOS?

From Dev

Rotating a point around another point

From Dev

Python: Coordinates Boxes around Polyline

From Dev

OpenGl Rotation around point

From Dev

Getting Coordinates of a Point in a circle

From Dev

Getting Coordinates of a Point in a circle

From Dev

Finding coordinates of a point on a line

From Dev

coordinates point is in jmapviewer polygon?

From Dev

Why am I'm getting an Error 207 (invalid floating point operation)/ How to get around it?

From Dev

How to get image pixel coordinates in java, by pointing mouse to a specific point on image?

From Dev

How to get image pixel coordinates in java, by pointing mouse to a specific point on image?

From Dev

How can I get the point coordinates from a vector layer in openlayers3?

From Dev

Use nth_element to get k-th point(by x-coordinates), but it doesn't work

Related Related

  1. 1

    Get Coordinates around a point

  2. 2

    Get the point coordinates of a CombinedGeometry

  3. 3

    Python - Get the coordinates of densest point

  4. 4

    get coordinates of arrow of a line point

  5. 5

    Calculating integrer x and y coordinates around point by range variable

  6. 6

    How to get the world coordinates of a point in a child actor?

  7. 7

    how to get tap point (coordinates) in iOS swift

  8. 8

    Get coordinates from a lambda function point

  9. 9

    How to get the world coordinates of a point in a child actor?

  10. 10

    Get coordinates of intersecting point of two trend lines

  11. 11

    How to get point coordinates in same projection as raster

  12. 12

    Creating a buffer around a geographic point and then checking whether a list of coordinates is inside that buffer?

  13. 13

    Is there a way to get all location point (lat and lon) around a specific country?

  14. 14

    OpenLayers 2 - How to get coordinates for selected feature (point)

  15. 15

    How to get coordinates of point where drawing arc ended in canvas?

  16. 16

    How to get coordinates about a point in Google Maps API iOS?

  17. 17

    How to get coordinates about a point in Google Maps API iOS?

  18. 18

    Rotating a point around another point

  19. 19

    Python: Coordinates Boxes around Polyline

  20. 20

    OpenGl Rotation around point

  21. 21

    Getting Coordinates of a Point in a circle

  22. 22

    Getting Coordinates of a Point in a circle

  23. 23

    Finding coordinates of a point on a line

  24. 24

    coordinates point is in jmapviewer polygon?

  25. 25

    Why am I'm getting an Error 207 (invalid floating point operation)/ How to get around it?

  26. 26

    How to get image pixel coordinates in java, by pointing mouse to a specific point on image?

  27. 27

    How to get image pixel coordinates in java, by pointing mouse to a specific point on image?

  28. 28

    How can I get the point coordinates from a vector layer in openlayers3?

  29. 29

    Use nth_element to get k-th point(by x-coordinates), but it doesn't work

HotTag

Archive