Login or Sign Up to become a member!
LessThanDot Site Logo

LessThanDot

A Technical Community for IT Professionals

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Highly Rated Users

Forum
No Posts Rated

Top 50
Given
Received

Forum Statistics

Users
Members:
1592
Members Online:
1
Guests Online:
11

Total Post History
Posts:
80553
Topics:
18446

7-Day Post History
New Posts:
4
New Topics:
1
Active Topics:
3

Our newest member
marykee

Other

FAQ
All times are UTC [ DST ]

Google Ads

Puzzle 23 - Distance to line segment

Mind Boggling Puzzles, to keep that grey matter in shape...
Forum rules
Always post answers in a "Hidecode" tag, so that others have a chance to answer the question too.
Please wait...

Puzzle 23 - Distance to line segment

Postby gmmastros on Fri Feb 20, 2009 4:38 pm

Calculate the shortest distance from a point to a line segment.

Image


Image

Given the coordinates of the line segment (shown in blue), calculate the shortest distance to a given point (also shown in blue). The distance that you are to calculate is represented by the red line. If the point is 'outside' the line segment, meaning, you cannot draw a perpendicular line to it, then you should return the distance from the given point to the closest end point (shown in the second image).

The calculations are relatively simple. No points will be rewarded for incorrect distances (within reasonable rounding issues). Max points will be awarded for correct results and fast performance.
-George
User avatar
gmmastros
LTD Admin
LTD Admin
LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616
LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616
LTD Gold - Rating: 1616LTD Gold - Rating: 1616
 
Posts: 2327
Joined: Tue Oct 09, 2007 5:19 pm
Unrated

Re: Puzzle 23 - Distance to line segment

Postby ChibaPet on Fri Feb 20, 2009 10:50 pm

Here's mine. I'm bad at math, so I didn't use any of those scary geometry things, except for a distance formula. I'd love to have implemented this without sqrt or exponentiation, but I'm not sure how.

Code is hidden, SHOW
ChibaPet
Apprentice
Apprentice
LTD Bronze - Rating: 4
 
Posts: 5
Joined: Fri Feb 20, 2009 10:47 pm
Unrated

Re: Puzzle 23 - Distance to line segment

Postby ChibaPet on Fri Feb 20, 2009 11:06 pm

ChibaPet wrote:Here's mine. I'm bad at math, so I didn't use any of those scary geometry things, except for a distance formula. I'd love to have implemented this without sqrt or exponentiation, but I'm not sure how.


Hm, yeah, my solution fails if the points defining the line segment are listed in the reverse order. I'm not going to bother fixing it, but I should figure out which of a and b is closest to the origin, and have that determine which endpoint is checked against "<" and which is checked against ">". I also feel like there are common cases I'm missing, but maybe that would suffice.

Alright, I'll do it, or it's going to gnaw at me.

Code is hidden, SHOW


There. Now the order of points in the line segment doesn't matter, at the expense of two more distance calculations per problem.
ChibaPet
Apprentice
Apprentice
LTD Bronze - Rating: 4
 
Posts: 5
Joined: Fri Feb 20, 2009 10:47 pm

Re: Puzzle 23 - Distance to line segment

Postby AlexCuse on Fri Feb 20, 2009 11:26 pm

Nice work ChibaPet, and Welcome :D

I can't test it, but it looks about like what I had in mind (which is probably bad news for you!)
Say what you like about the tenets of National Socialism Dude, at least it's an ethos
User avatar
AlexCuse
LTD Admin
LTD Admin
LTD Gold - Rating: 1021LTD Gold - Rating: 1021LTD Gold - Rating: 1021LTD Gold - Rating: 1021LTD Gold - Rating: 1021
LTD Gold - Rating: 1021LTD Gold - Rating: 1021LTD Gold - Rating: 1021LTD Gold - Rating: 1021LTD Gold - Rating: 1021
LTD Gold - Rating: 1021
 
