SRL Archive - Help and Support

Distance() -- need the function definition


R0b0t1

2008-05-30 11:59:30

Soooo…. I'm making a program w/ function to move the mouse over a Bézier curve, but I don't know if the Mac libraries have a Distancce function.

If anyone knows the code that makes the function Distance work, or a function definition in the standard libraries, please post. The post can be in any language, preferably SCAR/Object Pascal or C++ with nice standards.

Freddy1990

2008-05-30 12:02:39

Aw cmon man, this is really basic math http://en.wikipedia.org/wiki/Distance

Killerdou

2008-05-30 12:07:00

unless he is talking about the distance of a curve, which is not so simple math, especially for a computer:P

R0b0t1

2008-05-30 12:07:03

*reads article* Nope, still don't get it.


Seriously, I know what some of those symbols mean, but I couldn't really write that in C++ or SCAR/Delphi.

Besides, which one to I pick? 1-, 2-, p-, or infinity- norm distance?


EDIT: Look, I didn't post here because I was lazy, I posted because I need help.

EDIT: I guess 1- or 2- norm, those seem to have relations to graphs…

Freddy1990

2008-05-30 12:27:02

Just regular distance between 2 points unless you need the distance over your entire curve… But I assume you mean the distance between 2 points…

mastaraymond

2008-05-30 12:38:10

"I just lolled" xD.

Pythagoras pl0x.

Wizzup?

2008-05-30 13:03:41

R0bot1, you should read Pythagoras
If you have two points, you can draw a triangle with those. (by drawing a third point. (x1 + (x2 - x1), y1 + (x2 - y1)), but you don't need the x1 + and y1 + because you are looking for delta x and delta y)


Because we are talking about the distance, we can use delta x and delta y.
Which means you can always use the absolute value of X1 - X2 and Y1 - Y2.

Using Pythagoras: c^2 = a^2 + b^2.
So, c = Sqrt(A^2 + B^2); [BANANA]!!NOT JUST A + B. !![/BANANA]

a = delta y = abs(y1 - y2);
b = delta x = abs(x1 - x2);

Now, you don't need the abs() because you have to Square it. (Square is always positive)

Dist := Sqrt(Sqr(x1-x2) + Sqr(y1-y2));

There you go! :)

LordGregGreg

2008-05-30 13:35:39

[php]

function distance($a,$b)
{
return abs(a-b);
}
function distance($x1,$y1,$x2,$y2)
{
$deltaX = $x2-$x1;
$deltaY = $y2-$y1;
return sqrt(pow($deltaX,2) + pow($deltaY,2));
}
function distance($x1,$y1,$z1,$x2,$y2,$z2)
{
return sqrt(pow($x1-$x2,2) + pow($y1-$y2,2) + pow($z1-$z2,2));
}
function distance($x1,$y1,$z1,$t1,$x2,$y2,$z2,$t2)
{
return sqrt(pow($x1-$x2,2) + pow($y1-$y2,2) + pow($z1-$z2,2) + pow($t1-$t2,2));
}
[/php]

and unless you have been a taxi or pizza driver your entire life, you will always use 2-norm.

R0b0t1

2008-05-30 14:23:25

I know the pythagorean thing, but computers can't draw triangles :p

Bobarkinator

2008-05-30 14:26:18

They can contain the points of a triangle.

LordGregGreg

2008-05-30 14:31:31

From: R0b0t1
I know the pythagorean thing, but computers can't draw triangles :p


Polygon p = new Polygon();
p.addPoint(0,0);
p.addPoint(5,0);
p.addPoint(5,5);
g.fillPolygon(p);

R0b0t1

2008-05-30 15:07:05

Still not drawing. :p

Killerdou

2008-05-30 16:48:37

canvas.setbrush.color / canvas.setpen.color ???

bullzeye95

2008-05-30 17:53:19

R0b0t, Freddy gave you all you need. In SCAR:
function Distance(x1, y1, x2, y2: Integer): Integer;
begin
Result:= Round(sqrt(sqr(x2 - x1) + sqr(y2 - y1)));
end;

mixster

2008-05-30 18:00:10

Just to add something, you make the right-angled triangle (which is needed for Pythagoras to work) from the point by simply using a mixture of the 2 points:
…………………….x1,y1


x2,y2……………..x1,y2

The right angle is created using x2,y1 or you can have it upside-down by using x2,y1. You can then use Pythagoras as Bullzeye, Freddy, Wizzup? etc. have said above :)

Macrosoft

2008-05-30 22:18:06

