SRL Archive - SRL Archives

Pythagorean Theorem'er


Abyssal

2009-06-28 06:27:48


{*******************************************************************************
function Pythag(a, bHypot: Extended; NeedHypot: Boolean): Extended;
By: Abyssal
Description: Returns the third side of the triangle.
*******************************************************************************}
function Pythag(a, bHypot: Extended; NeedHypot: Boolean): Extended;
begin
if(NeedHypot)then
Result := Sqrt((Sqr(a) + Sqr(bHypot)));
if(not(NeedHypot))then
Result := Sqrt((Sqr(bHypot) - Sqr(a)));
end;

Set boolean "NeedHypot" if the hypothesis is the variable being solved for. Example:

Pythag(3, 4, True);

If you have the hypothesis and are solving for a different side (doesn't matter which), "NeedHypot" becomes false, and the hypotenuse is put into "b", hence the weird naming scheme. Example:

Pythag(4, 5, False);


Test it!

program PythagTest;

function Pythag(a, bHypot: Extended; NeedHypot: Boolean): Extended;
begin
if(NeedHypot)then
Result := Sqrt((Sqr(a) + Sqr(bHypot)));
if(not(NeedHypot))then
Result := Sqrt((Sqr(bHypot) - Sqr(a)));
end;

begin
Writeln(Pythag(4, 5, False));
Writeln(Pythag(3, 4, True));
end.

BobboHobbo

2009-06-28 06:30:53

This would of been handy in year 8 maths xD.

ian.

2009-06-28 06:32:28

:D YAY PYTHAGORAS! :p

Abyssal

2009-06-28 06:36:12

Hey Bobbo! You know what :P? Just kidding, however it does simplify the process. I mean, that's what includes are really about.

@99, no kidding. He's pretty much my idol.

ian.

2009-06-28 06:37:28

I like his math, but not his philosophy (He killed peoples D: lol)

BobboHobbo

2009-06-28 06:37:31

@Abby xD
Lol no i didnt mean like that i meant to say would of been usefull if you released it last year then i could like use it :).

Nadeem

2009-06-28 06:48:08

Never seen you before, but I like your name :p

Also don't we already have something like this? (Hypot(X, Y: Extended): Extended;), Although yours has an extra feature.

function Pythag(a, bHypot: Extended; NeedHypot: Boolean): Extended;
begin
if(NeedHypot)then
Result := Hypot(a, bHypot)
else
Result := Sqrt((Sqr(bHypot) - Sqr(a)));
end;



~NS

JAD

2009-06-28 06:59:03

Shortened even more:


function Pythag(a, bHypot: Extended; NeedHypot: Boolean): Extended;
begin
Result := Hypot(a, bHypot)
if(not(NeedHypot))then
Result := Sqrt((Sqr(bHypot) - Sqr(a)));
end;

Just being picky :p

Nice. Haven't seen you around here Abyssal in a long time :)

ian.

2009-06-28 07:01:33

Shortened even more:


function Pythag(a, bHypot: Extended; NeedHypot: Boolean): Extended;
begin
Result := Hypot(a, bHypot);
if(not(NeedHypot))then Result := Sqrt((Sqr(bHypot) - Sqr(a)));
end;

>.> Just to prove that shortening this does nothing.

Can't wait to see what you do in the future, Abyssal ;D

Nadeem

2009-06-28 07:17:43

It really doesn't matter if you shorten it lol


function Pythag(a, bHypot: Extended; NeedHypot: Boolean): Extended;
begin
if NeedHypot then Result := Hypot(a, bHypot) else Result := Sqrt(Sqr(bHypot) - Sqr(a));
end;



~NS

bullzeye95

2009-06-28 07:34:49

function Pythag(a, bHypot: Extended; NeedHypot: Boolean): Extended;
begin
Result:= Sqrt(abs(Sqr(bHypot) + (Integer(NeedHypot) * 2 - 1) * sqr(a)));
end;
I win.

ian.

2009-06-28 07:40:24

From: bullzeye95
function Pythag(a, bHypot: Extended; NeedHypot: Boolean): Extended;
begin
Result:= Sqrt(abs(Sqr(bHypot) + (Integer(NeedHypot) * 2 - 1) * sqr(a)));
end;
I win.


No, you lost the game because you're breathing manually.

senrath

2009-06-28 07:41:53

From: bullzeye95
function Pythag(a, bHypot: Extended; NeedHypot: Boolean): Extended;
begin
Result:= Sqrt(abs(Sqr(bHypot) + (Integer(NeedHypot) * 2 - 1) * sqr(a)));
end;
I win.


Ah, taking advantage of the fact that False = 0 and True = 1 (by default). I approve.

Abyssal

2009-06-28 08:46:11

Oh. Wow. Haha, I prefer slightly longer code and readability ;).