Posts: 5411
Joined: Tue Oct 09, 2007 5:26 pm
Location: Pennsylvania, US
Unrated

Re: Puzzle 23 - Distance to line segment

Postby koorogi on Sat Feb 21, 2009 12:37 am

I followed a link here from reddit and figured I'd give it a go. Here's my solution in C.

Code is hidden, SHOW
User avatar
koorogi
Newbie
Newbie
LTD Bronze - Rating: 13
 
Posts: 1
Joined: Sat Feb 21, 2009 12:29 am

Re: Puzzle 23 - Distance to line segment

Postby chrissie1 on Sat Feb 21, 2009 8:49 am

ChibaPet wrote:Here's mine. I'm bad at math, so I didn't use any of those scary geometry things, except for a distance formula. I'd love to have implemented this without sqrt or exponentiation, but I'm not sure how.


Sorry to sound stupid but what language is that you are using.
pink fuzzy slippers
User avatar
chrissie1
Senior Guru
Senior Guru
LTD Gold - Rating: 2088LTD Gold - Rating: 2088LTD Gold - Rating: 2088LTD Gold - Rating: 2088LTD Gold - Rating: 2088
LTD Gold - Rating: 2088LTD Gold - Rating: 2088LTD Gold - Rating: 2088LTD Gold - Rating: 2088LTD Gold - Rating: 2088
LTD Gold - Rating: 2088LTD Gold - Rating: 2088
 
Posts: 9348
Joined: Wed Oct 10, 2007 7:18 pm
Location: Belgium
Unrated

Re: Puzzle 23 - Distance to line segment

Postby damber on Sat Feb 21, 2009 1:30 pm

chrissie1 wrote:
ChibaPet wrote:Here's mine. I'm bad at math, so I didn't use any of those scary geometry things, except for a distance formula. I'd love to have implemented this without sqrt or exponentiation, but I'm not sure how.


Sorry to sound stupid but what language is that you are using.


