2

I am using scratch and am trying to find the shortest distance between two points and then draw a line between them.

Basically the program allows the user to click on two points on a map and then calculates the longitude and latitude for both points. This part is fine.

I then need to draw a line between the two points that takes into consideration that the map is flat and sometimes the line needs to go off the screen and back around to find the shortest line. It would be great if I could also get the line to curve between the points. Is there an equation that would allow me to do this?

Thanks

Charlie
  • 21
  • 3
  • I remember using Bresenham's algorithm with some success: http://en.wikipedia.org/wiki/Bresenham's_line_algorithm – Matt Sep 15 '13 at 13:06
  • You have the longitude and latitude for each point, so you have spherical coordinates. The shortest path on the sphere is on a great-circled of the sphere, that gives you the path in spherical coordinates. Use the inverse of your plane-to-longitude/latitude function to trace out the curve. – Codie CodeMonkey Sep 16 '13 at 09:29
  • 2
    Are your points 1000's of miles apart? If not, you could probably get away with using flat earth equations (aka Equirectangular approximation). If the points are far apart, you could use Haversine. These formula are located here: http://www.movable-type.co.uk/scripts/latlong.html – TreyA Sep 16 '13 at 10:17

1 Answers1

1

Have you considered using the minimum and maximum x and y coordinates to determine whether said point is closer to the wall or to the center. Then write a logic that figures out if it would be better to either go off the grid and into the other side, then continuing to the point. Or, to just directly draw a line between the two points. I would do it for you but I think you would appreciate the challenge. As for drawing the line, use an invisible sprite that goes to one, puts the pen down, and moves to the other line.

Tom Cupis
  • 316
  • 3
  • 24
Domi
  • 51
  • 2
  • 5