Yeah, JAD, I'm hoping to make a reappearance for a while.

Nadeem, yeah, unless you were around back during Kaitnieks days I'll probably be stranger to most, heh.

tarajunky

2009-06-28 10:30:15

From: Abyssal
Oh. Wow. Haha, I prefer slightly longer code and readability ;).


+1.

Smartzkid

2009-06-28 10:39:56

*hypotenuse :D

Simtoon

2009-06-28 12:55:28


program New;

Const
aHypot=34;
bHypot=38;
NeedHypot=True;

function Pythag(aHypot, bHypot: Extended; NeedHypot: Boolean): Extended;
begin
Result:= Sqrt(abs(Sqr(bHypot) + (Integer(NeedHypot) * 2 - 1) * sqr(aHypot)));
end;

begin
WriteLn(Pythag(aHypot, bHypot, NeedHypot));
end.

I WIN!

Gu3st...

2009-06-28 14:14:37

From: Simtoon

program New;

Const
aHypot=34;
bHypot=38;
NeedHypot=True;

function Pythag(aHypot, bHypot: Extended; NeedHypot: Boolean): Extended;
begin
Result:= Sqrt(abs(Sqr(bHypot) + (Integer(NeedHypot) * 2 - 1) * sqr(aHypot)));
end;

begin
WriteLn(Pythag(aHypot, bHypot, NeedHypot));
end.

I WIN!

Uuuhm… Why do you win? :p
you added some consts? :P

ZephyrsFury

2009-06-28 14:33:56

Oh wow… its Abyssal! You made me proud of myself when I first joined :D

Abyssal

2009-06-28 18:56:13

What did I say, Zephyrs? :)

@Tara, heh, thanks.

ZephyrsFury

2009-06-28 19:40:48

From: Abyssal
What did I say, Zephyrs? :)

@Tara, heh, thanks.


Not much apparently :p
http://www.villavu.com/forum/showthread.php?p=132189#post132189

Abyssal

2009-06-28 21:49:21

Haha :).

Glad to be back.

jakeyboy29

2009-06-28 21:54:23

sin, cos,tan
please?

senrath

2009-06-28 21:55:25

From: jakeyboy29
sin, cos,tan
please?


You mean the sine, cose, and tan functions that are built into SCAR?

Naum

2009-06-28 22:08:40

You could make one using the sine and the cosine rule, so it can work with angles aswell :)

Laur€ns

2009-06-28 22:23:54

Sine law is easy :p Cosine law, never heard of..

Naum

2009-06-28 22:31:23

http://en.wikipedia.org/wiki/Cosine_law

It is really simple :)

Abyssal

2009-06-29 02:33:47

Oh you mean the law of sines and cosines?

I'd have to pull out my Algebra notes from this year, forgot some of it, but I can look into it :)

Awkwardsaw

2009-07-05 19:58:28

D=. i wish i had this in summer school (we dont do pythag or trig till sophomore maths here)

n3ss3s

2009-07-06 22:17:28

EDIT: Oh nvm ;)

Also Nauman, if you make a function for the Sine/Cosine law, remember that giving input as 2 points is just stupid since computers can form a right triangle from 2 points, so remember to make it input the side lengths and angles…

Naum

2009-07-07 00:09:48

From: n3ss3s
EDIT: Oh nvm ;)

Also Nauman, if you make a function for the Sine/Cosine law, remember that giving input as 2 points is just stupid since computers can form a right triangle from 2 points, so remember to make it input the side lengths and angles…


Who said I was making it ;). And yes I shall bear that in mind, if I happen to at some stage make a Cosine Law'er..

mixster

2009-07-07 13:17:11


function CosineLaw(a, b, c: Extended; len: Boolean): Extended;
//a^2 = b^2 + c^2 - 2bcCosA
begin
if (len) then
Result := Sqrt(Sqr(b) + Sqr(c) - (2 * b * c * Cos(a)))
else
Result := ArcCos((Sqr(c) + Sqr(b) - Sqr(a)) / (2 * b * c));
end;

function SineLaw(a, b, ang: Extended; len: Boolean): Extended;
// (a / sin A) = (b / Sin B)
begin
if (len) then
Result := (sin(a) * b) / (Sin(ang))
else
Result := ArcSin((a * Sin(ang)) / b );
end;
Assuming you can entrust the user to pass the correct variables, it works.
For CosineLaw, a is the length opposite the angle you wish to work out or the angle opposite the side you wish to work out.
For SineLaw, a is either the angle or length you have that's opposite the length or angle you're trying to work out while b and ang are the opposite angle/length pair you have.
Angles in all cases refer to radians rather than degrees, but not hard to alter it.

Not hard to implement and pretty much useless here since it's all about points and points mean right angle triangles :)