ummm... I think my sarcasm filter is a bit wonky... are you serious? (I really can't tell for some reason :-( )

...or didn't you read the first line?
a smile is worth a thousand kind words, so smile, it's easy! :-)


CODE: $5
WORKING CODE: $500
PROPERLY DESIGNED & WORKING CODE: Priceless
User avatar
damber
LTD Admin
LTD Admin
LTD Silver - Rating: 660LTD Silver - Rating: 660LTD Silver - Rating: 660LTD Silver - Rating: 660LTD Silver - Rating: 660
LTD Silver - Rating: 660LTD Silver - Rating: 660LTD Silver - Rating: 660LTD Silver - Rating: 660LTD Silver - Rating: 660
 
Posts: 3134
Joined: Tue Oct 09, 2007 1:48 pm
Location: North Wales, UK
Unrated

Re: Puzzle 23 - Distance to line segment

Postby chrissie1 on Sat Feb 21, 2009 2:28 pm

damber wrote:
chrissie1 wrote:
ChibaPet wrote:Here's mine. I'm bad at math, so I didn't use any of those scary geometry things, except for a distance formula. I'd love to have implemented this without sqrt or exponentiation, but I'm not sure how.


Sorry to sound stupid but what language is that you are using.


ummm... I think my sarcasm filter is a bit wonky... are you serious? (I really can't tell for some reason :-( )

...or didn't you read the first line?


ah math is the language. Didn't know that was a programming language. Or am I misstaken.

My limited English tell me math is short fo mathematics.

ah now I see it's perl.

Nope I missed that one.

I changed the hidecode tags to reflect that for other people like me.
pink fuzzy slippers
User avatar
chrissie1
Senior Guru
Senior Guru
LTD Gold - Rating: 2088LTD Gold - Rating: 2088LTD Gold - Rating: 2088LTD Gold - Rating: 2088LTD Gold - Rating: 2088
LTD Gold - Rating: 2088LTD Gold - Rating: 2088LTD Gold - Rating: 2088LTD Gold - Rating: 2088LTD Gold - Rating: 2088
LTD Gold - Rating: 2088LTD Gold - Rating: 2088
 
Posts: 9348
Joined: Wed Oct 10, 2007 7:18 pm
Location: Belgium
Unrated

Re: Puzzle 23 - Distance to line segment

Postby gmmastros on Sat Feb 21, 2009 5:30 pm

I've created an executable to perform this calculation and also to show the task graphically. This should run on any windows 2000+ operating system.

http://tinyurl.com/b66rfr
-George
User avatar
gmmastros
LTD Admin
LTD Admin
LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616
LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616
LTD Gold - Rating: 1616LTD Gold - Rating: 1616
 
Posts: 2327
Joined: Tue Oct 09, 2007 5:19 pm
Unrated

Re: Puzzle 23 - Distance to line segment

Postby AlexCuse on Sat Feb 21, 2009 5:45 pm

Chrissie, hopefully there aren't too many people like you ;)

This was much trickier than I expected, but I learned a lot! Cut out some stuff in the LineSegment class (I hope to eventually make this able to determine the intersection point and create the intersecting line as well - perhaps this could be a puzzle for another week?) hopefully didn't break anything

Code is hidden, SHOW
Say what you like about the tenets of National Socialism Dude, at least it's an ethos
User avatar
AlexCuse
LTD Admin
LTD Admin
LTD Gold - Rating: 1021LTD Gold - Rating: 1021LTD Gold - Rating: 1021LTD Gold - Rating: 1021LTD Gold - Rating: 1021
LTD Gold - Rating: 1021LTD Gold - Rating: 1021LTD Gold - Rating: 1021LTD Gold - Rating: 1021LTD Gold - Rating: 1021
LTD Gold - Rating: 1021
 
Posts: 5411
Joined: Tue Oct 09, 2007 5:26 pm
Location: Pennsylvania, US
Unrated

Re: Puzzle 23 - Distance to line segment

Postby gmmastros on Sat Feb 21, 2009 6:04 pm

I'm trying to test the code submitted.

So far, I've only tested koorogi's code. His code produces the correct answer and is only slightly slower than my own code, which I will post later.

When I compile my code, it runs in 160 nano-seconds. Koorogi's code (converted to VB, sorry) runs in 300 nano-seconds.

Please, can everyone test their own performance. Run the code a couple million times and post the execution time (divided by the number of loops of course).
-George
User avatar
gmmastros
LTD Admin
LTD Admin
LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616
LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616
LTD Gold - Rating: 1616LTD Gold - Rating: 1616
 
Posts: 2327
Joined: Tue Oct 09, 2007 5:19 pm
Unrated

Re: Puzzle 23 - Distance to line segment

Postby AlexCuse on Sat Feb 21, 2009 6:26 pm

A tick is supposedly one hundred nanoseconds, I'm sure its' a bit more than that though. I'm too lazy to do anything more complicated.

These times are on a pretty slow laptop, with a lot of other stuff running.

[hide]Lets define the points - P1 and P2 will define the line segment, P3 will be the
point to get distance from...
P1 x:
100
P1 y:
100
P2 x:
200
P2 y:
200
P3 x:
350
P3 y:
795
Distance is: 613.616329639295
Duration is: 2590000 Ticks or 1.295 Ticks per execution


Would you like to try again? (Y/N)[/hide]
Say what you like about the tenets of National Socialism Dude, at least it's an ethos
User avatar
AlexCuse
LTD Admin
LTD Admin
LTD Gold - Rating: 1021LTD Gold - Rating: 1021LTD Gold - Rating: 1021LTD Gold - Rating: 1021LTD Gold - Rating: 1021
LTD Gold - Rating: 1021LTD Gold - Rating: 1021LTD Gold - Rating: 1021LTD Gold - Rating: 1021LTD Gold - Rating: 1021
LTD Gold - Rating: 1021
 
Posts: 5411
Joined: Tue Oct 09, 2007 5:26 pm
Location: Pennsylvania, US
Unrated

Re: Puzzle 23 - Distance to line segment

Postby gmmastros on Sat Feb 21, 2009 6:26 pm

Chibapet,

The code you posted works if the line is not horizontal or vertical.
-George
User avatar
gmmastros
LTD Admin
LTD Admin
LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616
LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616
LTD Gold - Rating: 1616LTD Gold - Rating: 1616
 
Posts: 2327
Joined: Tue Oct 09, 2007 5:19 pm
Unrated

Re: Puzzle 23 - Distance to line segment

Postby ChibaPet on Wed Feb 25, 2009 12:26 am

gmmastros wrote:Chibapet,

The code you posted works if the line is not horizontal or vertical.


Yar. Broken.

The following corrects that. It could search for point C being on the line which contains the line segment, but I guess that would only be worthwhile if it's a common case in the data.

Anyway, it's less pretty now, but more correct. Lucky I don't program spacecraft controls, I guess.

Code is hidden, SHOW
ChibaPet
Apprentice
Apprentice
LTD Bronze - Rating: 4
 
Posts: 5
Joined: Fri Feb 20, 2009 10:47 pm
Unrated

Re: Puzzle 23 - Distance to line segment

Postby gmmastros on Wed Feb 25, 2009 1:12 am

Here is my algorithm:

Code is hidden, SHOW


My code has many similarities to code already posted. The main difference is... I calculate the point where the 'distance' line intersects the line segment. This could be anywhere on the line or at one of the end points. From there... it's a simple distance calculation.
-George
User avatar
gmmastros
LTD Admin
LTD Admin
LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616
LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616
LTD Gold - Rating: 1616LTD Gold - Rating: 1616
 
Posts: 2327
Joined: Tue Oct 09, 2007 5:19 pm
Unrated

Re: Puzzle 23 - Distance to line segment

Postby gmmastros on Wed Feb 25, 2009 1:13 am

By the way.... I'd love to see someone post a javascript version. Anyone?
-George
User avatar
gmmastros
LTD Admin
LTD Admin
LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616
LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616
LTD Gold - Rating: 1616LTD Gold - Rating: 1616
 
Posts: 2327
Joined: Tue Oct 09, 2007 5:19 pm
Unrated

Re: Puzzle 23 - Distance to line segment

Postby ChibaPet on Wed Feb 25, 2009 2:54 pm

gmmastros wrote:Here is my algorithm:


Hm, I'm not understanding what you're doing with the slope... Math is not my strong point. But does that work? Comments in the code would be a welcome addition!

Also, near the end, I think you want the square root in your distance calculation... Is Sqr the square root in VB? A quick Google doesn't enlighten me overmuch about a VB built-in square root function, but the principle of least surprise says "Sqr" squares its argument.
ChibaPet
Apprentice
Apprentice
LTD Bronze - Rating: 4
 
Posts: 5
Joined: Fri Feb 20, 2009 10:47 pm
Unrated

Re: Puzzle 23 - Distance to line segment

Postby gmmastros on Wed Feb 25, 2009 3:20 pm

ChibaPet wrote:Is Sqr the square root in VB?.


Yes.

http://msdn.microsoft.com/en-us/library/aa263367(VS.60).aspx

I'll write a mathematical proof soon. So that (hopefully), it will be clear how this works. I wrote this code (actually, a slower, uglier version) several years ago, so I don't remember exactly how it was derived. If I recall correctly, I started with the equation for a couple of the lines, and then reduced the formulas a lot to get them in to this form. I tweaked them a lot to get them as fast as possible.
-George
User avatar
gmmastros
LTD Admin
LTD Admin
LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616
LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616
LTD Gold - Rating: 1616LTD Gold - Rating: 1616
 
Posts: 2327
Joined: Tue Oct 09, 2007 5:19 pm
Unrated

Re: Puzzle 23 - Distance to line segment

Postby gmmastros on Wed Feb 25, 2009 11:00 pm

Entering equations is not the 'most fun' thing to do, so I wrote it out long hand. I hope nobody has any problem reading this. :blush:

Xa, Ya is one point of the line segment and Xb,Yb is the other point. Xp,Yp is the point to calculate the distance for. Xi,Yi is the point on the line where the perpendicular 'distance' line intersect the line segment. In the drawing, it intersects within the end points, but it may end up being outside the end points. This mathematical proof does not take that in to account, but the VB code does.

Also.... shown below is the proof for the X intersect point. Calculations for the Y intersect point is not shown, but is sufficiently similar that is *probably* doesn't need to be shown.

Image

You'll notice that this matches the equation I use in my algorithm for the x-intercept (where the distance line intersects the line segment). Immediately after the calculations, I check the X and Y values to make sure they are on the line. If not, I set them to the closest point.

Then, at the end, I calculate the distance using the normal distance calculation.
-George
User avatar
gmmastros
LTD Admin
LTD Admin
LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616
LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616
LTD Gold - Rating: 1616LTD Gold - Rating: 1616
 
Posts: 2327
Joined: Tue Oct 09, 2007 5:19 pm
Unrated

Re: Puzzle 23 - Distance to line segment

Postby gmmastros on Sun Mar 01, 2009 12:47 am

For what it's worth, I found a bug in my code. If the line segment is actually a point (distance = 0, x and y's are the same), my code calculates the wrong distance, which should actually be a simple point to point distance. Anyway, here's the corrected code.

  1. Public Function PerpendicularDistanceToLine(ByVal xPoint As Single, ByVal yPoint As Single, ByVal xA As Single, ByVal yA As Single, ByVal xB As Single, ByVal yB As Single) As Single
  2.    
  3.     Dim Slope As Single
  4.    
  5.     Dim xInt As Single
  6.     Dim yInt As Single
  7.    
  8.     If xA = xB Then
  9.        
  10.         xInt = xA
  11.         yInt = yPoint
  12.        
  13.     ElseIf yA = yB Then
  14.        
  15.         xInt = xPoint
  16.         yInt = yA
  17.    
  18.     Else
  19.        
  20.         Slope = ((yB - yA) / (xB - xA))
  21.         xInt = (yPoint - yA + xA * Slope + xPoint / Slope) / (1 / Slope + Slope)
  22.         yInt = (xPoint - xA + yA * Slope + yPoint / Slope) / (1 / Slope + Slope)
  23.    
  24.     End If
  25.    
  26.     If xA < xB Then
  27.         If xInt < xA Then
  28.             xInt = xA
  29.         ElseIf xInt > xB Then
  30.             xInt = xB
  31.         End If
  32.     ElseIf xA > xB Then
  33.         If xInt < xB Then
  34.             xInt = xB
  35.         ElseIf xInt > xA Then
  36.             xInt = xA
  37.         End If
  38.     ElseIf xA = xB Then
  39.         xInt = xA
  40.     End If
  41.    
  42.     If yA < yB Then
  43.         If yInt < yA Then
  44.             yInt = yA
  45.         ElseIf yInt > yB Then
  46.             yInt = yB
  47.         End If
  48.     ElseIf yB < yA Then
  49.         If yInt < yB Then
  50.             yInt = yB
  51.         ElseIf yInt > yA Then
  52.             yInt = yA
  53.         End If
  54.     ElseIf yA = yB Then
  55.         yInt = yA
  56.     End If
  57.  
  58.     PerpendicularDistanceToLine = Sqr((xInt - xPoint) * (xInt - xPoint) + (yInt - yPoint) * (yInt - yPoint))
  59.    
  60. End Function
  61.  


The problem was right at the end where I check to see if the xInt is within xA and xB and is yInt is between yA and Yb. Notice the If/Else If blocks. I needed to add a special case where they are equal so that the intersect points would BOTH get set properly.
-George
User avatar
gmmastros
LTD Admin
LTD Admin
LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616
LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616LTD Gold - Rating: 1616
LTD Gold - Rating: 1616LTD Gold - Rating: 1616
 
Posts: 2327
Joined: Tue Oct 09, 2007 5:19 pm
Unrated

Re: Puzzle 23 - Distance to line segment

Postby ChibaPet on Mon Mar 02, 2009 8:10 pm

gmmastros wrote:For what it's worth, I found a bug in my code. If the line segment is actually a point (distance = 0, x and y's are the same), my code calculates the wrong distance, which should actually be a simple point to point distance.