Robot, If you still don't get it, think of it this way. The x-axis is perpendicular to the y-axis. The distance between two points is the hyp. of a right triangle.


The red line is the distance we are trying to find the distance of. You know that the Pythagorean Theorem states that in a right triangle, for legs a and b and hyp. c, a squared plus b squared = c squared. Look at the diagram. You notice how the change in x and the change in y of the two points make two new lines that are perpendicular? A right triangle is produced using the distance that we are trying to find as the hyp and the change in x and the change in y as legs. Therefore, the change in y squared + the change in x squared = the distance between the two points squared. Then square root both sides of the equation.

Dont worry, the only reason I know this is because I skipped 8th grade math into 9th grade geometry. You are younger and shouldn't be expected to know this, but now you do.

n3ss3s

2008-05-31 05:14:26

If it ain't no right triangle and you know an angle in there, that's where Duke would say "Killing time" or something similar.

(

Let's put it like this -

a = adjacent, b = opposite, c = hypotenuse
alpha = between c and a
beta = between b and a
delta = between c and b

)

a^2 = c^2 + b^2 - 2 * b * c * cos delta
b^2 = c^2 + a^2 - 2 * a * c * cos alpha
c^2 = a^2 + b^2 - 2 * a * b * cos beta

Cosine law is the alternative for Pythagora's theorem when dealing with a non-right triangle.


You are younger and shouldn't be expected to know this, but now you do.


I'm 13 :p

Wizzup?

2008-05-31 06:18:05

From: n3ss3s
If it ain't no right triangle and you know an angle in there, that's where Duke would say "Killing time" or something similar.

(

Let's put it like this -

a = adjacent, b = opposite, c = hypotenuse
alpha = between c and a
beta = between b and a
delta = between c and b

)

a^2 = c^2 + b^2 - 2 * b * c * cos delta
b^2 = c^2 + a^2 - 2 * a * c * cos alpha
c^2 = a^2 + b^2 - 2 * a * b * cos beta

Cosine law is the alternative for Pythagora's theorem when dealing with a non-right triangle.




I'm 13 :p


(Co)sine law, extended Pythagoras… I don't think he needs this, _at all_, because if he has two points, he can make a triangle which contains a 90 degree corner. :p
Also, cos 90 = 0, which brings us back to our original Pythagoras.

n3ss3s

2008-05-31 10:36:56

From: Wizzup?
(Co)sine law, extended Pythagoras… I don't think he needs this, _at all_, because if he has two points, he can make a triangle which contains a 90 degree corner. :p
Also, cos 90 = 0, which brings us back to our original Pythagoras.



If it ain't no right triangle and you know an angle in there, that's where Duke would say "Killing time" or something similar.


If he hasn't got two points but a triangle… Anyway.

Wizzup?

2008-05-31 10:40:07

From: n3ss3s
If he hasn't got two points but a triangle… Anyway.


Distance between the two points… Not a triangle…

Macrosoft

2008-05-31 12:03:22

Comon n3s, your making him feel bad. He doesn't need the whole trig explanation. The average kid wouldn't know this at 13.

Then again, we aren't average kids (Im 13 too).

Anyways, your supposed to simplify the idea so he understands, not complicate it further by bringing trig into this!

itSchRis917

2008-05-31 23:29:12

The description was given a long time ago.. Just use the distance formula..

Bam Bam

2008-06-01 13:24:38

Haha, I read all the way to the bottom of the first page thinking this was just some random noob asking, but it was R0b0t xD.

And this is indeed really simple. My geometry teacher thought it was easier to remember it as D = sqrt(Leg1^2 + Leg2^2) so I just thought I would add that.

R0b0t1

2008-06-02 12:59:13

Wow… postcount up much?

Problems already been solved people.

Wizzup?

2008-06-02 13:54:36

From: R0b0t1
Wow… postcount up much?

Problems already been solved people.

Well thats a nice way to thank me for all the effort I put into my post…

Nava2

2008-06-02 19:14:42

From: Wizzup?
Well thats a nice way to thank me for all the effort I put into my post…


Thank you :).

Also, maybe the distance function should be put into SRL… :)

Nava2

Wizzup?

2008-06-02 19:59:39

From: Nava2
Thank you :).

Also, maybe the distance function should be put into SRL… :)

Nava2


It is in SCAR. :)

R0b0t1

2008-06-02 21:09:56

Well, thank you, put I know the whole pythagorean thing. I just couldn't apply it properly.

Nava2

2008-06-03 15:19:42

From: Wizzup?
It is in SCAR. :)


Thanks :D.

Nava2