Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / April 2008

Tip: Looking for answers? Try searching our database.

How can I determine if a point is a given distance from a second     point?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tom P. - 31 Mar 2008 19:24 GMT
I am writing a drawing program but I want to keep the scale down
(there could end up being several hundred objects on the screen).

I want to limit the points collected to a certain distance from other
points already collected, in other words, if you're drawing a line it
will only record points on the line every 6 pixels. How do I determine
how far away one point is from another? if they are restricted to
straight lines that's fine but as soon as they draw at an angle I'm
faced with determining how far away one point is from another in co-
ordinate space.

Any help would be appreciated.

Tom P.
thomasnguyencom - 31 Mar 2008 19:58 GMT
> I am writing a drawing program but I want to keep the scale down
> (there could end up being several hundred objects on the screen).
[quoted text clipped - 10 lines]
>
> Tom P.

Point x = new Point(1, 2);
Point y = new Point(3, 4);
double distance = Point.Distance(x, y);

I hope that helps.

goodluck,
-tom
Tom P. - 31 Mar 2008 20:15 GMT
> > I am writing a drawing program but I want to keep the scale down
> > (there could end up being several hundred objects on the screen).
[quoted text clipped - 19 lines]
> goodluck,
> -tom

You're kidding?  All the crap I went through and it's already a
framework method?  I could choke myself.

Thank you very much. I'm sorry I didn't look harder.

Tom P.
Israel - 31 Mar 2008 20:15 GMT
> > I am writing a drawing program but I want to keep the scale down
> > (there could end up being several hundred objects on the screen).
[quoted text clipped - 19 lines]
> goodluck,
> -tom

If the distance cutoff is constant you're going to be executing this
many times or for many points you might want to do the math manually
and then compare against distance squared instead e.g. distance^2 =
(x2-x1)^2 + (y2-y1)^2
Tom P. - 31 Mar 2008 20:33 GMT
> > > I am writing a drawing program but I want to keep the scale down
> > > (there could end up being several hundred objects on the screen).
[quoted text clipped - 24 lines]
> and then compare against distance squared instead e.g. distance^2 =
> (x2-x1)^2 + (y2-y1)^2

This is what I'm going to be using. I'll just do the math myself. I
never did find the method referred to, I don't know where tom got it
but he's got me beat.

Thanks for the help guys.

Tom P.
thomasnguyencom - 31 Mar 2008 22:58 GMT
> > > > I am writing a drawing program but I want to keep the scale down
> > > > (there could end up being several hundred objects on the screen).
[quoted text clipped - 32 lines]
>
> Tom P.

Sorry, my copy and paste sucks. I created a Point class that includes
the math for it. I wish there was a built-in functionality.
Tom P. - 31 Mar 2008 20:29 GMT
> > I am writing a drawing program but I want to keep the scale down
> > (there could end up being several hundred objects on the screen).
[quoted text clipped - 19 lines]
> goodluck,
> -tom

On second thought... no it doesn't help.

I'm not finding that static method. Am I missing a namespace or
something?

Tom P.
Peter Webb - 01 Apr 2008 03:19 GMT
>> > I am writing a drawing program but I want to keep the scale down
>> > (there could end up being several hundred objects on the screen).
[quoted text clipped - 26 lines]
>
> Tom P.

Distance between (x1,y1) and (x2,y2)
= sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2))

(Pythagoras's theorem).
JAM - 31 Mar 2008 20:20 GMT
> I am writing a drawing program but I want to keep the scale down
> (there could end up being several hundred objects on the screen).
[quoted text clipped - 10 lines]
>
> Tom P.

You are not very clear in the description, what exactly you want to
do. If you need precise distance in 3D (or 2D) space because you are
writing CAD like vector based application then you need to use precise
phytagorean floating point based distance calculation. In order to
save time you can skip the square root and instead of distance limit
of 6.0 use it's square value 36.0 to eliminate points recorded "too
close" to the previous. The problem arises when your drafting program
allows for curves that can loop and intersect itself, because then the
points that are close geometrically might not neccesairly belong to
the subsequent recording and both should be recorded to preserve curve
shape. That can be achieved by comparing every recorded point only
with the very previous one accepted, which has extra benefit of also
being efficient and fast assuming subsequent points define just one
curve. If this is not your intention that you might need to compare
each recorded entry to every point previously accepted which will be
expensive. In such case I would rather divide the entire 2D working
space into cells of 6 x 6 pixels (in 3D case you would need 6 x 6 x 6
cell) and keep status of each cell in the array. If for example your
2D working space has 600 x 600 pixels size than you would need array
of 100 x 100 cells to record your points. If some newly recorded point
falls into the particular cell that is already occupied then it would
be rejected and obviously accepted otherwise. If the array approach is
too expensive in memory you can limit yourslef to comparing just delta
x, delta y (and delta z for 3D) between points (subsequent or
alldepending on your needs). If every absolute value of delta between
comapared points falls below 6 than the point is rejected or otherwise
accepted The downside of this approach is that it would keep 6 pixels
distance along x axis or along y axis, but the limit would be larger
along some angle to the primary coordinate system. In the extreme 45
degrees you would eliminate points that are less than 8.48 pixels
apart (2.48 pixels "too much" based on your criteria).  This later
approach would be more suited for the drafting application that is
raster based where exact distance at an angle to the raster pixel
matrix has no good physical meaning since display pixes fall into the
integer rectangular grid. I guess one could use rounding functions to
simulate circle but that would probably lead to the expensive
algorithm without much gain in display quality.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.