Just tested - mine seems to get that right. I still need to time it, though.

Okay. It's consistently taking five seconds to run a million times. I'm using the original problem, so it's running close to the longest code path - no horizontal or vertical shortcuts. No point reordering, but that's trivial anyway.

I'll get some microsecond resolution soon, and I'll get an average time per run.
ChibaPet
Apprentice
Apprentice
LTD Bronze - Rating: 4
 
Posts: 5
Joined: Fri Feb 20, 2009 10:47 pm
Unrated

Re: Puzzle 23 - Distance to line segment

Postby Emtucifor on Mon Mar 02, 2009 8:14 pm

May I suggest that

ElseIf yA = yB Then

can just be

Else
God cries a little bit every time someone builds a database.
User avatar
Emtucifor
Guru
Guru
LTD Gold - Rating: 1030LTD Gold - Rating: 1030LTD Gold - Rating: 1030LTD Gold - Rating: 1030LTD Gold - Rating: 1030
LTD Gold - Rating: 1030LTD Gold - Rating: 1030LTD Gold - Rating: 1030LTD Gold - Rating: 1030LTD Gold - Rating: 1030
LTD Gold - Rating: 1030
 
Posts: 2835
Joined: Fri May 30, 2008 9:30 pm
Location: Bellingham, WA
Unrated

