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

T

Tom P.

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.
 
T

thomasnguyencom

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.

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

I hope that helps.

goodluck,
-tom
 
T

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

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.
 
I

Israel

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

I hope that helps.

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
 
J

JAM

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.

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.
 
T

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

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

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

Tom P.
 
T

Tom P.

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

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.
 
T

thomasnguyencom

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.

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.
 
P

Peter Webb

Tom P. said:
On second thought... no it doesn't help.

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

Tom P.

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

(Pythagoras's theorem).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top