Re: Puzzle 23 - Distance to line segment

Postby natinja on Wed Mar 04, 2009 12:09 am

A solution based on a Python transcription of the Point class found in Squeak (http://squeak.org).

Code is hidden, SHOW


Trying to solve the same problem in Squeak is much easier since it already provides a Point class as part of the standard graphic primitives with all the methods for doing the calculation. Note the nice shorthand to instantiate a Point by sending the #@ message and the Y coordinate to a number.

Code is hidden, SHOW
natinja
Newbie
Newbie
LTD Bronze - Rating: 6
 
Posts: 2
Joined: Tue Mar 03, 2009 1:10 am

Re: Puzzle 23 - Distance to line segment

Postby GothGargoyle on Mon Mar 09, 2009 11:41 pm

Here's my solution in Java. It actually contains two solutions - the entire thing can basically be done with a built-in function, so as well as using that I went ahead and did the manual calculations for fun :)

Code is hidden, SHOW
GothGargoyle
Apprentice
Apprentice
LTD Bronze - Rating: 18
 
Posts: 5
Joined: Wed Mar 04, 2009 8:31 pm
Unrated

Re: Puzzle 23 - Distance to line segment

Postby damber on Tue Mar 10, 2009 8:29 pm

it would be interesting to see the performance of the builtin function vs the various manual methods here... if anyone has the time to run them through
a smile is worth a thousand kind words, so smile, it's easy! :-)


CODE: $5
WORKING CODE: $500
PROPERLY DESIGNED & WORKING CODE: Priceless
User avatar
damber
LTD Admin
LTD Admin
LTD Silver - Rating: 660LTD Silver - Rating: 660LTD Silver - Rating: 660LTD Silver - Rating: 660LTD Silver - Rating: 660
LTD Silver - Rating: 660LTD Silver - Rating: 660LTD Silver - Rating: 660LTD Silver - Rating: 660LTD Silver - Rating: 660
 
Posts: 3134
Joined: Tue Oct 09, 2007 1:48 pm
Location: North Wales, UK
Unrated

Next